JMS TransactionRolledbackLocalException 导致 GlassFish 故障

JMS TransactionRolledbackLocalException leads to GlassFish breakdown

我的基本问题:
MessageDriven bean 在某处抛出一个 Exception,这会导致 JMS Queue 中的回滚,直到它 爆炸 并且服务器崩溃。我什至找不到 Exceptions 来解决问题,因为服务器日志在几分钟内就增长到几百 MB。

我要找的是:
一种首先 清除 JMS Queue 然后 停止 一劳永逸回滚的方法。

我是这样做的:


   消息驱动 Bean 的对象无状态 bean 在这里抛出
 我想发送 ObjectMessage 来接收异常消息
  +------------+ +----------------+ +---------------- + +----------+
  |我的对象 |----->| MessageProducer |-------->|消息接收者 |---->|做事 |
  +------------+ +----------------+ +---------------- + +----------+

我发送的对象是一个简单的serializable POJO。

MessageProducer 看起来像这样:

@Stateless
public class MessageProducer{

    @Resource(mappedName = "jms/JMSConnectionFactory")
    private ConnectionFactory connectionFactory;

    @Resource(mappedName = "jms/MessageReceiver")
    Queue messageReceiver;

    public void sendMessage(PostContainer postContainer){          
        MessageProducer messageProducer;
        // try with resources to close everything on Exception
        try(Connection connection = connectionFactory.createConnection(); 
               Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)){
            messageProducer = session.createProducer(messageReceiver);

            ObjectMessage objectMessage = session.createObjectMessage();
            objectMessage.setObject(myObject);
            objectMessage.setJMSRedelivered(false);  // this doesn't seem to have any effect  
            messageProducer.send(objectMessage);
            messageProducer.close();
        }catch(Exception ex){
            System.out.err("error when sendingMessage .. ignoring"); // when "ignoring" the Exception I thought to at least save me the enormous log messages
        }
    }
}

消息 bean 如下所示:

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/MessageReceiver")
})
public class MessageReceiver implements MessageListener{

    public MessageReceiver(){
    }

    @Override
    public void onMessage(Message message){
        try{
            if(message instanceof ObjectMessage){
                ObjectMessage objectMessage = (ObjectMessage) message;
                MyObject myObject = (MyObject) objectMessage.getObject();
                doStuff(myObject); // the method that handles the incoming object - here the Exception gets thrown
            }
        }catch(Exception ex){
           System.err.println("message could not be received: " + ex.getMessage());  // still trying to ignore exceptions here
        }
    }
}

部分异常

javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
    at com.sun.ejb.containers.EJBContainerTransactionManager.checkExceptionClientTx(EJBContainerTransactionManager.java:662)
    at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:507)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
    ......... (and so on....)
    at com.me.MessageBeans.MessageReceiver.doStuff(MessageReceiver.java:86)
    at com.me.MessageBeans.MessageReceiver.onMessage(MessageReceiver.java:61)
    at sun.reflect.GeneratedMethodAccessor313.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:55)
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
    at sun.reflect.GeneratedMethodAccessor310.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    ......... (and so on....)
Caused by: javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.

 // I try to persist myObject with JPA into the database (that's what's not working right now.. but it's beside the point here
    ......... (and so on....)

现在我的实际问题

:)
我如何停止我的JMS Queue重新传送消息(导致任何Exceptions或无法成功传送任何其他原因)? (例如捕获异常,发送消息时设置一些参数...)

在设置 DestinationResources 时,我是否必须在 GlassFish 中设置 parameter/option连接工厂 ?

我怎么能手动清除队列,因为现在它已满,“重新发送消息",每次部署我的应用程序时,我都会在几分钟内收到大量日志消息。

感谢大家抽空阅读:)

MDB 中的事务管理很棘手。

发生的事情是从您的 JPA Facade 服务方法中抛出 java.lang.RuntimeException,这会自动将当前事务标记为回滚。因此,当它到达 MDB 中的 catch 子句时已经太晚了。 JMS 将重试失败的事务。

任何从您的服务方法中转义的 java.lang.RuntimeException 都会将 MDB 的事务标记为回滚。您可以通过在服务方法中捕获它来防止这种情况。

您可能需要在 JPA Facade 中使用 @TransactionAttribute(REQUIRES_NEW) 注释服务方法,以防 JPA 标记 Tx(对此不确定)或来自服务方法内的另一个 EJB 调用失败。

您可以按照 To Purge Messages From a Physical Destination 中的说明清除您的队列。