JMS Producer已关闭

时间:2014-12-05 09:12:45

标签: ejb jms wildfly hornetq

通过Hornetq使用Wildfly和JMS我得到了这个例外

javax.ejb.EJBTransactionRolledbackException: Producer is closed

每隔几个小时发生一次或两次。

Caused by: javax.jms.IllegalStateRuntimeException: Producer is closed
        at org.hornetq.jms.client.JmsExceptionUtils.convertToRuntimeException(JmsExceptionUtils.java:60) [hornetq-jms-client-2.4.1.Final.jar:]
        at org.hornetq.jms.client.HornetQJMSProducer.send(HornetQJMSProducer.java:112) [hornetq-jms-client-2.4.1.Final.jar:]
        at org.hornetq.jms.client.HornetQJMSProducer.send(HornetQJMSProducer.java:230) [hornetq-jms-client-2.4.1.Final.jar:]
        at com.turbineam.tradeagent.message.MessageSender.sendMessage(MessageSender.java:26) [classes:]
        at com.turbineam.tradeagent.agent.TradeAgent.sendMessage(TradeAgent.java:36) [classes:]
        at com.turbineam.tradeagent.agent.TradeAgent.handleMessage(TradeAgent.java:82) [classes:]
        at sun.reflect.GeneratedMethodAccessor273.invoke(Unknown Source) [:1.8.0_05]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_05]
        at java.lang.reflect.Method.invoke(Method.java:483) [rt.jar:1.8.0_05]
        at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
        at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
        at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:407)
        at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:82) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
        at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:93) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
        at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
        at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
        at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
        at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:407)
        at org.jboss.as.ejb3.concurrency.ContainerManagedConcurrencyInterceptor.processInvocation(ContainerManagedConcurrencyInterceptor.java:104) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
        at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:407)
        at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
        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)
        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)
        at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
        at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
        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)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:251) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
        ... 201 more
Caused by: javax.jms.IllegalStateException: Producer is closed
        at org.hornetq.jms.client.HornetQMessageProducer.checkClosed(HornetQMessageProducer.java:537) [hornetq-jms-client-2.4.1.Final.jar:]
        at org.hornetq.jms.client.HornetQMessageProducer.send(HornetQMessageProducer.java:206) [hornetq-jms-client-2.4.1.Final.jar:]
        at org.hornetq.jms.client.HornetQMessageProducer.send(HornetQMessageProducer.java:200) [hornetq-jms-client-2.4.1.Final.jar:]
        at org.hornetq.ra.HornetQRAMessageProducer.send(HornetQRAMessageProducer.java:145)
        at org.hornetq.jms.client.HornetQJMSProducer.send(HornetQJMSProducer.java:107) [hornetq-jms-client-2.4.1.Final.jar:]
        ... 240 more

发件人创建如下:

@Slf4j
public class MessageSender {

    @Inject
    @JMSConnectionFactory("java:jboss/DefaultJMSConnectionFactory")
    private JMSContext context;

    public void sendMessage(Destination destination, String messageId, Serializable serializable) throws NamingException, JMSException {

        log.debug("Sending a message: " + serializable.toString());
        context.createProducer().setJMSCorrelationID(messageId).send(destination, serializable);
    }
}

也许我错过了一些东西,但我不明白为什么会这样。如何妥善处理?我绝对需要发送这条消息无所谓。提前谢谢。

编辑:

我认为最重要的部分是让我解决这个问题的原因是我不时得到(除了封闭的制片人):

WARN  [org.hornetq.core.client] (ejb) HQ214021: Invalid concurrent session usage. Sessions are not supposed to be used by more than one thread concurrently.

我注入MessageSender的Bean使用@Stateless注释。

1 个答案:

答案 0 :(得分:2)

好的,经过多次尝试,我设法解决了这个问题。这种设置有什么问题,如果没有在MessageSender上提供注释确定范围,那么它就是@Dependent。这意味着它被注入的bean的范围。在那种情况下@Stateless。只创建了一个会话。 @Stateless bean的方法可以同时运行,这是问题的主要原因。 sendMeasge使用相同的JMS会话。解决此问题的最简单方法是确保sendMessage中始终存在一个thred或创建多个会话。我找到的解决方案之一是以下列方式使用@RequestScope:

@Slf4j
@RequestScope
public class MessageSender {

    @Inject
    @JMSConnectionFactory("java:jboss/DefaultJMSConnectionFactory")
    private JMSContext context;

    public void sendMessage(Destination destination, String messageId, Serializable serializable) throws NamingException, JMSException {

        log.debug("Sending a message: " + serializable.toString());
        context.createProducer().setJMSCorrelationID(messageId).send(destination, serializable);
    }
}

在上面的示例中,我们每次发送消息时都会创建会话 - 因此不再存在并发会话使用的问题,并且封闭生产者的问题也消失了。

如果有人有兴趣,我建议您继续阅读: http://www.mastertheboss.com/jboss-server/jboss-jms/jms-20-tutorial-on-wildfly-as