
JMS - API in Java EE Applications
Description
The JMS API can be used to create, send, receive and read message in the applications and has become integral part of Java EE platform. The Java EE applications use JMS API within EJB (Enterprise Java Beans) and web containers, which apply Java EE platform specification to Java EE components.
EJB is an essential part of a J2EE (Java 2 Enterprise Edition) platform, which develops and deploy the enterprise applications, by considering robustness, high scalability, and high performance. Web containers are used for the execution of web pages, which run on web servers like Jetty, Tomcat etc.
The following ways describe the use of JMS API in the J2EE application −
Administered Objects
These are preconfigured objects in the J2EE application, generated by an administrator to use with JMS clients. There are two types of administered objects; namely destination and connection factory. A destination is an object, that target its messages by JMS clients and receives the messages from the destination. The connection factory is an object, which establishes the connection between JMS client and Service provider. For more information, refer to the Programming Model chapter.
Using @Resource Annotation
It defines the name of the injected bean in Java EE applications and you can specify the JMS resource as static in an application client component. It can be specified as shown below −
@Resource(lookup = "jms/ConnectionFactory") private static ConnectionFactory connectionFactory; @Resource(lookup = "jms/Queue") private static Queue queue;
Resource Management
In the J2EE application, the JMS API resources contain JMS API connection and session. If you are using JMS API for an enterprise bean instance, then create the resource by using @PostConstruct callback method and close the resource by using @PreDestroy callback method.
The JMS API allows developers to create enterprise applications easily and defines the synchronous and asynchronous, reliable communications between J2EE components and other applications. The enterprise applications can be developed with new message-driven beans for defining business events along with existing business events.
Transactions
The bean method can send and receive the message by using the container-managed transactions, instead of using local transactions and manipulates the transaction separation with help of EJB container. Keep the occurrence of JMS operations and database access in a single transaction, by sending and receiving the messages in Java Transaction API (JTA) transactions. You don't need to use an annotation to specify the container-managed transactions, because they are default transactions in the Java EE applications.
Using Message-Driven Beans to Receive Messages Asynchronously
The message-driven bean is special type of enterprise bean supported by J2EE application, which processes the JMS messages asynchronously in the Java EE applications. The session bean sends and receives the JMS messages synchronously. The messages sent from client's application, enterprise bean, or a web component does not use Java EE technology.
The message-driven bean class contains below features −
This class uses the javax.jms.MessageListener interface to receive asynchronously delivered messages and onMessage method for moving the message to listener.
It creates a connection by using @PostConstruct callback method and closes the connection by using @PreDestroy callback method. Generally, this class uses these methods to produce the messages and receive the messages from another destination.