How to add a new Runnable to an ExecutorService?

0

This is the first time I work with threads ...    The method below runs every 15 seconds by EJB.    I would like if in case there is an SMS list in the database it would add to a new SMS trigger thread

But you are 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]

Follow 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();
        }
    }
}
    
asked by anonymous 15.10.2014 / 15:44

1 answer

1

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

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

However, you do not 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.

    
15.10.2014 / 22:00