Java JMS消息传递

Lon*_*don 8 java messaging jboss spring amqp

我有一个工作示例,通过qpid消息发送消息到服务器和服务器接收它.这是发送到服务器的简单hello world:

http://pastebin.com/M7mSECJn

这里是接收请求并发送响应的服务器(当前客户端没有收到响应):

http://pastebin.com/2mEeuzrV

这是我的属性文件:

http://pastebin.com/TLEFdpXG

它们都工作得很好,我可以通过Qpid JMX管理控制台看到qpid队列中的消息.这些示例从https://svn.apache.org/repos/asf/qpid/trunk/qpid/java/client/example下载(有人也可能需要它).

我之前使用spring完成了Jboss消息传递,但是我无法用qpid做同样的事情.使用appsContext中的jboss,我有bean jndiTemplate,conectionFactory,destinationQueue和jmscontainer,如下所示:

<!-- 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/testQueue</value>
  </property>
 </bean>

  <bean id="jmsContainer"
  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  <property name="connectionFactory" ref="connectionFactory" />
  <property name="destination" ref="queueDestination" />
  <property name="messageListener" ref="listener" />
 </bean>
Run Code Online (Sandbox Code Playgroud)

当然是发件人和听众:

 <bean id="sender" class="com.practice.Sender">
  <property name="connectionFactory" ref="connectionFactory" />
  <property name="queueDestination" ref="queueDestination" />
 </bean>


 <bean id="listener" class="com.practice.MsgListener" />
Run Code Online (Sandbox Code Playgroud)

现在我想用spring上下文逻辑重写这个qpid示例.谁能帮我?

Mar*_*ope 1

Spring 提供了 JmsTemplate 类。查看网站,其中包含如何设置模板(使用 activemq)的示例

对于您的具体示例,请尝试替换jmsContainer以下内容:

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  <property name="connectionFactory" ref="connectionFactory" />
</bean>
Run Code Online (Sandbox Code Playgroud)

您还必须更改代码才能使用 spring JmsTemplate:

public class MessageSender  {

    private Destination destination;
    private JmsTemplate jmsTemplate;

    public void setJmsTemplate(JmsTemplate jmsTemplate)  {
        this.jmsTemplate = jmsTemplate;
    }

    public void setDestination(Destination destination) {
        this.destination = destination;
    }

    public void sendMessage() { 
        MessageCreator creator = new MessageCreator() {
            public Message createMessage(Session session) {
                TextMessage message = null;
                try  {
                    message = session.createTextMessage();
                    message.setStringProperty("text", "Hello, World!");
                }
                catch (JMSException e) {
                    e.printStackTrace();
                }
                return message;
            }
        };  
    jmsTemplate.send(destination, creator);
    }
}
Run Code Online (Sandbox Code Playgroud)

springsource网站上也有很好的文档