Monday, November 7, 2011

Message Queue - ActiveMQ












MQ = Message Queues --> IBM
ActiveMQ = Apache Message Queue --> Apache

MQ series is a tool where we can post messages and when ever consumer wants that message , that consumer can consume that message.

Here i am explaining with real time example where the Java End user who wants to get java tip daily to his mobile , they can register to this java program and get the TIPS daily

Here is the outlook of the program
----------------------------------


As explained in the above diagram, Producer will post the messages to Active MQ , which keeps all messages from the producer.

When ever consumer comes to get the message that would be released from the MQ.
Java Classes
------------
1)ActiveMQProducer.java ( which reads java tips and produce in to MQ )

Create Connection to Active MQ
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start(); //create connection to the Active MQ

Create session

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

session create Queue/Topics

Destination destination = session.createQueue();

Create Producer
MessageProducer producer = session.createProducer(destination);

Send Message to Producer
TextMessage message = session.createTextMessage(messageMap.get();
producer.send(message);

2)ActiveMQConsumer.java (which pull the messages from MQ and publish)

Create Connection to MQ
ConnectionFactory factory = new ActiveMQConnectionFactory(url);
Connection connection = factory.createConnection();
connection.start();

Create Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Create Queue/Topics
Destination destination = session.createQueue(nonDeliveredKey);

Create Consumer and receive

MessageConsumer consumer = session.createConsumer(destination);
Message message = consumer.receive();

3)ReadMessageAndMobile.java
which reads java messages from the property files and also reads the mobile number of the users and start posting the messages to the end user

public static Map readTips()
public static Map readMobileNumbers()
public static void writeOnlyKeys(Map messageKeysMap)
TIPNumber as key and TIP is value
public static String readNonDeliveredKey()
If one time consumer pull and post the messages mark that key as "Delivered" , then while reading the file again read only Non-Delivered keys
public static void changeDeliveredStatus(String str)
Once message is delivered change that particular message key "Delivered" So that next time same key will not read

javaTips.properties

Tip1 = Java programming language developed by Sun microsystems in 1991.
Tip2 = The major components of JVM are Class Loader,Executiion Engine,Just In Time(JIT) compiler.
Tip3 = The relationship b/w objects of superclass and subclass is an is - a relationship
Tip4 = When a class consist of another class as its elements it is called Has a relationship
Tip5 = A static field means that its data is stored in one location in memory, no matter how many objects
Tip6 = Access specifiers in java Public,Private,Protected and friendly(default).


MobileNumbers.properties
96xxxxxxxx = Suneel
77xxxxxxxx = Kumar

UserCredentials.properties
Username = xxxxxxxx
Password = xxxxxxxx

Before running the MQ application you have to install Active MQ server


start the application "activemq.bat", server starts up
user the url
http://localhost:8161/admin or http://0.0.0.0:8161/admin


After running the application output looks like this
----------------------------------------------------
First there will no messages in the Queue



After running the Producer program
----------------------------------------------------

all Message keys will be in Not Delivered status

#only keys and status on message deliveries
#Mon Nov 07 18:27:54 IST 2011
Tip13=Not Delivered
Tip11=Not Delivered
Tip10=Not Delivered
TIp17=Not Delivered
Tip9=Not Delivered
Tip8=Not Delivered
Tip7=Not Delivered
TIP12=Not Delivered
Tip6=Not Delivered
Tip5=Not Delivered
Tip4=Not Delivered
Tip3=Not Delivered
Tip2=Not Delivered
Tip1=Not Delivered
Tip18=Not Delivered
Tip16=Not Delivered
Tip15=Not Delivered
Tip14=Not Delivered




After running the Consumer program
-----------------------------------
output
-----
Nov 7, 2011 6:33:49 PM org.apache.activemq.transport.failover.FailoverTransport doReconnect
INFO: Successfully connected to tcp://localhost:61616
*************::Not Delivered
textMessage received :A session is a group of activities that are performed by user while accessing website in between logging in logging out


Messagekeys
-----------
#updated with the new values
#Mon Nov 07 18:33:49 IST 2011
Tip13=Delivered
Tip11=Not Delivered
Tip10=Not Delivered
TIp17=Not Delivered
Tip9=Not Delivered
Tip8=Not Delivered
Tip7=Not Delivered
TIP12=Not Delivered
Tip6=Not Delivered
Tip5=Not Delivered
Tip4=Not Delivered
Tip3=Not Delivered
Tip2=Not Delivered
Tip1=Not Delivered
Tip18=Not Delivered
Tip16=Not Delivered
Tip15=Not Delivered
Tip14=Not Delivered


Java code - download
----------
ActiveMQProducer.java  -- click here to download
ActiveMQConsumer.java -- click here to download
ReadMessageAndMobile.java  -- click here to download
ActiveMQ jar file - click here to download