Monday, 6 June 2016

JMS Questions with Answers

Java Message Service is an interface for point-to-point queuing and Topic (Publish/Subscribe). For interview purpose, here are some questions along with its answers.

What is JMS? 

Java Message Service is an interface implemented by most JAVAEE containers to provide point-to-point queuing and topic (publish/subscribe) behavior. JMS is frequently used by EJB's that need to start another process asynchronously.
For example, instead of sending an email directly from an Enterprise JavaBean, the bean may choose to put the message onto a JMS queue to be handled by a Message-Driven Bean (another type of EJB) or another system in the enterprise. This technique allows the EJB to return to handling requests immediately instead of waiting for a potentially lengthy process to complete.


What type messaging is provided by JMS? 

JMS provides both synchronous and asynchronous messaging.


How many messaging models does JMS provide? 

JMS provides two messaging models, publish-and-subscribe (Topic) and point-to-point queuing.


What is point-to-point model in JMS? 

A point-to-point model is based on the concept of a message queue i.e. Senders send messages into the queue, and the receiver reads messages from this queue. In the point-to-point model, several receivers can exist, attached to the same queue but the message will be delivered to one of them. Its a one-to-one communication.


What are the advantages of JMS? 

One of the main advantages of JMS is that it is asynchronous. Thus not all the components need to be up all the time for the application to function as a whole.


What is the publish-and-subscribe model in JMS? 

A publish-subscribe model is based on the message topic concept. In other words, publishers send messages to a topic, and all subscribers of the given topic receive these messages. Its a one-to-many communication


What is JMS administered object? 

A preconfigured JMS object (a resource manager connection factory or a destination) created by an administrator for the use of JMS clients and placed in a JNDI namespace


What is publish/subscribe messaging? 

With publish/subscribe message passing the sending application/client establishes a named topic in the JMS broker/server and publishes messages to this queue. The receiving clients register (specifically, subscribe) via the broker to messages by topic; every subscriber to a topic receives each message published to that topic. There is a one-to-many relationship between the publishing client and the subscribing clients.

Which models are supported by JMS? 
Publish/subscribe (pub/sub). This model allows a client (publisher) to send messages to a JMS topic. These messages are retrieved by other clients (subscribers) (it may happen so that a topic has no subscribers) asynchronously. Pub/sub model requires a broker distributing messages to different consumers.


What are the different parts of a JMS message? 

A JMS message contains three parts:

  • Header
  • Optional properties 
  • Optional body



What is the main parts of JMS applications? 

The main parts of JMS applications are:

  • ConnectionFactory and Destination
  • Connection
  • Session
  • MessageProducer
  • MessageConsumer
  • Message



What is the role of the JMS Provider? 

The JMS provider handles security of the messages, data conversion and the client triggering. The JMS provider specifies the level of encryption and the security level of the message, the best data type for the non-JMS client.


What is the difference between Java Mail and JMS Queue? 

JMS is the ideal high-performance messaging platform for intrabusiness messaging, with full programmatic control over quality of service and delivery options.
JavaMail provides lowest common denominator, slow, but human-readable messaging using infrastructure already available on virtually every computing platform.


Does JMS specification define transactions? 

JMS specification defines a transaction mechanisms allowing clients to send and receive groups of logically bounded messages as a single unit of information. A Session may be marked as transacted. It means that all messages sent in a session are considered as parts of a transaction. A set of messages can be committed (commit() method) or rolled back (rollback() method). If a provider supports distributed transactions, it's recommended to use XAResource API.


What is synchronous messaging? 

Synchronous messaging involves a client that waits for the server to respond to a message. So if one end is down the entire communication will fail.


What is asynchronous messaging? 

Asynchronous messaging involves a client that does not wait for a message from the server. An event is used to trigger a message from a server. So even if the client is down , the messaging will complete successfully.


How does a typical client perform the communication?  


  1. Use JNDI to locate administrative objects.
  2. Locate a single ConnectionFactory object.
  3. Locate one or more Destination objects.
  4. Use the ConnectionFactory to create a JMS Connection.
  5. Use the Connection to create one or more Session(s).
  6. Use a Session and the Destinations to create the MessageProducers and MessageConsumers needed.
  7. Perform your communication.



What is JMS session? 

A single-threaded context for sending and receiving JMS messages. A JMS session can be nontransacted, locally transacted, or participating in a distributed transaction.


Where should we use JMS?

JMS is the ideal high-performance messaging platform for intrabusiness messaging, with full programmatic control over quality of service and delivery options.


What is the difference between durable and non-durable subscriptions? 

A durable subscription gives a subscriber the freedom of receiving all messages from a topic, whereas a non-durable subscription doesn't make any guarantees about messages sent by others when a client was disconnected from a topic.


What is the difference between Message producer and Message consumer? 

Messaging systems provide a host of powerful advantages over other conventional distributed computing models. Primarily, they encourage "loose coupling" between message consumers and message producers. There is a high degree of anonymity between producer and consumer: to the message consumer, it doesn't matter who produced the message, where the producer lives on the network, or when the message was produced.