Thursday, March 9, 2023

Top 16 JMS (Java Messaging Service) Interview Questions and Answers

Java messaging Service or JMS interview questions is one of the important parts of any Core Java or J2EE interview as messaging is a key aspect of enterprise Java development. JMS is a popular open-source Messaging API and various vendors like Apache ActiveMQ, Websphere MQ, Sonic MQ provides an implementation of Java messaging API or JMS. Any Java messaging services or JMS interview can be divided into two parts where the first part focuses on fundamentals of JMS API and messaging concept like What is topic, What is Queue, publish-subscribe or point to point model, etc, While the second part is related to JMS experience with particular JMS provider.

In this article, I will share the list of JMS interview questions, which not only help to answers some questions on interviews but also help to understand Java Messaging better.

Btw, If you are a new Java Messaging Service then I also recommend you to first go through a fundamental course like Java Messaging Service - JMS Fundamentals course by Bharat Thippireddy on Udemy. It covers everything a professional Java developer needs to know about JMS and it's also very affordable and you can buy in just $10 on Udemy sales.



16 JMS (Java Messaging Service) Interview Questions and Answers

Without wasting any more of your time, here is a list of 10 frequently asked JMS questions from Java Interviews. These questions are equally useful for both beginners and experienced programmers.

1. Difference between synchronous and asynchronous messaging? How do you do that in JMS?

As the name suggests synchronous messaging is synchronous, which means Sender waits for a response from the Receiver, instead of doing anything else. On the other hand in Asynchronous processing, Sender just sends the message and starts doing other things, instead of waiting for a response like maybe send another message. JMS supports both synchronous and asynchronous messaging





2. Difference between publish-subscribe and point-to-point messaging?

publish-subscribe or pub/sub and point to point or simply PTP, are two most used architecture in the messaging world. As there name suggest, if communication happens between two parties i.e. one sender and only one receiver then it's called a Point to Point messaging, while if you have many receivers, who are interested in a message sent by one producer, than you use a publish-subscribe model, where you use Topics, which is subscribed by each consumer.

Almost all messaging products support both forms of messaging in one way or another. Tibco Rendezvous supports point to point messaging as request/reply mode, while in JMS based messaging products you can simply use Queue for point to point messaging.

3. What is JMS administered object?

JMS administered objects are those objects which are managed i.e. created, updated, or destroyed by admins who manage JMS provider or Broker. Usually JMS provider like Tibco, Websphere MQ provides tools to create and manage these objects.

A common example of JMS administered objects are Connection Factories, Destination like Topic and Queue, which is created by messaging or middleware team and later accessed in Java programs using JNDI and JMS API.

Top 16 JMS (Java messaging Service) Interview Questions and Answers

4. Difference between Topic and Queue in JMS?

Topic and Queue are two kinds of destination, where a Sender sends messages. in Point to Pointe messaging, also known as PTP messaging, Sender and Receiver communicates using Queue, while on publish-subscribe messaging, also known as pub/sub messaging, Receiver subscribes on a Topic.

The topic is used, when more than one consumers are interested in receiving the same messages. In JMS, both Queue and Topic are interfaces, which extend Destination and subsequently encapsulate the name of the Topic or Queue.

Since Destination is a JMS administered object, the creation of Topic and Queue is usually governed by JMS Provider administrator. Usually messaging or middleware team, which administers broker, creates Topic and Queue on request of the application team and allows them to access using JNDI.

A JMS client can specify a Queue or Topic name while creating Message Producer or Consumer e.g. by using Session.createConsumer(Destination queueOrTopic) or Session.createProducer(Destination queueOrTopic).

5. What kind of message is supported by JMS API?

JMS Specification supports different types of messages e.g. BytesMessage, MapMessage, ObjectMessage, StreamMessage, and TextMessage, all these are sub interface of Message interface and implemented by JMS provider.

Also, a JMS message is composed of three parts Header, properties, and body. The only header is mandatory for a JMS message though. TextMessage is one of the most common message type, which is used among JMS clients. TextMessage is a wrapper around java.lang.String and provides convenient methods like getText() to get the message body's String. TextMessage is also used to transport XML content in most applications.

6. Does JMS support Guarantee message delivery?

JMS provides different options to guarantee message delivery like PERSISTENT and NON_PERSISTENT deliver mode to guarantee the message is not lost during transportation. IT also provides JMS provider to specify retention policy for Queue and Topic, which can retain messages until it acknowledged or expired, or till certain delivery attempt. durable subscription is another way to ensure your Receiver, receives a message even if it is inactive when the publisher publishes the message.


7. Difference between PERSISTENT and NON_PERSISTENT messages?

PERSISTENT messages are those which sent with delivery mode DeliverMode.PERSISTENT and similarly for the non-persistent messages. A client makes a message PERSISTENT if message loses in transit in not affordable, while a client marks a message NON_PERSITENT if occasional message loses is OK.

When JMS Provider receives a PERSISTENT message, it persists it to guarantee that it reaches the destination, even in the case of a broker going down. By the way, message retention at destination Queue or Topic is not governed by delivery mode, and it's set administratively. See Java Messaging Service - JMS Fundamentals for more details.


8. Which Acknowledge modes are supported by JMS API? How does CLIENT_ACKNOWLEDGE mode work?

JMS supports four kinds of acknowledging modes like AUTO_ACKNOWLEGE, CLIENT_ACKNOWLEGE, DUPS_OK_ACKNOWLEDGE, and SESSION_TRANSACTED acknowledgment mode.

With AUTO_ACKNOWLEDGE mode, JMS session automatically acknowledges a client's receipt of a message either when the session successfully returned from receive() method or the message listener session has called, which process message, returns.

The CLIENT_ACKNOWLEDGE mode provides you more control, where a message is acknowledged by calling acknowledge() method of the message itself, which means you can ACK message once you are done with processing it.

The message is not removed from the Queue, which is in Server until it acknowledged and redelivered if the client failed to acknowledge it. By the way, there is a catch, once you acknowledge a message, all messages are acknowledged and subsequently removed from Queue on Server.


9. If you have a transacted session what happens if you get an exception while processing a message?

If JMS Session is transacted, in which case it returns SESSION_TRANSACTED from getAcknowledgeMode(), acknowledgment mode is ignored.

In transacted sessions, commit() and rollback() methods are used for committing receipt of a message. A call to commit(), confirms receipt of all messages on that session.

Similarly, if an Exception is thrown from onMessage(), it would be considered as rollback, and the message will remain in Queue for delivery.


10. Do you need to acknowledge the message explicitly while using transacted JMS Session?

This is a follow-up of the previous JMS Interview Question. No, you don't need to acknowledge the message explicitly by calling the acknowledge() method if you are using a transacted JMS Session. As I said above, commit() and rollback() is used for acknowledging messages.

By the way, if you are using Spring JMS facilities such as AbstractMessageListenerContainer or DefaultMessageListenerContainer and have their sessionTransacted property as true, then every successful onMessage() execution will acknowledge the message and guaranteed redelivery in case of exception thrown.


11. How do you find if a message is redelivered by JMS provider?

JMSRedelivered property from Message header can be used by JMS provider to indicate redelivery of a Message. Some JMS Providers also use, properties like delivery Count to indicate the number of times a message is delivered if the delivery Count is more than 1 than it can be used to identify if messages have been redelivered or not. Having said that, it may possible that different JMS providers to indicate delivery count.

12. What happens if a durable subscriber is not running? will JMS Server discard the message?

A durable subscription is created by the subscriber to ensure that it receives all the messages published by the publisher, even during the period when the Receiver is inactive or down. JMS Provider or Broker will retain all messages published by a publisher for a durable subscriber until messages get acknowledge or expired.

Each durable subscriber is known by a unique name (Client id) and JMS Provider keeps a record of all these durable subscriptions to ensure message delivery, even if they are not active.

13. Does JMS Session object is thread-safe? Can we share JMS Session among multiple threads?

A Session object in JMS is a single-threaded context for creating message producers and consumers. It's not thread-safe, and not advised to share among multiple threads.


14. What is JMS Selector? How does it work? Any example of Using JMS Selector?

JMS Selector is a filtering facility provided by JMS Provider or broker. Suppose a client is only interested in listening to some specific messages and not all traffic from Server than it can either filter messages upon receiving, or it can use JMS selector to do the filtering on Broker itself. JMS Selector allows you to specify filtering criteria in the form of String, which is a subset of SQL conditional expression.

For example, if you are interested in messages which are for stocks starts with A, you can specific condition as SYMBOL like "A%", where SYMBOL is a user-defined Message property set by Sender.

Since JMS Selector can not select messages based upon the content of the message body, it's the sender's responsibility to populate an agreed Message Property for each message. Since selection is done on the broker side, a JMS selector is provided to the broker by the consumer while subscribing to a topic or listening to Queue.

After that, this consumer will only receive the message which passes the condition specified as part of the JMS selector string. JMS Selector can be a handy feature to implement load balancing, for example, you can create multiple instances of your application, each listening on a subset of stocks, specified by JMS Selector.

15. Suppose your application is communicating with another application using JMS and sending and receiving XML messages. What will you do to improve performance?

Well, large XML messages really consume a lot of bandwidth, when they are transferred from client to broker (JMS Provider) and further from broker to recipient. You can reduce this bandwidth by compressing messages before sending it on the client-side.

Since XML messages compress a lot, it's worth doing at the client-side with the expense of some CPU time. Java even provides a compression facility in form of java.util.zip package. But remember that compression and decompression on both the sender and receiver side must use the same algorithm.

Also, If you are using the PERSISTENT delivery mode, then messages are persisted on the server-side, before delivering to consumers, compressed messages take less time and space on the server-side during persistence.

16. Which JMS Provider have you worked with? Like IBM's WMQ, Apache ActiveMQ, Sonic MQ, etc

This messaging question is normally asked at the start of the interview, to get an idea of whether you have practical experience in JMS or not. If you have used JMS then, it must be via a JMS provider like Websphere MQ from IBM or Tibco EMS. You can answer this question based upon your experience with a particular JMS Provider.


That's all on this list of Java Messaging and JMS Interview Questions and Answers. Remember messaging is one of the very important topics in any Java interview and based upon your experience on different messaging technology like Tibco RV, Tibco EMS, Websphere MQ, you may be asked relevant questions.

Given JMS is a standard way of handling messages among Java application, good knowledge of JMS basics, messaging fundamentals, and prior experience in using JMS can really help you do to well in any Java J2EE interview.



Other Interview Questions Articles you may like to explore
  • 130+ Java Interview Questions from the last 5 years (questions)
  • 21 String Programming Interview Questions (questions)
  • Top 10 Spring Framework Interview Questions with Answers (see here)
  • 25 Software Design Interview Questions for Programmers (questions)
  • 50+ Data Structure and Algorithms Problems from Interviews (questions)
  • 10 Hibernate Interview Questions for Java EE developers (see here)
  • 20+ Linked List Interview Questions for Java developers (list)
  • Top 20 Java Design Pattern Questions asked on Interviews (see here)
  • 100+ Coding Problems and few tips to crack interview (problems)
  • Review these 50 Java questions before interviews (review)
  • 10 SQL Query Interview Questions (SQL)
  • 10 JDBC Interview Questions for Java Programmers (questions)
  • 75 Programming Interview Questions with solutions (questions)
  • 15 Java NIO and Networking Interview Questions with Answers (see here)
  • 15 Data Structure and Algorithm Questions from Java Interviews (read here)
  • Top 10 Trick Java Interview Questions and Answers (see here)
  • 50+ Java Collections Interview Questions (see here)
  • Top 40 Core Java Phone Interview Questions with Answers (list)
Thanks for reading this article so far. If you like these Java Generics and Collections interview questions then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

P.S. - If you are new to Java world or want to learn Java and looking for some online courses then I highly recommend The Complete Java Masterclass course by Tim Buchalaka on Udemy. It's the most up-to-date course and also covers everything a professional Java developer needs to know.  It's also very affordable and you can buy in just $10 on Udemy sales. 

No comments:

Post a Comment