2424public class talk extends AbstractApplication {
2525
2626 private static final long TIMEOUT = 200 ;
27+ private static final int DEFAULT_POOL_SIZE = 3 ;
2728 protected final Map <String , Queue <Builder >> list = new ConcurrentHashMap <String , Queue <Builder >>();
2829 protected final Map <String , Queue <Builder >> meetings = new ConcurrentHashMap <String , Queue <Builder >>();
2930 protected final Map <String , List <String >> sessions = new ConcurrentHashMap <String , List <String >>();
30- private final ExecutorService service = Executors . newFixedThreadPool ( 3 ) ;
31+ private ExecutorService service ;
3132
3233 @ Override
3334 public void init () {
@@ -36,23 +37,25 @@ public void init() {
3637 this .setAction ("talk/version" , "version" );
3738 this .setAction ("talk/testing" , "testing" );
3839
39- Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
40- @ Override
41- public void run () {
42- service .shutdown ();
43- while (true ) {
44- try {
45- System .out .println ("Waiting for the service to terminate..." );
46- if (service .awaitTermination (5 , TimeUnit .SECONDS )) {
47- System .out .println ("Service will be terminated soon." );
40+ if (this .service != null ) {
41+ Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
42+ @ Override
43+ public void run () {
44+ service .shutdown ();
45+ while (true ) {
46+ try {
47+ System .out .println ("Waiting for the service to terminate..." );
48+ if (service .awaitTermination (5 , TimeUnit .SECONDS )) {
49+ System .out .println ("Service will be terminated soon." );
4850 break ;
49- }
50- } catch (InterruptedException e ) {
51- e .printStackTrace ();
52- }
53- }
54- }
55- }));
51+ }
52+ } catch (InterruptedException e ) {
53+ e .printStackTrace ();
54+ }
55+ }
56+ }
57+ }));
58+ }
5659 }
5760
5861 /**
@@ -97,7 +100,7 @@ public final String save(final Object meetingCode, final Builder builder) {
97100 this .meetings .notifyAll ();
98101 }
99102
100- service .execute (new Runnable (){
103+ this . getService () .execute (new Runnable (){
101104 @ Override
102105 public void run () {
103106 synchronized (talk .this .meetings ) {
@@ -117,6 +120,10 @@ public void run() {
117120 return builder .toString ();
118121 }
119122
123+ private ExecutorService getService () {
124+ return this .service !=null ? this .service : Executors .newFixedThreadPool (DEFAULT_POOL_SIZE );
125+ }
126+
120127 /**
121128 * Poll message from the messages of the session specified sessionId.
122129 * @param sessionId
@@ -197,7 +204,7 @@ public boolean testing(final int n) throws ApplicationException {
197204 sess .add ("{B}" );
198205 this .sessions .put ("[M001]" , sess );
199206
200- service .execute (new Runnable (){
207+ this . getService () .execute (new Runnable (){
201208 @ Override
202209 public void run () {
203210 int i =0 ;
@@ -215,7 +222,7 @@ public void run() {
215222 }
216223 });
217224
218- service .execute (new Runnable (){
225+ this . getService () .execute (new Runnable (){
219226 @ Override
220227 public void run () {
221228 int i =0 ;
@@ -233,7 +240,7 @@ public void run() {
233240 }
234241 });
235242
236- service .execute (new Runnable (){
243+ this . getService () .execute (new Runnable (){
237244 @ Override
238245 public void run () {
239246 // TODO Auto-generated method stub
@@ -252,7 +259,7 @@ public void run() {
252259 }
253260 });
254261
255- service .execute (new Runnable (){
262+ this . getService () .execute (new Runnable (){
256263 @ Override
257264 public void run () {
258265 // TODO Auto-generated method stub
0 commit comments