【问题标题】:Java JMS MessagingJava JMS 消息传递
【发布时间】:2011-02-26 05:57:56
【问题描述】:

我有一个将消息发送到服务器和服务器通过 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 示例。谁能帮帮我?

【问题讨论】:

    标签: java spring jboss messaging amqp


    【解决方案1】:

    Spring 提供了 JmsTemplate 类。查看this 网站,其中有一个如何设置模板的示例(使用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 网站上也有很好的文档this

    【讨论】:

    • 抱歉,我无法正确配置 spring appcontext 能否请您为您的示例发布其他配置 bean,它不一定适用于我,所以我可以看到示例然后在我的实现中实现
    猜你喜欢
    • 2011-01-29
    • 2016-02-23
    • 1970-01-01
    • 2019-07-18
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多