rh banner

Saturday, 3 January 2015

JMS - Java Messaging Service - Interview question with answers for experienced candidates

JMS - Java Messaging Service - Interview question with answers for experienced candidates

The Java Message Service defines a intra-business messaging API, designed to be easily and efficiently supported by a wide range of enterprise messaging products. Enterprises need to send and receive multiple messages. JMS supports this requirement by using asynchronous messaging.

1.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.

2. 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.

Explain the Role of the JMS Provider?

JMS Provider is an implementation of the JMS interface for a Message Oriented Middleware (MOM). Providers are implemented as either a Java JMS implementation or an adapter to a non-Java MOM.
Their functions are:
- Handling security of messages
- Data Conversion
- Client triggering
- Specifying encryption level, security level, best data type for JMS Client.

What is the use of BytesMessage?

BytesMessage contains an array of primitive bytes in it's payload. When the datatypes of two applications are not compatible with other message types,, they can send them using ByteMessage. JMS can be simply used as a transport medium with the payload being opaque to the JMS client.
However, it is important that the payload be read in the same order and type when it was created. Since there is no boundary line between the different data types stored. Thus you can even read a long as short. This would result in erroneous data.

Describe the use of StreamMessage?

StreamMessage carries a stream of Java primitive types as it's payload. Unlike ByteMessage, there are restrictions on the way the data is read to avoid erroneous output. StreamMessage prevents reading a long value as short, something that is allowed in case of BytesMessage. SreamMessage enforces a set of strict conversion rules which actually prevents reading of one primitive type as another.

What is the important part of JMS applications?

Session
Connection
Message
Message Producer
Message Consumer
Connection factory and destination

Mention the difference between durable and non-durable subscription?

Durable subscription gives a subscriber the freedom of receiving all messages from a topic, while a non-durable subscription does not make any guarantees about messages sent by others when a client get disconnected by others.

What is the difference between Point to Point and Publish and Subscribe method?

In point to point communication, the producers and consumers of the message are defined. The producers push the messages in the queues and the receivers pull them.
In publish and subscribe method, the producers publish the message and the subscribers who have subscribed to that topic receive them. In this way, a message may be received, or processed, by multiple consumers.

Explain the use of Message object.

Message Object is a light weight entity that comprises of only header and properties. It does not comprise of payload. Thus, no data needs to be transferred when the receivers just need to be notified. Due to this using message becomes a very efficient way.

What is the difference between JMS and RPC?

The basic difference between RPC and JMS lies in the way they message. RPC uses synchronous messaging while JMS uses asynchronous messaging approach.
In RPC the method invoker waits for the method to finish execution and return the control back to the invoker.
In JMS the message sender just sends the message to the destination and continues it's own processing.

What are transaction options available in JMS?

An application has myriad transaction options available, including whether or not it wants to participate in transactions. If your application does not use transactions, it can use one of these acknowledgement modes: auto, duplicates okay, and client. You specify the acknowledgement modes when creating a JMS session. If your application uses transactions, it can choose from these transaction options: transacted session, MDB with container-managed transaction demarcation (CMTD), and MDB with bean-managed transaction demarcation (BMTD). The following lists briefly describe these acknowledgement modes and transaction options.

Acknowledgement options:

·Auto mode: When a session uses auto mode, the messages sent or received from the session are automatically acknowledged. This is the simplest mode and expresses JMS's power by enabling once-only message delivery guarantee.
·Duplicates okay mode: When a session uses duplicates okay mode, the messages sent or received from the session are automatically acknowledged just like auto mode, albeit lazily. Under rare circumstances, the messages might be delivered more than once. This mode enables at-least-once message delivery guarantee.
·Client mode: When a session uses client mode, the messages sent or received from the session are not acknowledged automatically. The application must acknowledge the message receipt. This mode gives the application (rather than the JMS provider) complete control over message acknowledgement, at the cost of increased code complexity.
Other types of acknowledgement modes are possible. However, these acknowledgement modes are JMS provider specific, and therefore, compromise the JMS application portability.

Transaction options:
·Transacted session: An application can participate in a transaction by creating a transacted session (or local transaction). The application completely controls the message delivery by either committing or rolling back the session.
·Message-driven beans with CMTD: An MDB can participate in a container transaction by specifying CMTD in the XML deployment descriptor. The transaction commits upon successful message processing or the application can explicitly roll it back.
·Message-driven beans with BMTD: An MDB can choose not to participate in a container transaction by specifying BMTD in the XML deployment descriptor. The MDB programmer has to design and code programmatic transactions.

What is MDB and What is the special feature of that?

MDB is Message driven bean, which very much resembles the Stateless session bean. The incoming and out going messages can be handled by the Message driven bean. The
ability to communicate asynchronously is the special feature about the Message driven bean.

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.

What is the use of TextMessage? 

TextMessage contains instance of java.lang.String as it's payload. Thus it is very useful for exchanging textual data. It can also be used for exchanging complex character data such as an XML document.

What is the use of MapMessage? 

A MapMessage carries name-value pair as it's payload. Thus it's payload is similar to the java.util.Properties object of Java. The values can be Java primitives or their wrappers

What is JMS queue? 

A staging area that contains messages that have been sent and are waiting to be read. Note that, contrary to what the name queue suggests, messages don't have to be delivered in the order sent. If the message driven bean pool contains more than one instance then messages can be processed concurrently and thus it is possible that a later message is processed sooner than an earlier one. A JMS queue guarantees only that each message is processed only once.

What is JMS topic? 

A distribution mechanism for publishing messages that are delivered to multiple subscribers.

Why doesn’t the JMS API provide end-to-end synchronous message delivery and notification of delivery? 

Some messaging systems provide synchronous delivery to destinations as a mechanism for implementing reliable applications. Some systems provide clients with various forms of delivery notification so that the clients can detect dropped or ignored messages. This is not the model defined by the JMS API. JMS API messaging provides guaranteed delivery via the once-and-only-once delivery semantics of PERSISTENT messages. In addition, message consumers can insure reliable processing of messages by using either CLIENT_ACKNOWLEDGE mode or transacted sessions. This achieves reliable delivery with minimum synchronization and is the enterprise messaging model most vendors and developers prefer. The JMS API does not define a schema of systems messages (such as delivery notifications). If an application requires acknowledgment of message receipt, it can define an application-level acknowledgment message.

When to Remove messages from the queue ?

When an application uses ConnectionConsumers, JMS might need to remove messages from the queue in a number of situations:
1) Badly formatted message
A message might arrive that JMS cannot parse.
2) Poison message
A message might reach the backout threshold, but the ConnectionConsumer fails to requeue it on the backout queue.
3)No interested ConnectionConsumer
For point-to-point messaging, when the QueueConnectionFactory is set so that it does not retain unwanted messages, a message arrives that is unwanted by any of the ConnectionConsumers.

What is poison messages? And how to handle poison messages?

Poison messages, messages the application can never successfully process.
A badly-formatted message arrives on a queue. Such a message might make the receiving application fail and back out the receipt of the message. In this situation, such a message might be received, then returned to the queue, repeatedly. These messages are known as poison messages. The ConnectionConsumer must be able to detect poison messages and reroute them to an alternative destination.

What are the Message Headers in JMS message ?

JMSDestination - send or publish method
JMSDeliveryMode - send or publish method
JMSExpiration - send or publish method
JMSPriority - send or publish method
JMSMessageID - send or publish method
JMSTimestamp - send or publish method
JMSCorrelationID - Client
JMSReplyTo - Client
JMSType - Client
JMSRedelivered - JMS provider