【问题标题】:JMS QUEUE: how to store message in the queue until flag is set to trueJMS QUEUE:如何将消息存储在队列中,直到标志设置为 true
【发布时间】:2013-03-29 20:27:48
【问题描述】:

我正在尝试 JMS 队列的示例应用程序。我希望进入队列的消息一直留在那里,直到我的标志设置为 true。我正在使用 spring 框架和具有以下配置的 MDP 侦听器:

服务器上下文.xml:

<bean id="MDPListener" class="controller.MDPListener"></bean>

<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL">
        <value>tcp://localhost:61616</value>
        </property>
</bean>

<bean id="dataQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg value="dataQueue15"></constructor-arg>

</bean>

<bean id="container" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="messageListener" ref="MDPListener"/>
        <property name="destination" ref="dataQueue"/>
        <property name="sessionTransacted" value="true"/>



</bean>

我的 onMessage 代码如下:

public void onMessage(Message message,Session session) {        

        System.out.println("The session: "+session.toString());
        System.out.println("New Message arrived part2 .. Passing to Controller");

        Boolean g=false;

        if(g==true)
        {
            System.out.println("Data true..session committed!!");
        }
        else
        {

            System.out.println("in the queue");
            throw new RuntimeException("Saved");
        }
}

现在,当抛出异常时,消息会保留在队列中,并且控件会返回到同一个侦听器,该侦听器会重新侦听相同的消息并在一段时间后停止。这会导致死队列。我无法存储该消息。我希望我的听众收听队列,但不是上一条消息,而是下一条消息。请帮忙!

【问题讨论】:

  • 我相信 JMS 队列是非常糟糕的存储东西的地方。如果您不希望您的消息被传递 - 只是不要将其发布到队列中。

标签: java jms spring-jms


【解决方案1】:

JMS 不能以这种方式工作 - 您必须将消息移动到代码中的另一个队列。

或者,使用 ActiveMQ,您可以配置重新传递策略以更快地将消息发送到 DLQ(我相信默认是 6 次重试)。

【讨论】:

  • 感谢上帝!最后给出一个简单而中肯的答案!脱帽! :D 现在为我工作 .. 重新交付政策是关键
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 2020-06-03
  • 2016-05-02
  • 1970-01-01
  • 2015-07-13
  • 2015-02-25
  • 2014-02-27
相关资源
最近更新 更多