Most Popular JMS Interview Questions and Answers for Fresher and Experienced Professionals:
JMS or Java Messaging Service has presently become one of the most dominant models for secure, reliable, and scalable delivery of messages across the world.
This model is very well structured and supports several forms of messaging techniques and protocols.
Let’s dive in and go through some questions and answers that are frequently asked on this topic across the industry.
Table of Contents:
Most Popular JMS Interview Questions
Given below is a list of the most frequently asked Java Message Service interview questions along with detailed answers.
Q #1) What is JMS?
Answer: Java Messaging Service is a Java API, which enables the systems to create, read, send, and receive messages.
The most important part of the algorithm is very well structured and allows one application to send a message to another application and also enables broadcasting features to the subscribers.
Q #2) What are the types of communication provided by JMS? Explain in detail.
Answer: This API provides two types of communication:
- Asynchronous: The message will be delivered to the client, it is not required for the client to send requests to receive it. The client application will receive it once the sender application transmits.
- Reliable: Here the message is sent to the client application once the API protocol ensures the availability of the receiver application.
Q #3) What is the number of messaging models available on JMS?
Answer: Specifically, there are two types of models provided by JMS:
Point to Point: As the name itself suggests it is a one-to-one messaging mechanism, where the sender sends a message to a single receiver. The message is available to the receiver application once it’s ready and until then the message is stored in the queue.
The most important part of it is there are zero dependencies for time between both the sender and the receiver application.
Publish and Subscribe: This messaging mechanism is very uniquely designed by JMS.
For Example, one reader subscribes to one blog where the person is interested. Now there may be several people interested in a particular blog.
And they subscribe/register to that blog. Now once a new post or topic is published on the blog, all the registered readers will get an update. This messaging model is called Publish and Subscribe.
Q #4) What is a Queue?
Answer: In the point to point mechanism of JMS, the source application sends a message to the destination application, the message is consumed by the destination application once it is available, till that time the storage unit of the very time is called a queue.
Q #5) What is a Topic?
Answer: In the Publish/Subscribe model, the client/publisher application generates one message and that message is available to all the subscribers or destination applications. This message is called a Topic.
Q #6) What is the principle difference between the working mechanism of JMS and RPC?
Answer: The identifiable difference between the two models lies in how the message is delivered.
In the case of JMS, the sender application sends the message to the destination application and then it again waits/or processes another message as per the programming criteria.
In the case of RPC, the thread is completed once the message reaches the destination and the control comes back to the method responsible for message transport.
Q #7) What is Message Oriented Middleware?
Answer: Message Oriented Middleware is software that works between the sender application and destination application in the JMS working model.
Q #8) How is Message Oriented Middleware responsible for no time dependency between the sender and receiver component with respect to the Point to Point model on JMS?
Answer: Since the MOM middleware operates between the sender and receiver component it takes care of the message and transports the message by queuing mechanism. So, until the destination/receiver application becomes available to receive/read the message, the message is stored in a queue.
The most important part is that the method responsible for sending the message is not occupied until the receiver application receives the message. Thus, both the sender and receiver application works independently without any time dependency.
Q #9) Name the messaging types supported by JMS.
Answer: The type of messages that are supported by JMS are:
- Text Messages
- Stream Messages
- Map Messages
- Bytes Messages
- Object Messages
Q #10) What is a Bytes Message?
Answer: The Bytes Message object is actually responsible for sending the message containing a stream of uninterrupted bytes and it inherits from the message interface and adds a bytes message body. The message receiver is responsible for the interpretation of the message.
JMS API allows the transportation of this type of messages, but as per oracle docs, they are typically not used as the inclusion of properties may affect the message format.
Q #11) What is a StreamMessage?
Answer: A StreamMessage object is used to send the stream of primitive data types in the Java programming language. The data are sequentially filled and read. It inherits from the Message interface and adds a stream message body.
java.io.DataInputStream and java.io.DataOutputStream are APIs supporting these types of messaging.
Q #12) What is a Text Message?
Answer: A Text message is the one that is taken care of by java.lang.String and it inherits from the message interface and adds a text message body. This is used to transport the messages containing a text.
Q #13) What is an Object message?
Answer: An object message generally contains a serializable Java object in its message body. Generally, the receiver application receives the Object message in a read-only mode.
Q #14) What is a Map message?
Answer: The message body of the Map Message object contains a set of name-value pairs, where the names are String objects, and values are Java primitives. The entries can be accessed sequentially or randomly by name. Map Message actually inherits from the Message interface and adds a message body that contains a Map.
Q #15) What is JNDI? How is it related to JMS?
Answer: JNDI is the Java Naming and Directory Interface. If an application is connected to a database, it allows the application developer to give a name to that database instead of worrying about the database connection credentials.
The JNDI API will access the naming directory find the mapping between the name and the database object and connect accordingly. We can use this mechanism while we are connecting to any connectionFactory (queue or topic) for sending messages.
Q #16) How does a sender application transport/send a message through JMS?
Answer: Given below are a few ways in which a message is sent through JMS:
- Implement JNDI to look up the credentials of the connectionFactory.
- Create a connectionFactory object for implementation.
- Identify the destination objects(one or more).
- Utilize the connectionFactory object to establish the JMS connection.
- Create one or more sessions.
- Use a Session and the Destinations to create the MessageProducers and MessageConsumers needed.
- Communicate using the channel.
Q #17) Name the components of JMS.
Answer: The components of JMS include:
- JMS Provider
- JMS Client
- Messages
- Administered Objects
- Native Clients
Q #18) What is Administered Objects in JMS?
Answer: The JMS administered object is actually those credentials configured by the administrator in order to connect with the JMS client and are defined under JNDI. These objects are configured before connecting with the JMS client inside the server.
Q #19) What are the functionalities of a JMS Provider?
Answer: JMS Provider basically takes care of security and data.
It is responsible for ensuring that the message is delivered in a secure manner, it also takes care of data encryption and data encoding standards and is responsible for invoking the message for the Non-JMS client.
Q #20) What is a JMS session?
Answer: A JMS session is a state controlling the total flow from sending to receiving the JMS messages.
Q #21) Can we use JMS for sending automated e-mails?
Answer: JMS has no standard APIs supporting the feature however we can use JavaMail to send automated emails.
Q #22) What is the functionality of a message Listener in the context of JMS?
Answer: Message Listener is typically used with message consumer in the case of asynchronous delivery. For asynchronous delivery, one can register an object of MessageListener with messageConsumer.
Q #23) What is the JMS Client?
Answer: The JMS client is a component written in the Java programming language that is responsible for invoking and consuming message bodies.
Q #24) What is a Message?
Answer: A message is a body, rather a component which communicates between the JMS clients.
Q #25) What is the functionality of a JMS message Producer?
Answer: A message producer is basically a component that is created by a JMS session for sending a message to the receiver application.
One can create a session and implement the MessageProducer interface to define a destination object, queue object or topic object. One can declare a producer as unspecified by assigning null in its argument instead of an object. Later we can use Java method overloading on the send method to specify a destination, or message as arguments or parameters.
Q #26) What is the functionality of JMS message Consumers?
Answer: A message consumer is a component that is created by a JMS session for receiving a message by the receiver application. One can create a session and implement a MessageConsumer interface to define a destination object, queue object or topic object.
One can use createDurableSubscriber with the session object to create a durable topic subscriber, but one can use it to create a topic for the Publish/Subscribe model and not for creating queues.
The consumer becomes active once the consumer object is created. We can use the object to receive and send messages. To deactivate this, one can use a close method for a MessageConsumer.
Q #27) What is the functionality of a JMS Queue Browser?
Answer: As we have previously discussed the concept of the queue, where the message is stored until the receiver receives it. The functionality of browsing the messages in the queue and displaying the header values is supported by the QueueBrowser object.
One can create a QueueBrowser object via. JMS session.
Q #28) What is the functionality of a JMS Message Selector?
Answer: The JMS Message selector is an API that is responsible for filtering the messages that it receives for any particular application. Message Selectors assign the job to the JMS Provider, which is actually responsible for filtering messages.
A message selector actually takes string type values as input.
WatchType = ’Titan’ OR WatchType = ’Rolex’
The createConsumer and createDurableSubscriber methods allow one to specify a message selector as an argument when one creates a message consumer.
Q #29) How to handle the exception caused by JMS?
Answer: The main class responsible for throwing JMS related exceptions by JMS API is JMSException.
Catching JMSException provides a generic way of handling all the exceptions related to the JMS API.
The JMS Exception class includes the following subclasses, that are described in the API documentation:
- IllegalStateException
- InvalidClientIDException
- InvalidDestinationException
- InvalidSelectorException
- JMSSecurityException
- MessageEOFException
- MessageFormatException
- MessageNotReadableException
- MessageNotWriteableException
- ResourceAllocationException
- TransactionInProgressException
- TransactionRolledBackException
Q #30) How to handle non-transacted sessions with respect to JMS?
Answer: In case of non-transacted sessions, messages are acknowledged on the basis of the argument passed while creating a session object of QueueSession or TopicSession method.
The below options are generally used according to the business requirements:
- Session.AUTO_ACKNOWLEDGE: If one passes this argument while creating a session object then, if JMSException occurs, then a reliable consumer waits for a few seconds and then calls the method MessageConsumer.receive to receive the messages again. Due to failover, if any message is not delivered, then it will be redelivered.
- Session.CLIENT_ACKNOWLEDGE: If one passes this argument while creating a session object then, if JMSException occurs, the consumer calls Session.recover before calling Message.aknowledge or MessageConsumer.receive, because Session.recover is responsible for recovering and redelivering unacknowledged messages.
- Session.DUPS_OK_ACKNOWLEDGE: If one passes this argument while creating a session object then, if JMSException occurs, then a reliable consumer waits for a few seconds and then calls the method MessageConsumer.receive to receive the messages again. But here one can receive duplicate messages or the same messages redelivered as in this mode before failover, acknowledged messages may be redelivered.
Note: Here in the example code I have used QueueSession, but one can use TopicSession to pass these arguments.
Q #31) What is the functionality of the Oracle Glassfish server? What added advantage it has on top of Apache Tomcat server?
Answer: The Glassfish server is actually an application server and can be also used as web servers which means it can handle HTTP requests from web browsers.
As an application server, it is developed to handle all types of Java Enterprise applications in terms of servlets/JSP and also EJB components.
Whereas, Tomcat server is actually a servlet container that is generally used for handling servlet or JSP components.
Q #32) How to create an EJB session in order to start a JMS connection?
Answer: We can create an EJB session for JMS as we have written in the below code.
Q #33) Describe the concept of Message Driven Bean Clustering.
Answer: If an EJB component-based application is deployed on any application server cluster, then it can be configured to run on any server inside the cluster to provide availability and scalability for the application.
If an EJB is in the form of Message Driven Bean (MDB), then it can run on any server inside the cluster and can be initiated parallel to a number of application servers in the cluster.
Conclusion
I hope that this list of top JMS interview questions would have really been informative and I’m sure that you can crack any interview successfully with a thorough knowledge of this list.
Hopefully, this would have helped you a lot!! Happy Learning!!