Enterprise Java

Latest 20+ JMS Interview Questions and Answers (2020)

A quick walk through to JMS(Java Message Service) frequently asked interview questions and answers. The lastest questions from 2020 are added to the list.

1. Introduction

In this tutorial, We’ll learn about JMS interview questions that are frequently asked in 2020. As part of the interview, There are chances to ask some of the questions on JMS area if you have 6 years plus. But, even less experience, it is good to have in the profile on JMS experience. The interviewer will check as messaging is a key aspect of enterprise Java development.

JMS is a popular open-source Messaging API and many vendors such as
Apache Active MQ, Websphere MQ, Sonic MQ provides an implementation of Java messaging API or JMS.

Usually, Any interview starts with a basic. If all questions are answered properly then we will go onto the JMS experience project-based questions.

Basics mean What is Topic? What is the Queue? What is Publisher? What is Subscriber? What are a Publisher and Subscriber model? How to configure MQ?

Next level means Questions on a project where you have implemented JMS concepts?

2. JMS Interview Questions

2.1 WHat is the meaning of JMS?

JMS is a Java Messaging Service that is provided by Sun Microsystems that provides communication among the computers in a network. This is a standard to interact with the client system. Here, the J2EE application gets the ability with JMS to create, send or receive, read the messages.

2.2 What are the Messaging types provided by JMS?

JMS comes with two types of messages.

A) Synchronous

B) Asynchronous

2.3 What is the difference between Synchronous and Asynchronous Messaging Types?

Synchronous Messages:

Synchronous messaging involves a client that waits for the server to respond to a message. Messages are able to flow in both directions, to and from. Essentially it means that synchronous messaging is a two-way communication. i.e. Sender sends a message to the receiver and the receiver receives this message and gives a reply to the sender. The sender will not send another message until it gets a reply from the receiver.

Asynchronous Messages

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. Asynchronous Messaging means that it is a one-way communication and the flow of communication is one way only.

2.4 What are the types of messaging model do JMS provide?

It provides mainly two messaging models as below in the messaging world and these two are the commonly used.

A) Point-to-Point Model

B) Publish and Subscribe Model

Point-to-Point Model

The P2P messaging model consists of message senders, receivers, queues, and messages. A JMS client that generates messages is called the sender; a JMS client that consumes messages is called the receiver. In the P2P model, a sender sends a message to a destination called the queue; a receiver retrieves the message from the same queue

Publish and Subscribe Model

The pub/sub model consists of message publishers, subscribers, and topics. A message producer is called a publisher; a message consumer is called a subscriber. The destination where a publisher sends messages and the subscribers retrieve the messages is called the topic. The pub/sub model is based on the concept of nodes in a content hierarchy, where a publisher publishes messages to a destination and the messages are broadcast to all registered subscribers.

Read More

2.5 What is the difference between topic and queue?

A queue means a message goes to one and only one possible subscriber. A topic goes to each and every subscriber. Topics are for the publisher-subscriber model, while queues are for point-to-point. The same published message is received by all-consuming subscribers.

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

2.6 What is the use of the JMS provider?

A JMS provider is a messaging system that implements the JMS interfaces and provides administrative and control features. An implementation of the Java EE platform includes a JMS provider. JMS clients are the programs or components, written in the Java programming language, that produce and consume messages.

It also specifies the level of encryption, the security level of the message and the best-data type for the non-JMS client.

2.7 What are the components in JMS Architecture?

Message producers
Message consumers
JMS messages
Administered JMS objects
JNDI naming service

2.8 Tell an example of using point to point model in JMS?

2.9 What are the core JMS objects required to JMS-enabled application?

2.10 What is JMS administered object?

JMS administered object is a pre configured JMS object that is created by an administrator for the use of JMS clients and placed in JNDI namespace.

2.11 What are the important parts of the JMS application?

Connection
Session
Message
Message Producer
Message Consumer
Connection factory and destination

2.12 What is the JMS session and what are the uses?

A session is a single-threaded context for producing(producing) and consuming(receiving) messages. A JMS session could be a locally transacted, non-transacted or distributed transacted.

Uses:

A session serves several purposes:

It is a factory for its message producers and consumers.
It supplies provider-optimized message factories.
It supports a single series of transactions that combine work spanning its producers and consumers into atomic units.
It defines a serial order for the messages it consumes and the messages it produces.
It retains messages it consumes until it has been acknowledged.
It serializes the execution of message listeners registered with its message consumers.
A session can create and service multiple message producers and consumers.

2.13 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 and stores the messages permanently, while a non-durable subscription does not make any guarantees about messages sent by others when a client gets disconnected by others and these do not store the messages.

Note: We compared performance for durable and non-durable subscribers in two cases: persistent and nonpersistent 10k-sized messages. Both cases use AUTO_ACKNOWLEDGE acknowledgment mode. We found a performance impact only in the case of persistent messages, which slowed messages conveyed to durable subscribers by about 30%.

2.14 What is Byte Message?

A BytesMessage object is used to send a message containing a stream of uninterpreted bytes. It inherits from the Message interface and adds a bytes message body. The receiver of the message supplies the interpretation of the bytes.

The BytesMessage methods are based largely on those found in java.io.DataInputStream and java.io.DataOutputStream.

This message type is for client encoding of existing message formats. If possible, one of the other self-defining message types should be used instead.

Read More

2.15 Talk about different types of messages available in JMS API?

Different types of messages available in JMS API and those are
TextMessage, BytesMessage, StreamMessage, ObjectMessage, and
MapMessage.

A. TextMessage

TextMessage is used to store string values of any specific length. But this takes only String messages. This message type can be used to transport text-based messages, including those with XML content.

When a client receives a TextMessage, it is in read-only mode. If a client attempts to write to the message at this point, a MessageNotWriteableException is thrown. If clearBody is called, the message can now be both reads from and written to.

1
2
3
4
5
6
7
8
//Create a TextMessage
TextMessage sampleTextMsg = session.createTextMessage();
 
//Store values within TextMessage
sampleTextMsg.setText(“sample content”);
 
// Retrieve values from TextMessage
String storedText = sampleTextMsg.getText();

B. BytesMessage

01
02
03
04
05
06
07
08
09
10
//Create BytesMessage
BytesMessage sampleBytesMsg = session.createBytesMessage();
 
//Storing an array of numbers in BytesMessage
byte[] byteArray = new byte[]{66,68,70};
sampleBytesMsg.writeBytes(byteArray);
 
// Fetch the stored numbers by doing required casting
byte[] msgContent = new byte[3];
sampleBytesMsg.readBytes(msgContent);

C. StreamMessage

A StreamMessage object is used to send a stream of primitive types in the Java programming language. It is filled and read sequentially. The primitive types can be read or written explicitly using methods for each type.

01
02
03
04
05
06
07
08
09
10
11
12
//Create a StreamMessage
StreamMessage sampleStreamMsg = session.createStreamMessage();
 
//Store values within StreamMessage
sampleStreamMsg.writeBoolean(false);
sampleStreamMsg.writeBoolean(true);
sampleStreamMsg.writeBoolean(false);
 
// Retrieve values from StreamMessage
System.out.println(sampleStreamMsg.readBoolean());
System.out.println(sampleStreamMsg.readBoolean());
System.out.println(sampleStreamMsg.readBoolean());

D. ObjectMessage

Using ObjectMessage, an object can be wrapped as a message and transmitted across, but the object must be serializable.

01
02
03
04
05
06
07
08
09
10
11
//Create an ObjectMessage
ObjectMessage  sampleObjMsg = session.createObjectMessage();
 
//Create a ValueObject and initialize it with values
ValueObject vObj = new ValueObject(‘sampleField’,54);
 
//Store the ValueObject within ObjectMessage
sampleObjMsg.setObject(vObj));
 
//Retrieve the stored ValueObject from the ObjectMessage
vObj = (ValueObject) sampleObjMsg.getObject();

E. MapMessage

A MapMessage object is used to send a set of name-value pairs. The names are String objects, and the values are primitive data types of Java. The name should not be null.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
//Create a MapMessage
MapMessage sampleMapMsg = session.createMapMessage();
 
//Set Key Value Pair with type String
sampleMapMsg.setString('SampleKey', 'SampleValue');
 
// Retrieve String value using Key
sampleMapMsg.getString('SampleKey');
 
//Set Key Value Pair without specifying type
sampleMapMsg.setObject('SampleKey1', 'SampleValue1');
 
// Retrieve Object value using Key
sampleMapMsg.getObject(‘'SampleKey1');
  
2.16 Difference between the P2P(Peer to Peer) model and subscribe model?

The main difference is P2P is highly reliable and should be used in only one to one scenario but where a subscriber model can be used in one to many scenarios.

2.17 What is a JMS client?

JMS client is a core component in JMS and it is used to send or receive the messages. JMS supports two styles of messaging: the point−to−point and publish−and−subscribe messaging styles

2.18 Can we send e-mail as a message using JMS? Does JMS support Email as a message type?

No. JMS has no capability inherited support for it.

2.19 How JMS is different from RPC?

Java Messaging Service (JMS)

JMS is asynchronous in nature. When a message has to be transmitted, the Sender will send the message to the receiver. With this, the Sender’s job is done and it will continue with its further processing. The receiver will receive the message and continue with its own processing. No acknowledgment is sent from the receiver to the sender after receiving the message. This is because JMS is asynchronous.

There is no coupling between the sender and receiver of the message in JMS. Thus, JMS allows new senders and receivers to be added dynamically thereby managing variations in complexity over a period of time.  Hence, JMS is loosely coupled.

When there is an unexpected failure, JMS will store the messages that are pending to be delivered. When the system is up, the stored messages will be transmitted to the corresponding receivers.

Remote Procedure Call (RPC)

RPC is synchronous in nature. Unlike JMS, the invoker of the remote procedure call will invoke the method and continue to wait until the invoked method completes its execution. Once when the invoked method execution is complete, then the control returns back to the invoker. This is because RPC is synchronous in behavior.

RPC establishes tight coupling between the systems that interact with one another. When the invoker invokes a remote method, the invoker will get blocked until the response comes back to the invoker.

When there is an unexpected failure, the failure will impact the entire system. Thereby, the entire system will go down.

2.20 How to deliver a message to a non-java client in JMS?

If your Java code and the non-Java code can agree on a byte- or text-based message format, you can interchange the message between them. Yes, it can consume a java message.

3. Conclusion

In this article, We’ve seen the most commonly asked interview questions on JMS.

We’ll be adding a few more questions and answers.

If you know any questions apart from these, please post in the comments sections. We will add with answers to this article.

Published on Java Code Geeks with permission by Venkatesh, partner at our JCG program. See the original article here: Latest 20+ JMS Interview Questions and Answers (2020)

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button