Java JMS消息传递实现
我一直在为这项“简单”的任务苦苦挣扎,因为我已经被困了两天,现在需要帮助。我已经改变了无数次周围的事情,最后我偶然发现了 我想做的是,发送消息并接收它。我也在读第8章关于信息的内容。它很好地解释了两种类型的消息传递,有一个很好的示例用于Java JMS消息传递实现,java,spring,jboss,jms,Java,Spring,Jboss,Jms,我一直在为这项“简单”的任务苦苦挣扎,因为我已经被困了两天,现在需要帮助。我已经改变了无数次周围的事情,最后我偶然发现了 我想做的是,发送消息并接收它。我也在读第8章关于信息的内容。它很好地解释了两种类型的消息传递,有一个很好的示例用于发布和订阅类型,但现在是点对点消息传递的示例(这是我需要的一种) 我可以自己向队列发送消息,但不知道如何接收。这就是我尝试使用本春季教程的原因。到目前为止,我得到的是: 重新编辑的发件人: package quartz.spring.com.example;
发布和订阅
类型,但现在是点对点
消息传递的示例(这是我需要的一种)
我可以自己向队列发送消息,但不知道如何接收。这就是我尝试使用本春季教程的原因。到目前为止,我得到的是:
重新编辑的发件人:
package quartz.spring.com.example;
import java.util.HashMap;
import java.util.Map;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jms.core.MessagePostProcessor;
public class JmsQueueSender {
private JmsTemplate jmsTemplate;
private Destination destination;
public void setConnectionFactory(ConnectionFactory cf) {
this.jmsTemplate = new JmsTemplate102(cf, false);
}
public void setQueue(Queue queue) {
this.destination = queue;
}
public void simpleSend() {
this.jmsTemplate.send(this.destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("hello queue world");
}
});
}
public void sendWithConversion() {
Map map = new HashMap();
map.put("Name", "Mark");
map.put("Age", new Integer(47));
jmsTemplate.convertAndSend("ReceiverQueue", map, new MessagePostProcessor() {
public Message postProcessMessage(Message message) throws JMSException {
message.setIntProperty("AccountID", 1234);
message.setJMSCorrelationID("123-00001");
return message;
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<bean id="sender" class="quartz.spring.com.example.JmsQueueSender"
init-method="sendWithConversion" />
<bean id="receiver" class="quartz.spring.com.example.ExampleListener">
</bean>
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="messageListener" />
</bean>
<!-- Queue configuration -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
<prop key="java.naming.provider.url">jnp://localhost:1099</prop>
<prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
<prop key="java.naming.security.principal">admin</prop>
<prop key="java.naming.security.credentials">admin</prop>
</props>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="ConnectionFactory" />
</bean>
<bean id="queueDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName">
<value>queue/ReceiverQueue</value>
</property>
</bean>
</beans>
接收器:
package quartz.spring.com.example;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class ExampleListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
System.out.println(((TextMessage) message).getText());
}
catch (JMSException ex) {
throw new RuntimeException(ex);
}
}
else {
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}
}
重新编辑的applicationcontext.xml
package quartz.spring.com.example;
import java.util.HashMap;
import java.util.Map;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jms.core.MessagePostProcessor;
public class JmsQueueSender {
private JmsTemplate jmsTemplate;
private Destination destination;
public void setConnectionFactory(ConnectionFactory cf) {
this.jmsTemplate = new JmsTemplate102(cf, false);
}
public void setQueue(Queue queue) {
this.destination = queue;
}
public void simpleSend() {
this.jmsTemplate.send(this.destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("hello queue world");
}
});
}
public void sendWithConversion() {
Map map = new HashMap();
map.put("Name", "Mark");
map.put("Age", new Integer(47));
jmsTemplate.convertAndSend("ReceiverQueue", map, new MessagePostProcessor() {
public Message postProcessMessage(Message message) throws JMSException {
message.setIntProperty("AccountID", 1234);
message.setJMSCorrelationID("123-00001");
return message;
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<bean id="sender" class="quartz.spring.com.example.JmsQueueSender"
init-method="sendWithConversion" />
<bean id="receiver" class="quartz.spring.com.example.ExampleListener">
</bean>
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="messageListener" />
</bean>
<!-- Queue configuration -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
<prop key="java.naming.provider.url">jnp://localhost:1099</prop>
<prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
<prop key="java.naming.security.principal">admin</prop>
<prop key="java.naming.security.credentials">admin</prop>
</props>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="ConnectionFactory" />
</bean>
<bean id="queueDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName">
<value>queue/ReceiverQueue</value>
</property>
</bean>
</beans>
完成此操作后,将在
MessageConsumer使您能够
查询目的地是否有消息或消息
注册邮件通知
有人能告诉我正确的方向吗?有没有详细说明如何从队列接收消息的教程?我有有效的发送消息代码,没有在这里发布,因为这篇文章太长了。
编辑:
package quartz.spring.com.example;
import java.util.HashMap;
import java.util.Map;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.JmsTemplate102;
import org.springframework.jms.core.MessagePostProcessor;
public class JmsQueueSender {
private JmsTemplate jmsTemplate;
private Destination destination;
public void setConnectionFactory(ConnectionFactory cf) {
this.jmsTemplate = new JmsTemplate102(cf, false);
}
public void setQueue(Queue queue) {
this.destination = queue;
}
public void simpleSend() {
this.jmsTemplate.send(this.destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("hello queue world");
}
});
}
public void sendWithConversion() {
Map map = new HashMap();
map.put("Name", "Mark");
map.put("Age", new Integer(47));
jmsTemplate.convertAndSend("ReceiverQueue", map, new MessagePostProcessor() {
public Message postProcessMessage(Message message) throws JMSException {
message.setIntProperty("AccountID", 1234);
message.setJMSCorrelationID("123-00001");
return message;
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<bean id="sender" class="quartz.spring.com.example.JmsQueueSender"
init-method="sendWithConversion" />
<bean id="receiver" class="quartz.spring.com.example.ExampleListener">
</bean>
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="messageListener" />
</bean>
<!-- Queue configuration -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
<prop key="java.naming.provider.url">jnp://localhost:1099</prop>
<prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
<prop key="java.naming.security.principal">admin</prop>
<prop key="java.naming.security.credentials">admin</prop>
</props>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="ConnectionFactory" />
</bean>
<bean id="queueDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName">
<value>queue/ReceiverQueue</value>
</property>
</bean>
</beans>
我在我的jboss messaging destination-service.xml中添加了以下Mbean:
<mbean code="org.jboss.jms.server.destination.QueueService"
name="jboss.messaging.destination:service=Queue,name=ReceiverQueue"
xmbean-dd="xmdesc/Queue-xmbean.xml">
<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
<depends>jboss.messaging:service=PostOffice</depends>
</mbean>
消息传递:service=ServerPeer
jboss.messaging:service=邮局
从您忘记的Spring示例URL:
<!-- and this is the message listener container -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="destination"/>
<property name="messageListener" ref="messageListener" />
</bean>
及
第一个问题是我认为行jmsTemplate.convertAndSend
上存在nullPointerException。您的jmsTemplate尚未初始化
我相信这是因为init方法不是convertAndSend。您根本不需要init方法。您应该在applicationcontext.xml中设置以下属性:
<bean id="sender" class="quartz.spring.com.example.JmsQueueSender">
<property name="queue" value="theNameOfYourQueue"> <!-- or in stead of value ref to a String which contains the shared queue name -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
这将修复发送时的错误(顺便问一下,为什么使用JMSTemplate102而不是JMSTemplate?)
另一个问题是,通过设置bean的属性来配置队列名称。在这种情况下,您似乎正在侦听queueDestination队列/ReceiverQueue,因为您的jmsContainer被配置为处理侦听器对该队列的调用
在applicationcontext.xml中定义了messageListener bean
如果你在某处使用
ref=“someName”
也应该有一个我以为你在做PTP消息传递,但你在做的是异步消息传递。我想实现点对点,但显然我没有做到。你现在似乎有一个名为receiver的bean和一个名为messageListener的bean,侦听器:)和发送方testQueue中的队列名称似乎不是您正在侦听的对象。@extraneon确定,我更新了代码appcontext.xml和发送方。。但我仍然得到了这个错误:org.springframework.beans.factory.beancreatitionexception:创建名为“sender”的bean时出错,该bean在ServletContext资源[/WEB-INF/conf/applicationContext.xml]中定义:调用init方法失败;嵌套异常是java.lang.NullPointerException
示例侦听器如何知道要侦听哪个队列,我没有指定,也不知道如何侦听。@extraneon这不是我忘记的唯一一件事,显然还有一些要添加,我更新了我的发件人、appcontext并将mbean添加到目标服务中