Using amq namespace for building Spring JMS application for ActiveMQ

Using amq namespace makes developing Spring application for ActiveMQ very easy, i wanted to try that so i built this sample application, This is how my applicationContext.xml file looks like

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:jms="http://www.springframework.org/schema/jms"
  xmlns:amq="http://activemq.apache.org/schema/core"
  xsi:schemaLocation="http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <amq:connectionFactory id="connectionFactory"
    brokerURL="tcp://localhost:61616" />

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

</beans>
As you can see i have only two beans one for ConnectionFactory and other for JmsTemplate. This is how my MessagePublisher.java looks like

package com.webspherenotes.jms;

import java.util.Date;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class MessagePublisher {
  public static void main(String[] args)throws Exception {
    ApplicationContext context = 
 new ClassPathXmlApplicationContext("applicationContext.xml");
    JmsTemplate jmsTemplate =(JmsTemplate) context.getBean("jmsTemplate");
    MessageCreator message = new MessageCreator() {
      public Message createMessage(Session session) throws JMSException {
        TextMessage textMessage = session.createTextMessage();
        String messageStr = "This message is sent using MessageCreator" + new Date();
        textMessage.setText(messageStr);
        return textMessage;
      }
    };
    jmsTemplate.send(message);
  }
}
This is how my MessageReceiver class looks like

package com.webspherenotes.jms;

import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

public class MessageReceiver {
  public static void main(String[] args)throws Exception {
    ApplicationContext context = 
    new ClassPathXmlApplicationContext("applicationContext.xml");
    JmsTemplate jmsTemplate =(JmsTemplate) context.getBean("jmsTemplate");
    TextMessage message = (TextMessage)jmsTemplate.receive();
    System.out.println("Message received " + message.getText());
  }
}
This is sample of how to wait for message synchronously.

No comments: