JMS Sender Example – ActiveMQ

Here is a very small example for an JMS Message Sender:

I will not separately explain it, please check the comments in the code:


/** @author Philipp Boss
 * 04.07.2012
package com.pbo.jms;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

 * @author Philipp Boss
public class JmsSender {

	// Server where activeMQ is running
	static String server = "localhost";

	// that is the JavaNaming and Directory Interface --> service to find names objects in java
	static Context jndiContext = null;

	// this factory creates the connection to the queues
	static ConnectionFactory connectionFactory = null;

	// that is the connection we use for connecting to the queues
	static Connection connection = null;

	// the JMS Session
	static Session session = null;

	// the queue-name to which the message is send
	static String queueName = "testQueue";

	// that is the destination from which we want to read the messages
	static Destination destinationQueue = null;

	// that is the Consumer that receives the messages
	static MessageProducer messageProducer = null;

	 * @param args
	public static void main(String[] args) {

		System.out.println("Destination: " + queueName);

		// Create a JNDI API InitialContext object
		Properties props = new Properties();
		props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
		props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");

		try {
			jndiContext = new InitialContext(props);
			connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
			destinationQueue = (Destination) jndiContext.lookup("dynamicQueues/" + queueName);
		} catch (NamingException e) {
			System.out.println("Could not create JNDI API context or JNDI API lookup failed: " + e.toString());

		// Content of the JMSTextMessage
		String messageContent = "This is the content of the message. you can put here XML, or any other text...";

		try {
			connection = connectionFactory.createConnection();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			messageProducer = session.createProducer(destinationQueue);
			TextMessage message = session.createTextMessage();

			// set Standard JMS Header Fields
			// In activeMQ the JMS Message ID is overwritten by activeMQ...I don't know why....

			// Custom JMS Message properties
			message.setStringProperty("MyExternalUser", "external_philipp");
			message.setIntProperty("MY_INT_NUMBER", 10);

			// set the text to the message
			System.out.println("Sending message: n" + message.getText());
			System.out.println("Message successfully sent");

		} catch (JMSException e) {
			System.out.println("Exception occurred: " + e);
		} finally {
			if (connection != null) {
				try {
				} catch (JMSException e) {