Java JMS Messaging
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/exa开发者_高级运维mple (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?
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
精彩评论