|
Summary: An example of a JMS application working with Spring in GigaSpaces.
OverviewThe GigaSpaces JMS Spring Integration allows users to use the GigaSpaces Messaging Grid with existing JMS-based applications. This section provides an example of a JMS application working with Spring in GigaSpaces. The Sample ApplicationSenderpublic class SenderToQueue { public static void main(String[] args) { final int NUM_MSGS; final String MSG_TEXT = new String("This is a simple message"); if ( (args.length < 1)) { System.out.println("Usage: java SenderToQueue [<number_of_messages>]"); System.exit(1); } ApplicationContext context = new ClassPathXmlApplicationContext("jms_gigaspaces.xml"); //get the Spring JMSTemplate (here we use the JMS 102 template JmsTemplate102 jmsTemplate102 = (JmsTemplate102) context.getBean("jmsQueueTemplate"); if (args.length == 1){ NUM_MSGS = (new Integer(args[0])).intValue(); } else { NUM_MSGS = 1; } for (int i = 0; i < NUM_MSGS; i++) { final String theMessage = MSG_TEXT + " " + (i + 1); System.out.println("Sending message: " + theMessage); jmsTemplate102.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(theMessage); } }); } } } Receiverpublic class SynchQueueReceiver { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("jms_gigaspaces.xml"); //get the Spring JMSTemplate (here we use the JMS 102 template JmsTemplate102 jmsTemplate102 = (JmsTemplate102) context.getBean("jmsQueueTemplate"); while (true) { try{ Message msg = jmsTemplate102.receive(); if (msg instanceof TextMessage) { TextMessage textMessage = (TextMessage) msg; System.out.println("Reading message: " + textMessage.getText() ); } else { // Non-text control message indicates end of messages. break; } }catch(Exception e){ e.printStackTrace(); } } } } Application Context XML – jms_gigaspaces.xmlThis file includes the GigaSpaces JMS properties to inject into org.springframework.jndi.JndiTemplate, org.springframework.jms.core.JmsTemplate102 and org.springframework.jndi.JndiObjectFactoryBean: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial">com.sun.jndi.rmi.registry.RegistryContextFactory</prop> <prop key="java.naming.provider.url">rmi://localhost:10098</prop> </props> </property> </bean> <!-- JMS Queue Template --> <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate102"> <property name="connectionFactory"> <ref bean="jmsQueueConnectionFactory" /> </property> <property name="defaultDestination"> <ref bean="destination" /> </property> <property name="pubSubDomain"> <value>false</value> </property> <property name="receiveTimeout"> <value>20000</value> </property> </bean> <!-- JMS Queue Connection Factory --> <bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiTemplate"> <ref bean="jndiTemplate" /> </property> <property name="jndiName"> <value>GigaSpaces;helloJMSTemplate_container;helloJMSTemplate;GSQueueConnectionFactoryImpl</value> </property> </bean> <bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiTemplate"> <ref bean="jndiTemplate" /> </property> <property name="jndiName"> <value>GigaSpaces;helloJMSTemplate_container;helloJMSTemplate;jms;destinations;MyQueue</value> </property> </bean> </beans> |
Wiki Content Tree
Your Feedback Needed!
We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.
Comments (2)
Aug 15, 2006
Anonymous says:
In the Sender code, it would be more concise to write: jmsTemplate102.send(new...In the Sender code, it would be more concise to write: jmsTemplate102.send(new MessageCreator() {
public Message createMessage(Session session)
throws JMSException
});
As: jmsTemplate102.send(theMessage);By default JmsTemplate uses a SimpleMessageConverter to converts Strings to TextMessage.
Aug 15, 2006
Anonymous says:
Bad formatting. What I meant to say was: In the Sender code, it would be m...Bad formatting. What I meant to say was:
In the Sender code, it would be more concise to change:
To:
convertAndSend uses a SimpleMessageCreator which will automatically convert a String to a TextMessage
Add Comment