JMS in Spring

If you used JMS before, you knew the drill. Before we could send/receive a message, we need create get a connection factory, then create a connection, start a connection, create a session, create a queue or topic, create a producer/consumer....and after that, we need clean up the connection and session.

Now enter Spring

All we need is inject a JmsTemplate and Destination to producer/consumer bean.

Here is how to send a message in producer bean.
jmsTemplate.send(destination,
    new MessageCreator()
    {
     public Message createMessage(Session session) throws JMSException
     {
      TextMessage message = session.createTextMessage("Hello, World!");
      return message;
     }
    });

Here is how to receive a message in consumer bean.
Message message = (Message) jmsTemplate.receive(destination);
Here is the configuration for JmsTemplate and Destination
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
     <property name="brokerURL" value="tcp://localhost:61616"/>
</bean>

<bean id="queue_destination" class="org.apache.activemq.command.ActiveMQQueue">
     <constructor-arg index="0" value="BolunQueue"/>
</bean>
You are welcome!