8

我有一个将消息发送到服务器和服务器通过 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 做同样的事情。在 applicationsContext 中使用 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>

当然还有发送者和监听者:

 <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" />

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

4

1 回答 1

1

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

对于您的具体示例,请尝试替换jmsContainer此:

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  <property name="connectionFactory" ref="connectionFactory" />
</bean>

您还必须更改代码以使用 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);
    }
}

springsource 网站上也有很好的文档

于 2010-06-03T10:54:08.730 回答