Snippets‎ > ‎Java Examples‎ > ‎

Hello World Java Message Service with Open MQ

BY MARKUS SPRUNCK


This short article describes a minimal JMS application to send and receive messages with Open MQ.


To run the example code you need a running Open MQ installation. Just download from Open MQ Downloads and run the installation. 

After the installation start  <yourpath>\openmq\mq\bin\imqbrokerd.exe and you should see something like this: 

Example Code

File HelloProducer.java - creates a message and send it to the queue:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.sw_engineering_candies.example;

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;

public class HelloProducer {

	public static void sendMessage() {

		try {

			// Start connection
			ConnectionFactory cf = new com.sun.messaging.ConnectionFactory();
			Connection connection = cf.createConnection();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createTopic("HelloWorld");
			MessageProducer producer = session.createProducer(destination);
			connection.start();

			// create message to send
			TextMessage message = session.createTextMessage();
			message.setText("Hello World (" + System.currentTimeMillis() 
                                + ") from HelloProducer.java");

			System.out.println("Send from HelloProducer.java");
			producer.send(message);

			// close everything
			producer.close();
			session.close();
			connection.close();

		} catch (JMSException ex) {
			System.out.println("Error = " + ex.getMessage());
		}
	}

	public static void main(String args[]) {
		HelloProducer.sendMessage();
	}
}

File HelloConsumer.java - reads a message from the queue:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.sw_engineering_candies.example;


import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

public class HelloConsumer {

	public static void consumeMessage() {
		try {

			// creating a connection
			ConnectionFactory cf = new com.sun.messaging.ConnectionFactory();
			Connection connection = cf.createConnection();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createTopic("HelloWorld");
			MessageConsumer consumer = session.createConsumer(destination);
			connection.start();

			System.out.println("Start listen HelloWorld Queue from HelloConsumer.java");
			long now = System.currentTimeMillis();
			do {
				// receive our message
				TextMessage m = (TextMessage) consumer.receive();
				System.out.println(m.getText() + " timestamp=" + m.getJMSTimestamp());

			} while (now + 1000 * 60 * 10 > System.currentTimeMillis());
			System.out.println("End listen HelloWorld Queue from HelloConsumer.java");

			// close everything
			session.close();
			connection.close();
			consumer.close();

		} catch (JMSException ex) {
			System.out.println("Error = " + ex.getMessage());
		}
	}

	public static void main(String args[]) {
		HelloConsumer.consumeMessage();
	}
}

Expected Result

First start the HelloConsumer.java application and then HelloProducer.java.