微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果消息处理失败,则在 DLQ 中移动消息

如何解决如果消息处理失败,则在 DLQ 中移动消息

我有一个独立的程序来在 IBM MQ 中发送消息。我能够成功地将消息放入队列。

JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();

// Set the properties
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME,HOST);
cf.setIntProperty(WMQConstants.WMQ_PORT,PORT);
cf.setStringProperty(WMQConstants.WMQ_CHANNEL,CHANNEL);
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE,WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER,QMGR);
cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME,"JmsPutGet (JMS)");
cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP,true);
cf.setStringProperty(WMQConstants.USERID,APP_USER);
cf.setStringProperty(WMQConstants.PASSWORD,APP_PASSWORD);

// Create JMS objects
context = cf.createContext();
destination = context.createQueue("queue:///" + QUEUE_NAME);

long uniqueNumber = System.currentTimeMillis() % 1000;
TextMessage message = context.createTextMessage("Your lucky number today is " + uniqueNumber);

producer = context.createProducer();
producer.send(destination,message);

我在 JBoss 中部署了以下 MDB,我从队列中读取消息。我明确抛出 throw new RuntimeException("-----------1111111111--------------"); 以便消息处理失败,并且消息被移动到 DLQ(死信队列)。在我的情况下,发生异常并且消息被多次重新传递,并且一次又一次地调用 EJB 的 onMessage。

@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType",propertyValue = "javax.jms.Queue"),@ActivationConfigProperty(propertyName = "useJNDI",propertyValue = "false"),@ActivationConfigProperty(propertyName = "hostName",propertyValue = "${websphere.hostName}"),@ActivationConfigProperty(propertyName = "username",propertyValue = "${websphere.username}"),@ActivationConfigProperty(propertyName = "password",propertyValue = "${websphere.password}"),@ActivationConfigProperty(propertyName = "port",propertyValue = "${websphere.port}"),@ActivationConfigProperty(propertyName = "channel",propertyValue = "${websphere.channel}"),@ActivationConfigProperty(propertyName = "queueManager",propertyValue = "${websphere.queueManager}"),@ActivationConfigProperty(propertyName = "destination",propertyValue = "${websphere.queueName}"),@ActivationConfigProperty(propertyName = "transportType",propertyValue = "CLIENT"),@ActivationConfigProperty(propertyName = "ackNowledgeMode",propertyValue = "Auto-ackNowledge") })
@ResourceAdapter(value = "wmq.jmsra.rar")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)

public class TestMDB implements MessageListener {

@Override
public void onMessage(Message message) {

if (message instanceof TextMessage) {
    try {
        String mqmsg = ((TextMessage) message).getText();
        System.out.println("MSG RECEIVED: " + mqmsg);
        if (uploadFile(mqmsg)) {
            System.out.println("MSG WRITE TO FILE ");
        }

        message.setJMSExpiration(1000);

        throw new RuntimeException("-----------1111111111--------------");

    } catch (JMSException e) {
        throw new IllegalArgumentException(e);
    }
}
}
}

我试图通过设置重试次数找到一种方法,但没有这样做。

我想要实现的是,是否可以设置一个参数,以便在尝试 2 次后,将其移至 DLQ?

我已经能够在内置队列中为 JBoss 实现此结果,但不知道如何以及在何处为 IBM MQ 设置参数。

我正在使用以下链接将我的 IBM MQ 作为 docker 映像运行: https://developer.ibm.com/tutorials/mq-connect-app-queue-manager-containers/

我尝试了以下方法但无法使其正常工作:

https://www.ibm.com/support/knowledgecenter/SSFKSJ_7.5.0/com.ibm.mq.dev.doc/q031670_.htm

https://www.ibm.com/support/pages/how-specify-expiration-mq-message-and-when-expired-messages-are-removed-queue

https://www.ibm.com/support/knowledgecenter/en/SSWMAJ_2.0.0/com.ibm.ism.doc/Developing/devjms_poisonmessages.html

有关我的发送程序的更多详细信息,请访问:https://developer.ibm.com/series/badge-ibm-mq-developer-essentials/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。