Spring JMSTemplate - 丢失消息

时间:2013-03-12 10:46:51

标签: jms activemq messaging spring-jms jmstemplate

我们正在使用Spring JMSTemplate 3.0.1RELEASE将JMS消息发送到ActiveMQ集群。

我正在使用useAsynSend = true来发送异步请求,因为这些都是Fire和Forget。但是它们仍然是持久性的,我可以确认它们首先在AMQ Kaha-DB上保留,然后转发给Message Listeners。没有CorelationID或JMSReplyTo,因为我没有收听任何回复。

   <amq:connectionFactory id="amqJmsFactory"
    brokerURL="failover:(tcp://MY-SERVER-01:61616,tcp://MY-SERVER-02:61616)?randomize=true&jms.useAsyncSend=true" />

<!-- JMS Producer Configuration -->
<bean id="jmsProducerTemplate" class="org.springframework.jms.core.JmsTemplate"
    p:connectionFactory-ref="amqJmsFactory" />

  <bean id="activeMQBinding" class="com.my.company.activemq.ActiveMQProductBinding">
    <property name="template" ref="jmsProducerTemplate" />
</bean>

在ActiveMQProductBinding类中,我们有以下方法

  public void sendActiveMQMessageAsync(final Object message, String qName) {
    log.info("About to send message");
    template.send(qName, new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
            ObjectMessage objectMessage = session.createObjectMessage((Serializable) message);

            log.info("Sending message: " + objectMessage);

            return objectMessage;
        }
    });
}

现在我可以在日志中看到日志正在打印。没有异常被抛出。还有一些消息完全迷失了。它可能没有达到ActiveMQ。我将Mule JMS Listener配置为侦听上面'qName'定义的ActiveMQ队列上的消息。大多数消息都可以很好地传递到AMQ,Mule会在几分之一秒内完成。然而,只有一些消息迷失了。当我检查ActiveMQ上的队列时,我可以看到收到的所有消息都已被发送到Mule。因此,我怀疑消息根本没有到达ActiveMQ,或者AMQ拒绝。但是没有登录JMSTemplate spring或ActiveMQ。

创建的消息看起来像这样(它在上面的方法中打印)。

   2013-03-11 16:33:11,752 INFO [com.my.company.activemq.ActiveMQProductBinding$1] Sending message: ActiveMQObjectMessage {commandId = 0, responseRequired = false, messageId = null, originalDestination = null, originalTransactionId = null, producerId = null, destination = null, transactionId = null, expiration = 0, timestamp = 0, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = false, type = null, priority = 0, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@61408493, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = false, readOnlyBody = false, droppable = false}

我在JMSTemplate配置上丢失的任何内容或AMQ上的某些行为?请帮忙!

任何帮助人?

1 个答案:

答案 0 :(得分:1)

2我能想到的可能原因。 1)我们使用错误的连接工厂。我们应该使用Pooled Connection Factory和JMSTemplate来发送消息。 org.apache.activemq.pool.PooledConnectionFactory

2)我们正在使用useAsyncSend = true - 即发送方发送消息并且不等待确认,并且存在消息丢失的可能性。这似乎更有可能但不确定。

仍然不接受这个答案,因为有人可能会有更具体的解释。与此同时,如果有人偶然发现这个问题可能会受益于此问题。