1. java
  2. android
  3. c#
  4. .net
  5. javascript
  6. php
  7. jquery
  8. html
  9. sql

JAVA - ConcurrentAccessTimeoutException em classe Singleton com Schedule

Estou tentando implementar uma classe que deverá ser executada de tempos em tempos automaticamente, mas eu não sei quanto será o tempo de processamento então tentei simular um tempo maior que a próxima chamada da rotina. Só que estou tomando a exceção ConcurrentAccessTimeoutException.

Segue o código - Classe principal:

@Singleton
public class CartaoMultiemissor {

    private transient Logger LOGGER = Logger.getLogger("CartaoMultiemissor");

    @EJB
    private PagamentoDAO pagamentoDAO;

    @Schedule(second="*/10", minute="*", hour="*", persistent = false)
    @AccessTimeout(unit=TimeUnit.SECONDS, value=10)
    public void execute() {

        System.out.println("Executando ...");
        System.out.println("Hora da Execução : " + new Date());
        System.out.println("____________________________________________");   

        String simpleName = this.getClass().getSimpleName();

        if (!pagamentoDAO.isRunning()) {

            System.out.println(simpleName + " começou.");

            try {
                pagamentoDAO.doWork();

            } catch (Exception e) {
                LOGGER.error("doWork", e);
            }

            System.out.println(simpleName + " terminou.");
        }else{
            System.out.println(simpleName + " ainda em execução... Aguarde.");
        }
    }
}

Classe PagamentoDAO

@Stateless
public class PagamentoDAO {

    private static final long serialVersionUID = 1L;
    private transient Logger LOGGER = Logger.getLogger("CartaoMultiemissor");

    private boolean running = false;

    @Lock(LockType.READ)
    public boolean isRunning() {

        return this.running;
    }

    @Lock(LockType.WRITE)
    public void doWork() throws Exception {

        String simpleName = this.getClass().getSimpleName();
        this.running = true;
        System.out.println(simpleName + "work running…");
        Thread.sleep(20000); // dorme 20 segundos para simular um processamento maior que a próxima execuçao
        System.out.println(simpleName + "work finished.");
        this.running = false;

    }
}

Saída:

16:49:50,034 INFO  [stdout] (EJB default - 1) Executando ...

16:49:50,034 INFO  [stdout] (EJB default - 1) Hora da Execução : Wed Dec 02 16:49:50 BRST 2015

16:49:50,034 INFO  [stdout] (EJB default - 1) ____________________________________________

16:49:50,036 INFO  [stdout] (EJB default - 1) CartaoMultiemissor começou.

16:49:50,037 INFO  [stdout] (EJB default - 1) PagamentoDAOImplwork running…

16:49:50,129 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-12) Inicializando Mojarra 2.2.6-jbossorg-4 20140501-1134 para o contexto '/CartaoMultiemissor'
16:49:51,211 INFO  [org.primefaces.webapp.PostConstructApplicationEventListener] (MSC service thread 1-12) Running on PrimeFaces 5.0
16:49:51,219 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-12) JBAS017534: Registered web context: /CartaoMultiemissor
16:49:51,253 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "mysql-connector-java-5.1.31-bin.jar" (runtime-name : "mysql-connector-java-5.1.31-bin.jar")
16:49:51,254 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "CartaoMultiemissor.war" (runtime-name : "CartaoMultiemissor.war")
16:49:51,349 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
16:49:51,350 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
16:49:51,350 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.1.0.Final "Kenny" started in 5264ms - Started 458 of 516 services (99 services are lazy, passive or on-demand)
16:50:10,010 ERROR [org.jboss.as.ejb3] (EJB default - 2) JBAS014120: Error invoking timeout for timer: [id=3b577156-b850-43e5-9d80-f2a42f9c45f3 timedObjectId=CartaoMultiemissor.CartaoMultiemissor.CartaoMultiemissor auto-timer?:true persistent?:false timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@17956c5 initialExpiration=null intervalDuration(in milli sec)=0 nextExpiration=Wed Dec 02 16:50:20 BRST 2015 timerState=IN_TIMEOUT info=null: javax.ejb.ConcurrentAccessTimeoutException: JBAS014373: EJB 3.1 PFD2 4.8.5.5.1 concurrent access timeout on org.jboss.invocation.InterceptorContext$Invocation@1db4a7b - could not obtain lock within 10SECONDS
    at org.jboss.as.ejb3.concurrency.ContainerManagedConcurrencyInterceptor.processInvocation(ContainerManagedConcurrencyInterceptor.java:100) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:407) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:55)
    at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:83) [wildfly-weld-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45) [wildfly-ee-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53) [wildfly-ee-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.component.singleton.SingletonComponentInstanceAssociationInterceptor.processInvocation(SingletonComponentInstanceAssociationInterceptor.java:52) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:273) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:340) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:95) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) [wildfly-ee-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:448) [wildfly-security-manager-1.0.0.Final.jar:1.0.0.Final]
    at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.timerservice.TimedObjectInvokerImpl.callTimeout(TimedObjectInvokerImpl.java:104) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.as.ejb3.timerservice.task.CalendarTimerTask.callTimeout(CalendarTimerTask.java:61) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at org.jboss.as.ejb3.timerservice.task.TimerTask.run(TimerTask.java:168) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [rt.jar:1.7.0_51]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [rt.jar:1.7.0_51]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_51]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_51]
    at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_51]
    at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final]

16:50:10,019 INFO  [org.jboss.as.ejb3] (EJB default - 2) JBAS014121: Timer: [id=3b577156-b850-43e5-9d80-f2a42f9c45f3 timedObjectId=CartaoMultiemissor.CartaoMultiemissor.CartaoMultiemissor auto-timer?:true persistent?:false timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@17956c5 initialExpiration=null intervalDuration(in milli sec)=0 nextExpiration=Wed Dec 02 16:50:20 BRST 2015 timerState=IN_TIMEOUT info=null will be retried
16:50:10,020 INFO  [org.jboss.as.ejb3] (EJB default - 2) JBAS014123: Retrying timeout for timer: [id=3b577156-b850-43e5-9d80-f2a42f9c45f3 timedObjectId=CartaoMultiemissor.CartaoMultiemissor.CartaoMultiemissor auto-timer?:true persistent?:false timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@17956c5 initialExpiration=null intervalDuration(in milli sec)=0 nextExpiration=Wed Dec 02 16:50:20 BRST 2015 timerState=IN_TIMEOUT info=null
16:50:10,036 INFO  [stdout] (EJB default - 1) PagamentoDAOImplwork finished.

16:50:10,037 INFO  [stdout] (EJB default - 1) CartaoMultiemissor terminou.

16:50:10,038 INFO  [stdout] (EJB default - 3) Executando ...

16:50:10,039 INFO  [stdout] (EJB default - 3) Hora da Execução : Wed Dec 02 16:50:10 BRST 2015

16:50:10,040 INFO  [stdout] (EJB default - 3) ____________________________________________

16:50:10,043 INFO  [stdout] (EJB default - 3) CartaoMultiemissor começou.

16:50:10,045 INFO  [stdout] (EJB default - 3) PagamentoDAOImplwork running…

A aplicação nem chega a tentar executar o método execute() e lança a exceção. Se eu estimar um valor de timeout alto ele aparentemente funciona, mas isso não é adequado para a minha aplicação, pois envolverá chamadas a serviços externos. Quero que caso dê o tempo de executar de novo e ele ainda esteja processando, ele entre no método e na validação do isRunning()

Alguém pode me ajudar?!?!

  1. Você vai ver essas setas em qualquer página de pergunta. Com elas, você pode dizer se uma pergunta ou uma resposta foram relevantes ou não.
  2. Edite sua pergunta ou resposta caso queira alterar ou adicionar detalhes.
  3. Caso haja alguma dúvida sobre a pergunta, adicione um comentário. O espaço de respostas deve ser utilizado apenas para responder a pergunta.
  4. Se o autor da pergunta marcar uma resposta como solucionada, esta marca aparecerá.
  5. Clique aqui para mais detalhes sobre o funcionamento do GUJ!

1 resposta

Não é a resposta que estava procurando? Procure outras perguntas com as tags ejb singleton ou faça a sua própria pergunta.