Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Understanding Java Messaging System

In this article we will see an overview about the one of the best messaging systems used in java and j2ee application, the Java Messaging system.

Introduction to Java Messaging System

Message-oriented-middleware (MOM) is the term used to describe an implementation of a messaging system. There are many different types of implementations, from any number of different vendors. We will see here the overview on Java Messaging System (JMS).

Similar to JDBC, the JMS API abstracts the interactions across MOM implementations and vendors. A JMS-compliant messaging system can exist as stand-alone implementations of the JMS API. Or, the messaging system could be a proprietary implementation with a set of JMS-compliant API interfaces. Messaging systems are typically used to achieve a loosely-coupled architecture in distributed systems. In a loosely-coupled distributed system, the participants involved in the communication can function independent of one another, without knowing their type, location, or implementation. Additionally, messaging systems can be used to provide asynchronous communication between participants.

Five Messaging Key Concepts:

There are five key concepts involved in a messaging solution:

  • Message: A message encapsulates data passed during the communication. The message can be as simplistic as a String of text or as complex as an Object composition. One way to think about a message is as an envelope found in the traditional mail system. Similar to traditional mail, the message has a destination, delivery instructions, and contents, like a letter.
  • Message Producer: Message producers are the entities in a messaging solution that generate messages. A message producer could be a stand-alone Java application, a web-tier component like a servlet, an EJB-tier component like a session bean, or even a legacy system. In the mail analogy, the producer addresses the envelop, stuffs its contents with information, and hands it off to the postal service for delivery.
  • Messaging System: The messaging system is the one globally known entity in a loosely coupled distributed messaging system. Both message producers and message consumers know how to interact with the messaging system. It is the messaging system’s responsibility to route and deliver a message to the appropriate destination. In the mail analogy, the messaging system is the postal service. In order for the envelope to be routed and delivered by the postal service, both the message producer and the message consumer need a connection to the postal service. While the message is being transmitted from the sender to the postal service and the postal service to the consumer, it is involved in a session. When the postal service receives an envelope, it delivers the envelope to the appropriate destination (the mailbox).
  • Destination: In a messaging system, the destination describes the delivery location for a message. How the destination is implemented and where it physically resides is irrelevant to the producer of the message. In the mail analogy, the destination of an envelope would be an address, and the physical repository of the envelope at that destination could be a mailbox, a post office box, or something else. Message consumers are the end recipients of the message. Typically, message consumers digest the message by retrieving the message from the physical location and performing some application-specific business processing. In the mail analogy, the message consumer is the recipient of the envelope. After the recipient retrieves the envelope from the mailbox, the recipient opens the envelope and reads the letter.
Flow of a Message through JMS

Figure 1: Flow of a Message through JMS

This exercise assumes the application server is installed and running. Tool Reference is used in this exercise include:

  • Server Resources: Messaging: JMS Resource
  • Server Resources: Java EE Application Servers: Administrating JMS: Creating Physical Destinations
  • Server Resources: Configuring Java EE Resources: Configuring JMS Connection Factories
  • Server Resources: Configuring Java EE Resources: Configuring JMS Destination Resources

Task 1 - Create Server Resources

In this task, you will use the NetBeans IDE to create the necessary server resources. Specifically, you will create a ConnectionFactoryand a Destination.

Note: Setting up JMS resources can also be performed through the server administration console. Refer to the GlassFish documentation for more information.

Complete the following steps:

Create a new JMS Physical Destination. Right-click the EJB31 project and select New > Other

In the New File dialog, (Step 1 of 3):

  • Choose the GlassFish category.
  • Select JMS Resource from the File Types.

In the New JMS Resource dialog (Step 2 of 3):

  • Specify jms/HelloWorldQueueas the JNDI Name,
  • Select javax.jms.Queue from Admin Object Resource.
  • Click the Next button.

In the JMS Properties view (Step 3 of 3):

  • Specify HelloWorldQueue as the Value
  • Click the Finish button.
  • Create a new JMS Connection Factory.

Right-click the EJB31 project and select New > Other

In the New File dialog, (Step 1 of 3):

  • Choose the GlassFish category.
  • Select JMS Resource from the File Types.

In the New JMS Resource dialog (Step 2 of 3):

  • Specify jms/ HelloWorldQueueFactor y as the JNDI Name.
  • Select javax.jms.QueueConnec tionFactory as theConnection Resource. Click the Finish button. Build and Deploy the EJB31application

Task 2 - Verify the JMS Resources

In this task, you will verify the JMS resources were created properly. You can either log in to the administration console of GlassFish or perform the verification using NetBeans. Complete the following steps:

Verify that the EJB31application deployed properly.

Click the Services tab.

  • Expand Servers > GlassFish v3 Domain > Resources > Connectors.
  • To verify that the JMS Queue was created, expand Admin Object Resources. You should see jms/ HelloWorldQueue
  • To verify that the JMS Connection Factory was created, expand Connector Connection Pools. You should see jms/HelloWorldQueueFactory.

Note: If you do not see the JMS resources, try restarting and redeploying your application.

Write a JMS Message Producer:

In this exercise, you will create a Java EE application client container application. The client application will function as a JMS Message producer. In the next Lab “Developing Message-Driven Beans,” you will write a JMS message consumer as a message-driven bean. This exercise contains the following sections:

  • “Task 1 -Create a Message Producer Application”
  • “Task 2 -Modify the MessageProducerApp”
  • “Task 3 -Run the Application”

Task 1 - Create a Message Producer Application

Complete the following steps:

Right-click EJB31-app-client and select New > Java Main Class

  • Class Name: MessageProducerApp
  • Package: ejb31 2. Edit MessageProducerApp.java.

Edit MessageProducerApp.java. Right-click in the editor within the main method and select Insert Code > Send JMS Message

In the Send JMS Message Dialog, select the following options:

  • Server Destinations: jms/ HelloWorldQueue
  • Connection Factory: jms/ HelloWorldQueueFactor y

Modify the @Resource annotations; change mappedNameto lookup. @Resource(lookup = "jms/ HelloWorldQueue")

Modify sendJMSMessageToHelloWorldQ ueueand createJMSMessageForjmsHello WorldQueueto be static.

Review the generated methods sendJMSMessageToHelloWorldQ ueueand createJMSMessageForjmsHello WorldQueue.

Replace the logger output with System.outor System.errcalls. System.err.println("Cannot close session -" + e);

Task 2 - Modify the MessageProducerApp

In this task, you will modify the MessageProducerAppto invoke the sayHellomethod of the SingletonHelloWorldBean. The GreetingRequestreturned from the bean will be used as the message body. Complete the following steps:

Add a reference to the EJB.

  • Right-click in the editor and select Insert Code > CallEnterprise Bean
  • Select SingletonHelloWorldBeanfrom the list.

Add the following logic to the mainmethod:

  • Within the main method, invoke the sayHellomethod of the bean. GreetingRequest result = singletonHelloWorldBean.say Hello();
  • Import the helloworld.vo.GreetingRequest; import helloworld.vo.GreetingRequest;
  • Convert the GreetingRequestinto a String. String resultStr = result.toString();
  • Invoke the sendJMSMessageToHelloWorldQ ueuemethod. try { sendJMSMessageToHelloWorl dQueue(resultStr); System.out.println ("MessageProducerApp.main: sent message -" + resultStr); } catch (JMSException ex) { System.err.println (“MessageProducerApp.main: failed to send message -” + ex);

Task 3 -Run the Application

In order to run the application, you will need to change the Run properties of the EJB31-app-clientmodule. Complete the following steps:

Change the run configuration.

  • Right-click EJB31-app-clientand select Properties.
  • Select Run in the Categories pane.
  • Modify the Main Class to ejb31.MessageProducerApp.
  • Click the OK button. 2. Run the application.

You should see output similar to the following:

MessageProducerApp.main: sent message -helloworld.vo.GreetingRequest[requestTime=11/9/09 11:10 AM,greeting=Good Morning]

Conclusion:

The purpose of this section is to provide an overview of the JMS API technology required to develop a simple stand-alone messaging producer. In the above module, you learned that how to create asynchronous message consumers using message-driven beans.



Have good knowledge on Java, HTML, CSS and Android platform and is pursuing Masters in Computer Applications.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login