Where can I configure the thread pool behind the @Asynchronous calls in Java EE 6?

前端 未结 2 1444
后悔当初
后悔当初 2021-02-05 23:45

I recently learned that I can easily make any session bean method Asynchronous by simply adding the @Asynchronous annotation.

E.g.

@Asynch         


        
2条回答
  •  感动是毒
    2021-02-06 00:32

    I think timeout could be achieved by invoking Future.cancel(boolean) from a method annotated @Timeout. Requires keeping a reference to the Future returned by the async method, Singleton-ejb can be used for this.

    @Stateless
    public class AsyncEjb {
    
        @Resource
        private SessionContext sessionContext;
    
        @Asynchronous
        public Future asyncMethod() {
    
            ...
            //Check if canceled by timer
            if(sessionContext.wasCancelCalled()) {
                ...
            }
            ...
    
        }
    }
    
    @Singleton
    public class SingletonEjb {
        @EJB
        AsyncEjb asyncEjb;
    
        Future theFuture;
    
        public void asyncMethod() {
    
            theFuture = asyncEjb.asyncMethod();
    
            //Create programatic timer
            long duration = 6000;
            Timer timer =
            timerService.createSingleActionTimer(duration, new TimerConfig());
    
        }
    
        //Method invoked when timer runs out
        @Timeout
        public void timeout(Timer timer) {
            theFuture.cancel(true);
        }
    }
    

    Edit (new below):

    In glassfish you may configure the ejb-pool by seting below attributes in the admin console

    • Initial and Minimum Pool Size
    • Maximum Pool Size
    • Pool Resize Quantity
    • Pool Idle Timeout

    see Tuning the EJB Pool

提交回复
热议问题