How to add a new Runnable to an Executorservice?

Asked

Viewed 295 times

0

This is the first time I’ve worked with threads... The method below runs every 15 seconds by the EJB. I wanted that if there is SMS list in the bank it add to a new thread triggering these SMS

But it is returning the following error:

Caused by: java.util.concurrent.RejectedExecutionException: Task com.core.framework.timerservice.TimerSessionBeanSmsLote$1@1e13b49e rejected from java.util.concurrent.ThreadPoolExecutor@1945ea4f[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]
10:29:17,342 ERROR [org.jboss.as.ejb3] (EJB default - 1) JBAS014122: Error during retrying timeout for timer: [id=f3aba899-a38f-4111-bbc6-2465b9dfcbf0 timedObjectId=PortalEAR.PortalEJB.TimerSessionBeanSmsLote auto-timer?:true persistent?:true timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@2a060c86 initialExpiration=Thu Oct 09 00:00:00 BRT 2014 intervalDuration(in milli sec)=0 nextExpiration=Wed Oct 15 05:07:45 BRT 2014 timerState=IN_TIMEOUT: javax.ejb.EJBException: java.util.concurrent.RejectedExecutionException: Task com.core.framework.timerservice.TimerSessionBeanSmsLote$1@1ad48394 rejected from java.util.concurrent.ThreadPoolExecutor@1945ea4f[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]

Follows the code:

@Singleton
@TransactionManagement(TransactionManagementType.BEAN)
public class TimerSessionBeanSmsLote {

    private ExecutorService service = Executors.newFixedThreadPool(10);
    private List smsLoteList;

    // Esse método é executado de 15 em 15 segundos.........
    @Schedule(hour="*", minute="*", second="*/15") 
    @AccessTimeout(value = 20, unit = TimeUnit.DAYS)
    public void automaticTimeout()  {
        // pega a lista de sms no banco.............
        smsLoteList = buscaOsSMSnoBANCO();

        if(!smsLoteList.isEmpty()){
            service.execute(new Runnable() {
                @Override
                public void run() {
                   //Envia SMS.............
                }
            });
            service.shutdown();
        }
    }
}
  • 1

    When you do service.shutdown(), the ExecutorService passes to reject tasks and fires this exception. You can also reject if the tasks limit is reached; you set this limit to 10 - will it always be enough? Finally, you really need this process to run in another thread?

  • 2

    Never initialize threads not managed by the container on an EJB. Use the new competition API:http://docs.oracle.com/javaee/7/tutorial/doc/concurrency-utilities.htm

  • 2

    Finally, I see no need to work with threads here. A new Session bean (non-Singleton) with an asynchronous method for sending SMS would ensure competition.

1 answer

1

The way you added Runnable is correct and the way to add a new one is by not invoking service.shutdown().

Itso answers your question. Removing the service.shutdown() you will no longer have this exception.

However, you don’t seem to have a good design there. Analyzing your need and considering only what you have exposed, thread there is an exaggeration and in fact you should not try to manage threads in an EJB container.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.