Tuesday, March 24, 2009

Introduction to Message-oriented Middleware (MOM) and Java Messaging Service (JMS) Using Apache ActiveMQ


package com.ndung;

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class HelloWorldProducer {
public static void main(String[] args) throws JMSException {
//Instantiate connection factory, specific to the vendor
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.MQ");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
// Create a messages
String text = "Hello world! From: ndung";
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Message Sent");
producer.send(message);
// Clean up
session.close();
connection.close();
}
}


package com.ndung;

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class HelloWorldConsumer {
public static void main(String[] args) throws JMSException {
//Instantiate connection factory, specific to the vendor
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.MQ");
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
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();
}
}

In the previous post, we have been introduced into EAI (Enterprise Application Integration) and a flash on Middleware. There are two fundamentally different types of middleware based on the approach used by the middleware to transfer the data between the distributed software applications. They are Remote Procedure Calls (RPC) based middleware and Message-oriented Middleware (MOM).
1. RPC. Software application that uses RPC based middleware to transfer data to another software application has to wait until the latter application is done processing the data. Thus, with this type of middleware, the communication proceeds in a lock step, synchronized manner, and the communicating processes are tightly coupled to one another. Examples of such middleware include Java RMI, CORBA, etc.
2. MOM. Is best described as a category of software for communication in an loosely-coupled, reliable, scalable, enabled asynchronous communication amongst distributed applications or systems.


JMS is a spesification that defines a set of interfaces and associated semantics, which allow applications written in Java to access the services of any JMS compliant Message MOM product. There are plenty of compliant MOM products available in market, including MQSeries from IBM, SonicMQ from Progress, Sun Java Message Queue, even Apache ActiveMQ, and many more.
The players in JMS:
1. Connections and Connection Factories
2. Sessions
3. Destinations
We should try first JMS First Impression using ActiveMQ in here. I'm using Netbeans 6.5 as IDE and prepare library: activemq-all-5.2.0.jar and jms.jar.

There are two different types of MOM: point-to-point and publish-and-subscribe.
1. Point-to-Point messaging style. In this model, a MOM is used by two applications to communicate with each other, often as an asynchronous replacement for remote procedure calls (RPC).
2. Publish-and-Subscribe messaging style. In this model multiple applications connect to the MOM as their publishers, which are producers of messages, or subscribers, which are consumers of messages. An important point of difference between the two styles is that a a point-to-point system is typically either a one-to-one system, which means one message sender talking to one message receiver, or it is a a many-to-one system, which means more than one senders are talking to one receiver. On the other hand, publish-and-subscribe systems are typically many-to-many systems, which means that there could be one or more publishers talking to one or more subscribers at a time.

Simple PTP (Point-to-Point) Application and Pub/Sub (Publisher and Subscriber) Application
We will enhance sample above by setting up activemq broker first and using JNDI. Configuring JNDI web application in Tomcat container can read in here. After we extract apache-activemq-5.2.0.rar that we have downloaded, edit activemq.xml that placed in folder conf. Delete all "multicast" in this configuration file. After that, enter folder bin, and run activemq. In the same project with above create packages named com.ndung.ptp and com.ndung.ps. Create new two java classes named SimpleQueueSender.java and SimpleQueueReceiver.java in com.ndung.ptp package and create new two java classes named SimpleTopicPublisher.java and SimpleTopicSubscriber in com.ndung.ps package.


package com.ndung.ptp;

import java.util.Properties;
import javax.jms.*;
import javax.naming.*;

public class SimpleQueueSender {
public static void main(String[] args) throws NamingException, JMSException {
String queueName = "PTP.ACTIVE.MQ";
//setting JNDI configuration, differents between vendors.
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
Context jndiContext = new InitialContext(props);
ConnectionFactory queueConnectionFactory = (ConnectionFactory)
jndiContext.lookup("ConnectionFactory");
QueueConnection queueConnection = (QueueConnection)
queueConnectionFactory.createConnection();
QueueSession queueSession = queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(queueName);
QueueSender queueSender = queueSession.createSender(queue);
queueConnection.start();
TextMessage textMessage = queueSession.createTextMessage();
textMessage.setText("Hello World");
queueSender.send(textMessage);
queueSession.close();
queueConnection.close();
}
}


package com.ndung.ptp;

import java.util.Properties;
import javax.jms.*;
import javax.naming.*;

public class SimpleQueueReceiver {
public static void main(String[] args) throws JMSException, NamingException {
String queueName = "PTP.ACTIVE.MQ";
//setting JNDI configuration, differents between vendors.
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
Context jndiContext = new InitialContext(props);

ConnectionFactory queueConnectionFactory = (ConnectionFactory)
jndiContext.lookup("ConnectionFactory");
QueueConnection queueConnection = (QueueConnection)
queueConnectionFactory.createConnection();
QueueSession queueSession = queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue queue = queueSession.createQueue(queueName);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
TextMessage textMessage = (TextMessage) queueReceiver.receive();
System.out.println(textMessage);
queueSession.close();
queueConnection.close();
}
}


package com.ndung.ps;

import java.util.Properties;
import javax.jms.*;
import javax.naming.*;

public class SimpleTopicPublisher {
public static void main(String[] args) throws Exception {
try {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
Context jndiContext = new InitialContext(props);
ConnectionFactory myConnectionFactory = (ConnectionFactory)
jndiContext.lookup("ConnectionFactory");
// Use myConnectionFactory to get a Topic connection
TopicConnection myConnection = (TopicConnection)
myConnectionFactory.createConnection();
// Use myConnection to create a Topic session
TopicSession mySession = myConnection.createTopicSession(false, 1);
// Use mySession to get the Topic
Topic myTopic = mySession.createTopic("PS.ACTIVE.MQ");
// Use mySession to create a publisher for myTopic
TopicPublisher myPublisher = mySession.createPublisher(myTopic);
// Start the connection
myConnection.start();
// Create the HelloWorld message
TextMessage m = mySession.createTextMessage();
m.setText("Hello World");
// Use myPublisher to publish the message
myPublisher.publish(m);
// Done.
// Need to clean up
mySession.close();
myConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}


package com.ndung.ps;

import java.util.Properties;
import javax.jms.*;
import javax.naming.*;

public class SimpleTopicSubscriber {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
Context jndiContext = new InitialContext(props);
ConnectionFactory myConnectionFactory = (ConnectionFactory)
jndiContext.lookup("ConnectionFactory");
// Use myConnectionFactory to get a Topic connection
TopicConnection myConnection = (TopicConnection)
myConnectionFactory.createConnection();
// Use myConnection to create a Topic session
TopicSession mySession = myConnection.createTopicSession(false, 1);
// Use mySession to get the Topic
Topic myTopic = mySession.createTopic("PS.ACTIVE.MQ");
// Use mySession to create a subscriber
TopicSubscriber mySubscriber = mySession.createSubscriber(myTopic);
// Start the connection
myConnection.start();
// Wait for the Hello World message
// Use the receiver and wait forever until the message arrives
TextMessage m = (TextMessage) mySubscriber.receive();
// Display the message
System.out.println("Received the message: " + m.getText());
// Done.
// Need to clean up
mySession.close();
myConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

To know the differences between PTP application and Pub/Sub application:
1. Run two instance of SimpleQueueReceiver and then run one instance of SimpleQueueSender. One of SimpleQueueReceiver got message from SimpleQueueSender and the other one didn't.
2. Run two instance of SimpleTopicSubscriber and then run one instance of SimpleTopicPublisher. We can look both of SimpleTopicSubsriber got message from SimpleTopicPublisher.
We can monitor message queue by running http://localhost:8161/admin in web browser. More better coding style in the next post.

444 comments:

«Oldest   ‹Older   401 – 444 of 444
Anonymous said...

http://buytramadolonlinecool.com/#51726 buy tramadol online usa - tramadol overdose antidote

Anonymous said...

tramadol buy day 5 tramadol withdrawal - tramadol for dogs high

Anonymous said...

http://landvoicelearning.com/#62431 order tramadol pay cod - tramadol dosage 10 lb dog

Anonymous said...

http://landvoicelearning.com/#63987 where to buy tramadol cod - tramadol online legal

Anonymous said...

buy tramadol buy tramadol online usa - tramadol hcl medicine

Anonymous said...

2013 ativan online pharmacy - generic ativan http://www.ativanonlinenorx.net/#buy-ativan, [url=http://www.ativanonlinenorx.net/#ativan-cost]ativan online pharmacy[/url]

Anonymous said...

buy klonopin online happens if overdose klonopin - 2mg klonopin blue

Anonymous said...

buy tramadol online buy tramadol 200mg - what is tramadol the generic of

Anonymous said...

buy tramadol online tramadol hcl 50 mg how many can i take - tramadol hydrochloride 50mg dosage

Anonymous said...

http://buytramadolonlinecool.com/#73892 klonopin and tramadol high - buy tramadol online legally

Anonymous said...

buy tramadol tramadol online texas - buy tramadol online no prescription next day delivery

Anonymous said...

discount klonopin klonopin dosage dog - happens you overdose klonopin

Anonymous said...

buy cheap klonopin can i buy klonopin online - ok take 2mg klonopin

Anonymous said...

http://landvoicelearning.com/#51438 buy tramadol online bluelight - buy tramadol online cod overnight

Anonymous said...

http://buytramadolonlinecool.com/#56411 tramadol dose pediatric - tramadol withdrawal time frame

Anonymous said...

http://buytramadolonlinecool.com/#30694 tramadol online overnight - buy-tramadol-overnight

Anonymous said...

buy tramadol generic tramadol online - tramadol online in europe

Anonymous said...

buy tramadol how to buy tramadol online overnight - tramadol hcl 50 mg mylan

Anonymous said...

buy tramadol medication tramadol overdose dose - what is tramadol dosage

Anonymous said...

http://landvoicelearning.com/#51602 tramadol online no prescription cod - tramadol-no-prescription.us

Anonymous said...

http://buytramadolonlinecool.com/#61458 tramadol online no prescription overnight - tramadol dosage people

Anonymous said...

buy klonopin online xanax vs klonopin social anxiety - klonopin rash

Anonymous said...

buy tramadol online tramadol 50 high - tramadol euphoria dose

Anonymous said...

http://reidmoody.com/#51208 cheap lorazepam no prescription - lorazepam 1 mg price walmart

Anonymous said...

buy ativan online generic name of ativan - lorazepam no prescription canada

Anonymous said...

carisoprodol online carisoprodol usp 350 mg - carisoprodol generic name

Anonymous said...

3, [url=http://www.onlinecelebrexdeal.net/]Celecoxib Cost[/url] - Celecoxib 200mg - order celebrex online no prescription http://www.onlinecelebrexdeal.net/.

Anonymous said...

buy tramadol online buy tramadol 50mg net - tramadol 50 mg prospect

Anonymous said...

can i buy xanax online generic xanax effectiveness - best xanax generic brand

Anonymous said...

buy tramadol online tramadol hcl how much to get high - cheap tramadol sale

Anonymous said...

buy xanax online xanax effects duration - what do generic xanax bars look like

Anonymous said...

buy tramadol online tramadol dogs - tramadol quitting

Anonymous said...

buy xanax online xanax bar mg - xanax bars 2090 v

Anonymous said...

order tramadol online cod tramadol hydrochloride 100mg used - tramadol withdrawal exercise

Anonymous said...

cheapest xanax side effects long term xanax use - can buy xanax online yahoo

Anonymous said...

Yоur article features veгified beneficial to me.
It’s quite eduсational anԁ you're clearly extremely well-informed in this area. You have exposed my eye in order to various thoughts about this matter together with intriguing and reliable content.

Visit my site; viagra
Also visit my weblog - viagra

Anonymous said...

http://southcarolinaaccidentattorney.com/#51936 carisoprodol gaba - carisoprodol soma 350

Anonymous said...

buy xanax online xanax and alcohol and pot - many xanax pills overdose

Anonymous said...

6, [url=http://www.cheapzoloftrx.net/] Zoloft For Sale[/url] - Buy Zoloft - zoloft online no prescription http://www.cheapzoloftrx.net/ .

Anonymous said...

Your οωn write-up has еѕtablisheԁ necesѕaгy to me.
It’s quіte usеful and уou гeallу аге naturаllу really
educated in thіs region. You have gοt expоsed my own eуe to numеrous viеws on this subject uѕіng intгiguing, notаble аnd rеliаble сontent.



Hеrе is mу web page http://wiki.indonesiaunite.com/
Feel free to visit my weblog ; buy viagra

Anonymous said...

Your οwn wгіte-up features confirmeԁ beneficial tо me
persοnally. It’s quite helpful and you are
naturally reallу knowlеdgeablе іn
this area. You have expοsed our eye іn order to numerous opinіon of this matter
with intriguing and sounԁ articles.

my sitе ... buy Valium
My web blog - buy Valium

Anonymous said...

It's nearly impossible to find educated people for this topic, however, you seem like you know what you're talking about!
Thanks

Also visit my web site :: discount codes

Anonymous said...

People individuals actually consists in mastering completely different china and
seeking to end up making state of the art containers eachtime
each locate point also known as Of baking freaks.
When using the debute with all the MiniPlus your own coffee machine, Keurig could
be referfing to any kind of a sounding brewing beer
at home routine lines, our High level B40, all of the Special B60, those American platinum eagle B70, together with
amalgamated Breville high-quality revolutionary producing games.
A full drink handling procedure am attainable havin their
innovative 1-6 glass marketable complex theme. Definitely is usually supported back
in smaller k-cups also meals by reason of in which way long lasting it's actually. Massive terribly impressed that there is plastic cards inside Coffee brewer.

My web-site cuisinart 12 cup coffeemaker

vimax canada said...

thanks for sharing

«Oldest ‹Older   401 – 444 of 444   Newer› Newest»