javax.mail.Session bean 并发。如何控制?

javax.mail.Session bean concurrency. How to control?

我是运行一个带有@Schedule 方法的@Singleton bean,它轮询注入了@Resource 并在WildFly 部署中定义的gmail imap 服务。当我以高频率(每 5 秒)轮询服务时,有时会出现同时连接过多的错误。可能是因为连接到 gmail 不接受的 gmail 服务的 bean 太多。如何控制同时 javax.mail.Session 个 bean 的数量?

相关代码:

@Singleton
public class CustomerEmailController {

    @Resource(mappedName = "java:/MyGMail")
    Session mailSession;

    @Schedule(second = "*/5", minute = "*", hour = "*")
    public void readFromMailBoxAndPersistToDatabase() throws MessagingException {
    // Call mailSession.getStore here. Open folder, close folder and store.
    }

错误日志不时显示以下内容(大约二十分之一):

00:36:19,497 ERROR [org.jboss.as.ejb3] (EJB default - 3) JBAS014120: Error invoking timeout for timer: [id=0e58a318-9f7d-49ee-8143-3e99c53cc5ea timedObjectId=my-analyzer.my-analyzer.CustomerEmailController auto-timer?:true persistent?:true timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@46795c57 initialExpiration=null intervalDuration(in milli sec)=0 nextExpiration=Tue Jan 27 00:33:10 CET 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@1ee15b3a - could not obtain lock within 5000MILLISECONDS
    at org.jboss.as.ejb3.concurrency.ContainerManagedConcurrencyInterceptor.processInvocation(ContainerManagedConcurrencyInterceptor.java:100) [wildfly-ejb3-8.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:340) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) [wildfly-ejb3-8.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.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.processInvocation(ShutDownInterceptorFactory.java:64) [wildfly-ejb3-8.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.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.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.timerservice.task.CalendarTimerTask.callTimeout(CalendarTimerTask.java:61) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.timerservice.task.TimerTask.run(TimerTask.java:168) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_31]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_31]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_31]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_31]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_31]
    at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final]

00:36:19,509 INFO  [org.jboss.as.ejb3] (EJB default - 3) JBAS014121: Timer: [id=0e58a318-9f7d-49ee-8143-3e99c53cc5ea timedObjectId=my-analyzer.my-analyzer.CustomerEmailController auto-timer?:true persistent?:true timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@46795c57 initialExpiration=null intervalDuration(in milli sec)=0 nextExpiration=Tue Jan 27 00:33:10 CET 2015 timerState=IN_TIMEOUT info=null will be retried
00:36:19,510 INFO  [org.jboss.as.ejb3] (EJB default - 3) JBAS014123: Retrying timeout for timer: [id=0e58a318-9f7d-49ee-8143-3e99c53cc5ea timedObjectId=my-analyzer.my-analyzer.CustomerEmailController auto-timer?:true persistent?:true timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@46795c57 initialExpiration=null intervalDuration(in milli sec)=0 nextExpiration=Tue Jan 27 00:33:10 CET 2015 timerState=IN_TIMEOUT info=null

我在您的堆栈跟踪中没有看到任何 太多连接 错误。错误消息说

concurrent access timeout - could not obtain lock within 5000 MILLISECONDS

这很可能意味着之前预定的通话尚未完成,连接到邮件服务器和下载邮件的时间超过 5 秒。