JMS API is used to allow applications to create, send, receive messages. It is mostly used in enterprise distributed systems (J2EE applications) for processing data asynchronously. It consists of
JMS API architecture
Messaging domain
Messaging receiver
JMS API Architecture:
JMS application consists of :
There are 2 types of approaches for messaging- Point to point and Publish/subscribe.
Apache ActiveMQ, Weblogic, OpenJMS, IBM Websphere MQ are few examples of JMS provider implementations.
Below sample example shows simple standalone example of JMS point to point messaging where administered objects (server configuration) are not used. You need Apache ActiveMQ jars to run this program.
Sample TestJMS.java file:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQQueue;
public class TestJMS {
public static void main(String[] args) {
try {
//Start JMS service
BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:61616");
broker.start();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection conn = connectionFactory.createConnection();
Queue queue = new ActiveMQQueue("testQueue");
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Message Producer
MessageProducer prod = session.createProducer(queue);
//Message consumer
MessageConsumer consumer = session.createConsumer(queue);
TextMessage msg = session.createTextMessage("Hello JMS");
conn.start();
prod.send(msg);
Message message = null;
while((message = consumer.receiveNoWait()) != null){
if(message != null){
System.out.println("Message received:"+((TextMessage)message).getText());
}
}
conn.close();
broker.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output of program:
Message received:Hello JMS
You can download ActiveMQ at : http://activemq.apache.org/
I have used "activemq-all-5.6.0.jar" as Build path library to run above application.
JMS API architecture
Messaging domain
Messaging receiver
JMS API Architecture:
JMS application consists of :
- JMS provider: Messaging system that implements JMS interfaces and provide admin and control features
- JMS Client: Programs that produce and consume messages.
- Messages: objects that are transferred between JMS clients to communicate information.
- Administered Objects: pre configured JMS objects created by JMS administrator to be used by clients. Two kind of objects are destinations and connection factories.
There are 2 types of approaches for messaging- Point to point and Publish/subscribe.
- Point to Point messaging: Based on message queue, message sender and message receiver. Message is addressed to specific queue. Queues retain all message until it is consumed by receiver or until message expires.
- Publish/Subscribe messaging: In Pub/Sub messaging, message producer sends message to Topic. Message is delivered to receivers who have subscribed to that topic.
Apache ActiveMQ, Weblogic, OpenJMS, IBM Websphere MQ are few examples of JMS provider implementations.
Below sample example shows simple standalone example of JMS point to point messaging where administered objects (server configuration) are not used. You need Apache ActiveMQ jars to run this program.
Sample TestJMS.java file:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQQueue;
public class TestJMS {
public static void main(String[] args) {
try {
//Start JMS service
BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:61616");
broker.start();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection conn = connectionFactory.createConnection();
Queue queue = new ActiveMQQueue("testQueue");
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Message Producer
MessageProducer prod = session.createProducer(queue);
//Message consumer
MessageConsumer consumer = session.createConsumer(queue);
TextMessage msg = session.createTextMessage("Hello JMS");
conn.start();
prod.send(msg);
Message message = null;
while((message = consumer.receiveNoWait()) != null){
if(message != null){
System.out.println("Message received:"+((TextMessage)message).getText());
}
}
conn.close();
broker.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output of program:
Message received:Hello JMS
You can download ActiveMQ at : http://activemq.apache.org/
I have used "activemq-all-5.6.0.jar" as Build path library to run above application.
No comments:
Post a Comment