Tag Archives: Active MQ

ActiveMQ – My First Example

Hello and welcome after a very long time !!

There are lots of things that I want to post but as of now, would post my first example code with active mq.

Active MQ – This is a open source message broker with full JMS implementation.

You can please read more details from : http://activemq.apache.org/

First step : Download and unzip apache-activemq-5.11.1-bin binary files to a drive.

Second step: Installation
————–
Run the ActiveMQ batch inside the win32 folder.
http://localhost:8161/
Testing the installation
————————
Active MQ default port is 61616

From cmd prompt

netstat -an|find “61616”

Monitoring ActiveMQ
——————-
The default username and password is admin/admin.
You can configure this in the conf/jetty-real.properties file.
Stopping ActiveMQ
——————
Type Ctrl-C at the cmd prompt.

Now open Eclipse and create a Message Producer, Message consumer and a main class. Complete codes are posted here. These are taken from the active mq website and can be modified accordingly.

Add activemq-all-5.4.3.jar in the class path.

HelloProducer.java

========================================================================================================================================================

package com.Active_MQ;

import java.awt.font.TextMeasurer;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class HelloProducer implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
try {
//Creating connection factory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(“tcp://localhost:61616”);

//ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_URL);

//Create connection
Connection connection = connectionFactory.createConnection();
connection.start();

//Create session
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

//Create destination
Destination destination = session.createQueue(“TEST.FOO”);

MessageProducer producer = session.createProducer(destination);

producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

//Create a message

String text = “Hello world from ” + Thread.currentThread().getName()+” : ” + this.hashCode();
TextMessage message = session.createTextMessage(text);

//Tell the producer to send the message

System.out.println(“Sent message ” + message.hashCode() + ” : ” + Thread.currentThread().getName());

producer.send(message);

//Clean up

session.close();
connection.close();

} catch (Exception e) {
e.printStackTrace();
}
}

}

============================================================================
============================================================================

HelloConsumer.java

========================================================================================================================================================

package com.Active_MQ;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
public class HelloConsumer implements Runnable{
public void run()
{
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(“tcp://localhost:61616”);

Connection connection = connectionFactory.createConnection();

connection.start();
//connection.setExceptionListener((ExceptionListener) this);

Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

Destination destination = session.createQueue(“TEST.FOO”);

MessageConsumer consumer = session.createConsumer(destination);

Message message = consumer.receive(1000);

if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println(“Received “+text);

}
else
{
System.out.println(“Received “+message);
}

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

} catch (Exception e) {
e.printStackTrace();
}
}

}

========================================================================================================================================================

App.java

========================================================================================================================================================

package com.Active_MQ;

public class App {
public static void main(String[] args) {
try {
thread(new HelloProducer(),false);
thread(new HelloProducer(),false);
thread(new HelloConsumer(),false);
Thread.sleep(1000);
thread(new HelloConsumer(), false);
thread(new HelloProducer(), false);
thread(new HelloConsumer(), false);
thread(new HelloProducer(), false);
Thread.sleep(1000);
thread(new HelloConsumer(), false);
thread(new HelloProducer(), false);
thread(new HelloConsumer(), false);
thread(new HelloConsumer(), false);
thread(new HelloProducer(), false);
thread(new HelloProducer(), false);
Thread.sleep(1000);
thread(new HelloProducer(), false);
thread(new HelloConsumer(), false);
thread(new HelloConsumer(), false);
thread(new HelloProducer(), false);
thread(new HelloConsumer(), false);
thread(new HelloProducer(), false);
thread(new HelloConsumer(), false);
thread(new HelloProducer(), false);
thread(new HelloConsumer(), false);
thread(new HelloConsumer(), false);
thread(new HelloProducer(), false);
} catch (Exception e) {
e.printStackTrace();
}

}

public static void thread(Runnable runnable,boolean deamon)
{
Thread brokerThread = new Thread(runnable);
brokerThread.setDaemon(deamon);
brokerThread.start();
}

}

========================================================================================================================================================

Now when we run the main class output shows as :

========================================================================================================================================================

Sent message 700549 : Thread-1
Sent message 7702079 : Thread-0
Received Hello world from Thread-0 : 10859927
Sent message 14397555 : Thread-10
Received Hello world from Thread-1 : 18709978
Sent message 12968500 : Thread-12
Received Hello world from Thread-10 : 28432383
Sent message 12606869 : Thread-20
Sent message 29596937 : Thread-24
Sent message 32819228 : Thread-25
Received Hello world from Thread-12 : 19520119
Received Hello world from Thread-20 : 17982827
Received Hello world from Thread-25 : 24829876
Sent message 10565194 : Thread-43
Sent message 32734483 : Thread-33
Received Hello world from Thread-24 : 23815485
Sent message 25663554 : Thread-40
Sent message 19368064 : Thread-46
Sent message 26051994 : Thread-36
Received Hello world from Thread-33 : 32729846
Received Hello world from Thread-46 : 3333237
Received Hello world from Thread-43 : 3117096
Received Hello world from Thread-40 : 11203640
Received Hello world from Thread-36 : 7522198

========================================================================================================================================================

When I open my web console then I can see queue TEST.FOO is created and messages are enqueued and dequeued.

I am trying to test some more examples with Active MQ and will keep posting the same.

Till then, Happy coding !! 🙂