通过 Spring 集成入站适配器接收 JMS 消息随机失败
Posted
技术标签:
【中文标题】通过 Spring 集成入站适配器接收 JMS 消息随机失败【英文标题】:Receiving JMS messages via Spring integration inbound adapter randomly fails 【发布时间】:2012-08-05 18:23:54 【问题描述】:我是这个 Spring Integration 和 JMS 的新手,我开始使用它。在这里,我想通过 activemq 创建普通的 jms 消息,并通过 spring 入站适配器(消息驱动)接收它。
下面是我的spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd>
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<!-- jms beans -->
<beans:bean id="jms.msgQueue" class="org.apache.activemq.command.ActiveMQQueue">
<beans:constructor-arg value="MSG_QUEUE" />
</beans:bean>
<beans:bean name="jms.connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<beans:property name="brokerURL" value="tcp://localhost:61616" />
</beans:bean>
<!-- spring integration beans -->
<channel id="channels.jms.allMessages">
<queue capacity="1000" />
</channel>
<jms:message-driven-channel-adapter id="adapters.jms.msgAdapter"
connection-factory="jms.connectionFactory"
destination="jms.msgQueue"
channel="channels.jms.allMessages" />
这是我的测试课
package com.bst.jms;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
public class TestActiveMQ
public static void main(String[] args)
try
AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");
ConnectionFactory connectionFactory = (ConnectionFactory)context.getBean("jms.connectionFactory");
Destination destination = (Destination)context.getBean("jms.msgQueue");
PollableChannel msgChannel = (PollableChannel) context.getBean("channels.jms.allMessages", PollableChannel.class );
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
TextMessage textMessage = session.createTextMessage();
textMessage.setText("Message from JMS");
producer.send(textMessage);
System.out.println("--------------- Message Sending ------------------------");
Message<?> received = msgChannel.receive();
String payload = (String) received.getPayload();
System.out.println("Receving message = " + payload);
catch(JMSException ex)
System.out.println("----------- JMS Exception --------------");
但问题是我不能保证交货。有时程序无法接收到消息,而在某些情况下它会成功,但会出现一些警告,例如
Setup of JMS message listener invoker failed for destination 'queue://MSG_QUEUE' - trying to recover. Cause: Connection reset
Could not refresh JMS Connection for destination 'queue://MSG_QUEUE' - retrying in 5000 ms. Cause: Could not connect to broker URL: tcp://localhost:61616. Reason: java.net.ConnectException: Connection refused: connect
无法刷新目标“queue://MSG_QUEUE”的 JMS 连接 - 5000 毫秒后重试。原因:无法连接到代理 URL:tcp://localhost:61616。原因:java.net.ConnectException:连接被拒绝:连接
这会在成功之前发生几次。
你们对此有什么想法吗?
感谢您的帮助。
谢谢, 凯思
【问题讨论】:
【参考方案1】:这只是意味着当侦听器容器启动时代理没有运行。使用 tcp:// URL 时,您应该在创建此上下文之前在其自己的上下文(或另一个 JVM)中运行代理。
【讨论】:
【参考方案2】:我已经在我的 STS 中测试了这些代码,它工作正常。
你这边唯一的问题是,首先启动消息代理(比如 ActiveMQ)然后运行你的项目,你可以得到你需要的输出。
谢谢。
【讨论】:
以上是关于通过 Spring 集成入站适配器接收 JMS 消息随机失败的主要内容,如果未能解决你的问题,请参考以下文章
Spring集成+文件读取消息源_入站通道适配器+出站网关。