Java Message Service(JMS)
March 26, 2010 Leave a Comment
JMS (Java Message Service) is a Java API that allows java applications to communicate with other messaging implementations by creating, sending, receiving, and reading messages. It supports two models:
- point-to-point or queuing model: In this model, a producer sends messages to a particular queue and a consumer gets messages from the queue. Only one consumer will get the message and producer knows the consumer of the message.
- publish and subscribe model: In this model, many subscribers can receive messages on a particular message topic. Publisher and subscriber both are unaware of each other.
Java package “javax.jms” provides JMS api. To use JMS, one must have a JMS provider that can manage the sessions and queues. There are free, open source providers like Apache ActiveMQ, OpenJMS, JBoss Messaging etc and proprietary providers like BEA Weblogic, Oracle AQ etc.
Elements of JMS: |
JMS consists of several elements, some of them are listed below with their functionality.
|
JMS models: |
| The two models which are supported by JMS API are: point-to-point or queuing model and publish and subscribe model. In the point-to-point or queuing model, the producer posts messages to a particular queue and the consumer reads messages from the queue which are posted by producer. Here, the producer knows the destination of the message(the consumer) and posts the message directly to the consumer’s queue.And the publish/subscribe model supports publishing messages to a particular message topic. In this model, neither the publisher nor the subscriber know about each other. Zero or more subscribers may register the interest in receiving messages on a particular message topic. A good example for it is “anonymous bulletin board”. |
Defferent API’s used in JMS are: |
The JMS API are provided in the Java package javax.jms package. The defferent types of interfaces which are used in JMS and thier descriptions are listed as below:
|
Some interview questions
, try to find answers for them
- What is the use of Message object?
- What is the basic difference between Publish Subscribe model and P2P model?
- What is the use of BytesMessage?
- What is the use of StreamMessage?
- What is the use of TextMessage?
- What is the use of ObjectMessage?
- What is the use of MapMessage?
- What is the difference between BytesMessage and StreamMessage?
- What is object message ?
- What is text message?
- What is Map message?
- What is the difference between queue and topic ?
- What are the three components of a Message ?
- What is the difference between queue and topic?
- What are the types of messaging?
- What is the difference between Point to Point and Publish/Subscribe
- Why doesn?t the JMS API provide end-to-end synchronous message delivery and notification of delivery?
- What are the core JMS-related objects required for each JMS-enabled application?
- How does the Application server handle the JMS Connection?
- What is Stream Message ?
- What Is the JMS API?
- How JMS is different from RPC?
- What type messaging is provided by JMS ?
- What is JMS application ?
- What is the difference between Message producer and Message consumer?
- What is the difference between durable and non-durable subscriptions?
- What is the use of JMS? In which situations we are using JMS? Can we send message from one server to another server using JMS?
- What is JMS session?
- What is asynchronous messaging?
- What is synchronous messaging?
- Does JMS specification define transactions?
- What is the Role of the JMS Provider?
- What Is Messaging?
- What is the main parts of JMS applications?
- Which models are supported by JMS?
- What is publish/subscribe messaging?
- What is JMS administered object?
- What is the publish-and-subscribe model in JMS?
- What are the advantages of JMS?
- Is it mandatory to place class files in the WEB-INF folder and all JSP’s outside?
- What type messaging is provided by JMS?
- How may messaging models do JMS provide for and what are they?
- What is the point-to-point model in JMS?
- What is JMS?
- Explain the creation of JMS Administered objects?
- Explain the creation of JMS Administered objects?
- Explain about the exception handling mechanism of JMS?
- Explain about map message?
- Explain about message selector?
- Explain about message listeners?
Tutorial
Create JMS queue in Glassfish, we assume you have glassfish up and running.
Configure GlassFish (Version 2.1):
1. Log into glassfish admin, http://localhost:4848
2. Create JMS Resources:
1. Expand Resources / JMS Resources
2. Click on “Connection Factories”:
1. Click New and give the following property values:
1. JNDI Name = “jms/ConnectionFactory”
2. Resource Type = “javax.jms.ConnectionFactory”
3. Accept all other default values
2. Click OK.
3. Click on “Destination Resources”.
1. Click New and give the following property values:
1. JNDI Name = “jms/Queue”
2. Physical Destination Name = “PhysicalQueue”
3. Resource Type = javax.jms.Queue
4. Status = Enabled (checked)
2. Click OK
3. Configure JMS
1. Expand Configuration / Java Message Service
2. Click on Physical Destinations
3. Ensure that 2 destinations exist: mq.sys.dmq, PhysicalQueue.
NOTE: If these entries do not exist run each of the following commands from C:\glassfish\bin to add them
asadmin create-jmsdest –host localhost –port 7676 –desttype queue –property User=public:Password=pubic PhysicalQueue
1.
1. Click on “JMS Hosts”
1. Click on default_JMS_host (If it doesn’t exist, click new)
2. Verify values on Edit JMS Host screen
1. Name = default_JMS_host
2. Host = [Your local machine name]
3. port = 7676
4. Admin Username = admin
5. Admin Password = admin
6. Click Save
2. Click on “Java Message Service”
1. Select type = LOCAL
2. Ensure Default JMS Host = “default_JMS_host”
3. Leave all other default settings.
4. Click save
5. Click the “Ping” button up top: should show successful message.
2. Add new environment variables:
Java Program to produce and consume JMS queue messages…
import java.util.Enumeration;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.ConnectionFactory;
public class JMSProducerConsumer {
public static void main(String[] args) throws JMSException {
System.out.println(“Start Program…”);
ConnectionFactory connectionFactory;
Queue queue;
String jmsServer = “localhost:7676″;
connectionFactory = new com.sun.messaging.ConnectionFactory();
connectionFactory.setProperty(ConnectionConfiguration.imqAddressList,jmsServer);
connectionFactory.setProperty(ConnectionConfiguration.imqReconnectEnabled, “true”);
String queueName = “PhysicalQueue”;
queue = new com.sun.messaging.Queue(queueName);
Connection connection = null;
try {
connection = (Connection) connectionFactory.createConnection();
Session session = (Session) connection.createSession(false,Session.CLIENT_ACKNOWLEDGE);
Destination dest = (Destination) queue;
MessageProducer producer = session.createProducer(dest);
producer.setPriority(9);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
MapMessage mapMessage = session.createMapMessage();
mapMessage.setJMSType(“test”);
// map of data in JMS queue
mapMessage.setStringProperty(“hello”, “world”);
mapMessage.setString(“hello”, “World”);
producer.send(mapMessage);
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
//start consuming the message, in this example same program is producing and consuming the message but we can have consumer running on
//different vm or server.
consumeMessage();
}
private static void consumeMessage() {
MessageConsumer consumer = null;
Connection connection = null;
Session session = null;
try {
ConnectionFactory connectionFactory = new com.sun.messaging.ConnectionFactory();
int queueWait = 2000;
String jmsServer = “localhost:7676″;
connectionFactory.setProperty(ConnectionConfiguration.imqAddressList, jmsServer);
connectionFactory.setProperty(ConnectionConfiguration.imqReconnectEnabled, “true”);
String queueName = “PhysicalQueue”;
Queue queue = new com.sun.messaging.Queue(queueName);
Destination dest = (Destination) queue;
connection = (Connection) connectionFactory.createConnection();
session = connection.createSession(false,Session.CLIENT_ACKNOWLEDGE);
connection.start();
checkMessage(session,queue);
consumer = session.createConsumer(dest);
Message message = consumer.receive(queueWait);
if (!(message instanceof MapMessage)) {
throw new Exception(
“Recieved a message that was not MapMessage format.”);
}
MapMessage mapMessage = (MapMessage) message;
System.out.println(“The message is “+mapMessage.getString(“hello”));
System.out.println(“The message is “+mapMessage.getStringProperty(“hello”));
//this will remove message from queue otherwise message will remain in queue
message.acknowledge();
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// close the consumer if not null
if (consumer != null) {
try {
consumer.close();
} catch (Exception e) {
}
}
// close the session if not null
if (session != null) {
try {
session.close();
} catch (Exception e) {
}
}
// close the connection if not null
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
}
private static void checkMessage(Session session,Queue queue) throws JMSException {
// TODO Auto-generated method stub
// find out if any messages on the queue, if so, how many
QueueBrowser browser = session.createBrowser(queue);
Enumeration msgEnum = browser.getEnumeration();
int msgCnt = 0;
while (msgEnum.hasMoreElements()){
Message message = (Message)msgEnum.nextElement();
msgCnt ++;
}
System.out.println(“There are “+msgCnt+” in queue”);
browser.close();
}
}