Java JMS 消息传递

发布于 2024-09-03 08:05:33 字数 2680 浏览 2 评论 0原文

我有一个向服务器发送消息以及服务器通过 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示例。有人可以帮助我吗?

I have a working example of sending message to server and server receiving it via qpid messaging. Here is simple hello world to send to server :

http://pastebin.com/M7mSECJn

And here is server which receives requests and sends response(the current client doesn't receive response) :

http://pastebin.com/2mEeuzrV

Here is my property file :

http://pastebin.com/TLEFdpXG

They all work perfectly, I can see the messages in the qpid queue via Qpid JMX management console. These examples are downloaded from https://svn.apache.org/repos/asf/qpid/trunk/qpid/java/client/example (someone may need it also).

I've done Jboss messaging using spring before, but I can't manage to do the same with qpid. With jboss inside applicationsContext I had beans jndiTemplate, conectionFactory, destinationQueue, and jmscontainer like this :

<!-- 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>

and of course sender and listener :

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

Now I'd like to rewrite this qpid example using spring context logic. Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好倦 2024-09-10 08:05:33

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网站上还有关于这个

Spring offers the JmsTemplate class. Check this website out that has an example of how to setup the template (with activemq)

For your specific example try replacing the jmsContainer this:

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

You'll also have to change your code to use the 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);
    }
}

There's also good documentation on the springsource site for this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文