gpt4 book ai didi

java - 如果Onmessage()方法出现异常,是否将消息保留在Queue中并稍后再次调用MDB

转载 作者:行者123 更新时间:2023-12-01 08:51:02 25 4
gpt4 key购买 nike

我正在使用Oracle Weblogic Server,如何处理mdb onMessage()中的异常。如果有异常那么消息是否会保留在队列中,因为消息没有被处理&是否可以获取稍后留言

这是我的代码:

import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import java.util.logging.Logger;

import javax.ejb.ActivationConfigProperty;

@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") ,
@ActivationConfigProperty(propertyName="connectionFactoryJndiName",propertyValue="TestQCF"),
@ActivationConfigProperty(propertyName="destinationJndiName", propertyValue="testQ")
}
,mappedName="testQ"
)

public class MyMDB implements MessageListener{
public void onMessage(Message message){

TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Received message is :"+ textMessage.getText());

}
catch (JMSException e){
e.printStackTrace();
}
}
}

最佳答案

If there is an exception then message will be retained in the queue or not ,Because the message is not processed & is it possible to get the message later

这取决于为 MDB 指定的事务属性。 MDB 由容器调用,当 transaction_type 设置为 CONTAINER 时,只能指定两种事务类型 - REQUIRED 或 NOT_SUPPORTED,默认为后者。

当事务类型设置为 REQUIRED 时,异常会导致事务回滚,并且当回滚 MDB 容器管理事务时,容器会重新传递消息。

您还可以在出现异常时显式回滚事务。

public void onMessage(Message m){
try{
if(m.getJMSRedelivered()){
//do whatever for redelivery cases
}
//do other stuff
}catch(Exception e){
//log
msgContext.setRollbackOnly();
}
}

我经常回到这个资源http://www.javaworld.com/article/2074123/java-web-development/transaction-and-redelivery-in-jms.html了解消息重新传递和事务。它于 2002 年出版,但对我来说,它是解释 JMS 事务和重新传递的最好的文章。

关于java - 如果Onmessage()方法出现异常,是否将消息保留在Queue中并稍后再次调用MDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42408774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com