Thursday, July 16, 2020

# EJB & JMS Basic Knowledge #


1) What is EJB?
EJB stands for Enterprise Java Bean. It is a server-side component to develop scalable, robust and secured enterprise applications in java.

2) What are the types of Enterprise Bean?

There are three types of enterprise bean in java.
1.       Session Bean
2.       Message Driven Beam
3.       Entity Bean

3) What is session bean?

Session Bean encapsulates business logic. It can be invoked by local, remote or web service client.
There are 3 types of session bean.
  1. Stateless Session Bean
  2. Stateful Session Bean
  3. Singleton Session Bean

4) What is stateless session bean?

A Stateless session bean is a business object that doesn't maintain the conversational state with the client.
5) Write down the steps for the creation of stateless EJB.
  • Create a local interface.
  • The interface is to be used by the client application.
  • In case the EJB client environment is the same, use @Local annotation.
  • In case the EJB client environment is different, use @Remote annotation.
  • Create a stateful session bean.
  • To signify a stateful bean, use @Stateful annotation.

6) What is stateful session bean?

A Stateful session bean is a business object that maintains the conversational state with the client

7) What is singleton session bean?

Singleton session bean is instantiated only once for the application. It exists for the life cycle of the application.

8) What is JMS?

Java Message Service is a messaging service to create, send and receive messages asynchronously.
9) What are the advantages of JMS?
  • Asynchronous
  • Reliable

10) What is PTP model?

In Point to Point model, one message is delivered to one receiver only. Here, Queue is used as a message-oriented middleware.

11) What is Pub/Sub model?

In the Publisher/Subscriber model, one message is delivered to all subscribers. Here, Topic is used as a message-oriented middleware

12) What is MDB?

Message Driven Bean (MDB) encapsulates business logic. It is invoked by passing the message. It is like JMS receiver.

13) What is Entity Bean?

Entity Bean is a server-side component that represents the persistent data. Since EJB 3.x, it is replaced by JPA

14) What is Session Facade?

Session Facade is a design pattern to access enterprise bean through the local interface. It abstracts the business object interactions and provides a service layer. It makes the performance fast over the network.
15) List some key actors in persistence API.
Some of the key actors in persistence API are:
  • Entity
  • EntityManager
  • Persistence unit
  • Data source

16) List down the steps for the demonstration of EJP persistence mechanism.
  • Create a database table
  • Create entity classes for the table
  • Create persistent unit and data source
  • Create a stateless EJB
  • Update stateless EJBM
  • Console based application accesses the stateless EJB.

17) Name the attributes of javax.ejb.Stateful.
·         Name
·         mappedName
·         Description

18) Name the attributes of javax.ejb.EJB.

·         beanInterface
·         beanName
·         mappedName

19) Mention the three levels for applying interceptor methods.

·         Default
·         Class
·         Method

20 Mention the Java types that can be mapped using the @Lob annotation.
  • java.sql.Blob
  • String
  • byte[]
  • java.sql.Clob
  • Serializable Object


21) Name the ACID properties of a transaction.
  • Atomic
  • Consistent
  • Isolated
  • Durable
What is Message Driven Bean?
Message Driven Bean (MDB) is an enterprise bean which runs inside the EJB container and it acts as Listener for the JMS asynchronous message . It does not have Home and Remote interface as Session or Entity bean. It is called by container when container receives JMS asynchronous message.
MDB has to implement MessageListener which has a method onMessage (Message msg). When the container calls the MDB it passes the message to onMesage() method and then MDB process that message.

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 in order 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 model 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 with respect to 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 between the way in which 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.
While 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 a 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 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 #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 and will 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 #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 and will 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: 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 basically a component written in the Java programming language which 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 which 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, message as arguments or parameters.
Q #26) What is the functionality of JMS message Consumers?
Answer: A message consumer is basically a component which 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 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 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. In order 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 basically an API that is responsible for filtering the messages which it receives for any particular application. Message Selectors actually assign the job to 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 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: 
Glassfish server is actually an application server and can be also used as web servers which means it can handle HTTP requests from the 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!!


No comments:

Docker Swarn

Docker is a tool intended to make the process of creating, deploying and running applications easier by using container based virtualization...