4
4
import java .sql .ResultSet ;
5
5
import java .util .LinkedList ;
6
6
import java .util .List ;
7
- import java .util .concurrent .Executor ;
7
+ import java .util .concurrent .ExecutorService ;
8
8
import java .util .concurrent .LinkedBlockingDeque ;
9
9
import java .util .concurrent .RejectedExecutionException ;
10
10
import java .util .concurrent .ThreadPoolExecutor ;
@@ -45,15 +45,15 @@ public class CheckAccessResult {
45
45
public final String credentials ;
46
46
public final AccessRights [] rights ;
47
47
48
- public CheckAccessResult (String username , String credentials , AccessRights [] rights ) {
49
- this .username = username ;
50
- this .credentials = credentials ;
48
+ public CheckAccessResult (CheckAccess ca , AccessRights [] rights ) {
49
+ this .username = ca . username ;
50
+ this .credentials = ca . credentials ;
51
51
this .rights = rights ;
52
52
}
53
53
}
54
54
55
55
public class AccessService extends AbstractActor {
56
- private Executor pool ;
56
+ private ExecutorService pool ;
57
57
58
58
public AccessService (DataSource db , int poolSize , int queueSize ) {
59
59
pool = new ThreadPoolExecutor (0 , poolSize , 60 , SECONDS , new LinkedBlockingDeque <>(queueSize ));
@@ -64,22 +64,26 @@ public AccessService(DataSource db, int poolSize, int queueSize) {
64
64
try {
65
65
pool .execute (() -> checkAccess (db , ca , self ));
66
66
} catch (RejectedExecutionException e ) {
67
- ca .replyTo .tell (new CheckAccessResult (ca . username , ca . credentials , AccessRights .EMPTY ), self );
67
+ ca .replyTo .tell (new CheckAccessResult (ca , AccessRights .EMPTY ), self );
68
68
}})
69
69
.build ());
70
70
}
71
71
72
+ @ Override
73
+ public void postStop () {
74
+ pool .shutdownNow ();
75
+ }
76
+
72
77
private static void checkAccess (DataSource db , CheckAccess ca , ActorRef self ) {
73
- try {
74
- final Connection conn = db .getConnection ();
78
+ try (Connection conn = db .getConnection ()) {
75
79
final ResultSet result = conn .createStatement ().executeQuery ("<figure out access rights>" );
76
80
final List <AccessRights > rights = new LinkedList <>();
77
81
while (result .next ()) {
78
82
rights .add (AccessRights .valueOf (result .getString (0 )));
79
83
}
80
- ca .replyTo .tell (new CheckAccessResult (ca . username , ca . credentials , rights .toArray (AccessRights .EMPTY )), self );
84
+ ca .replyTo .tell (new CheckAccessResult (ca , rights .toArray (AccessRights .EMPTY )), self );
81
85
} catch (Exception e ) {
82
- ca .replyTo .tell (new CheckAccessResult (ca . username , ca . credentials , AccessRights .EMPTY ), self );
86
+ ca .replyTo .tell (new CheckAccessResult (ca , AccessRights .EMPTY ), self );
83
87
}
84
88
}
85
89
}
0 commit comments