Browse DevX
Sign up for e-mail newsletters from DevX


JMS Enables Concurrent Processing in EJB : Page 3

JMS provides an elegant solution for overcoming the restrictions you face when developing concurrent applications with EJB. Learn how its asynchronous model and its support for the MessageDrivenBean can enable a client to use EJB asynchronously.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

JMS Design Implementation
Now it's time to see how you can use the client- and server-side designs and develop a simple implementation for sending subtasks to the SubtaskMessageBean. You need to implement only the client side. For the server side, you do not need to implement any new classes.

The SimpleTaskManager class (see Listing 5) extends JMSTaskManager and provides implementation for all the abstract methods (see Figure 3). As explained previously, the onMessage() method gets acknowledgement messages for finished subtasks from the SubtaskMessageBean, and it simply prints the TextMessage the SubtaskMessageBean sends. All other methods provide JMS Queue-specific details (such as Queue Name, Queue Factory Name, etc.). By implementing all these methods in SimpleTaskManager, you make the JMSTaskManager generic, providing default behavior for the subtask management.

Figure 3: SimpleTaskManager Class Extends JMSTaskManager

The getMessageSelector() method returns the current date as the Message Selector so that the JMSTaskManager can use it to filter the acknowledgement messages from SubTaskMessageBean.

The createMessage() method creates a TextMessage, sets the String Subtask, and calls the setStringProperty() to pass the current date as the message Selector.

The DemoClient class (see Listing 6) constructs the SimpleTaskManager and passes a collection of subtasks to the sendSubTasks(). Then it calls listenForAcknowledgement() and waitForAcknowledgement() to listen and wait for the acknowledgement messages from the SubtaskMessageBean.

Running the Implementation in WSAD 5.1
This section discusses how you can run the DemoClient and SubtaskMessageBean in WSAD 5.1. (Click here for more detailed discussions about setting up JMS queues and running MDBs.)

Set Up Client-side Classes
In WSAD 5.1, from a J2EE perspective:

  1. Choose File, New, and then Project from the pop-up (see Figure 4).
  2. Choose J2EE and select Application client Project.
  3. Press Next, choose J2EE 1.3 Application client Project, and press Next twice.
  4. Type the project name as DemoClient and type DemoClientEAR for EAR Project.
  5. Press Finish to close the pop-up.

The above steps create a DemoClient and DemoClientEAR projects in WSAD, which you can see in Project Navigator. Open the DemoClient project and select appClientModule. Right mouse click to open the pop-up and choose Import. From the Import pop-up, select Zip file (see Figure 5) and press Next. In the file location specify the location of DemoClient.zip (included in the code download for this article) and press Finish. This will import the all the client-side Java classes and place them in a com.cybelink.client package. You can see all the classes in the package com.cybelink.client in the Project Navigator window in a J2EE perspective. You will also see a red x mark on the SimpleTaskManager class.

Open SimpleTaskManager and you'll notice the compiler complaining about missing com.cybelink.mdb.QueueConstants. You'll resolve this when you finish importing server-side classes.

Figure 4: WSAD 5.1 from a J2EE Perspective

Figure 5: The Import Pop-up

Now you need to add the class name in the client Deployment Descriptor so that you can call the main() method in that class during DemoClient startup. Open the MANIFEST.MF under META-INF in the DemoClient project (see Figure 6). Press the Browse button to select DemoClient for the Main-Class, and save the changes. These steps help WSAD 5.1 identify the class name that contains the main method.

Figure 6: The Import Pop-up

After importing all the client-side classes, the next step is creating an application client configuration that will start the DemoClient application. From the J2EE perspective, choose Run in the Main Menu and from the sub menus choose Run again to open a pop-up. In the pop-up's configurations options on the left panel (see Figure 7), choose WebSphere v5 Application client and press New. In the name field, change the configuration from New_configuration to DemoClient and press Apply. Make sure that DemoClientEAR is selected for the Enterprise Application and DemoClient for Application client. Select Classpath and press Add External JARs.

Figure 7: The Pop-up's Configurations Options

In the Jar Selection pop-up, choose the com.ibm.mq.jar and com.ibm.mqjms.jar files (see Figure 8) and press Open. These two Jar files are located in your \Java\lib directory. Press Apply and press Close to close the pop-up.

Figure 8: The Pop-up's Configurations Options

You have created a Java client project, imported the necessary Java classes, and added the necessary Jar files so that DemoClient can start within WSAD 5.1 and communicate with the WebSphere MQ. Your next step is to create a project for server-side SubtaskMessageBean so that WSAD 5.1 can start it.

Set Up Server-side Classes
Setting up server-side classes is a two-step process. First, you create a project for the server-side classes and then you create a server and server configuration to setup various JMS configurations.

Create a Project for MDB
In the J2EE perspective from the File menu, choose New and choose EJB Project from the submenus. In the pop-up window, choose Create 2.0 EJB Project and press Next (see Figure 9). Enter SimpletaskMDB for Project Name, select DemoClientEAR for EAR Project, and press Finish. These steps create a SimpletaskMDB EJB project, which you can see in the J2EE perspective.

Figure 9: New EJB Project Pop-up Window

Open the SimpletaskMDB project, choose ejbModule, and right mouse click to open a pop-up window. From that window (see Figure 5), select Import and choose Zip File. Enter the SimpletaskMDB.zip (included in the code download for this article) location to import the SimpletaskMDB and QueueConstants Java classes into WSAD 5.1.

Now you can fix the DemoClient Project's missing QueueConstants reference in SimpleTaskManager. Select DemoClient project and right mouse click. From the opened pop-up window, select Properties option. In the window (see Figure 10), choose the Java Build Path option on the left panel. On the right, choose the Projects tab, select the SimpletaskMDB project, and press OK. This enables the classes in the DemoClient project to access the classes in the SimpletaskMDB project. Youll see the compiler errors cleared from SimpleTaskManager.

Figure 10: Choose the Java Build Path Option

Now you are ready to configure the SimpleTaskMessageBean in ejb-jar.xml so that it can listen for incoming messages in WebSphere MQ.

Open the ejb-jar.xml file, which is under META-INF in the project SimpletaskMDB, and then go to the Beans tab. In the Beans page, press the Add button to open the pop-up (see Figure 11), select Message-driven bean, and enter SubtaskMessage for the Bean name. Press Next to select Queue for Destination Type (see Figure 12) and enter subtaskListenerQ for ListenerPort Name. Press Finish to close the pop-up.

Figure 11: Press the Add Button to Open the Pop-up

Figure 12: Press Next to Select Queue for Destination Type

You have modified the ejb-jar.xml file to include the SubtaskMessageBean configuration parameters.

Server and Server Configuration Creation
In WSAD 5.1, switch to the Server perspective. From the Server window, select Servers and right mouse click to open the pop-up. From the pop-up, choose New and then choose Server and Server Configuration to open pop-up (see Figure 13).

Figure 13: Choose Server and Server Configuration to Open Pop-up

For the Server Name type EJBConcurrency and for the Server type select Test Environment. Press Finish to close the pop-up. These steps create a new Server EJBConcurrency, which you can see in the Server Configuration window. Right mouse click the EJBConcurrency and select Add and remove Projects. From the pop-up (see Figure 14) choose DemoClientEAR from the Available Projects and press Add. Then press Finish to add DemoClientEAR to EJBConcurrency Server.

Figure 14: Choose DemoClientEAR from the Available Projects

Your next task is to configure the EJBConcurrency server so that WebSphere MQ can start the JMS queues. In the Server Configuration open EJBConcurrency in the editor and press the JMS tab to open the WebSphere MQ configuration screen. In JMS Server Properties under Server Settings (see Figure 16), click the Add button to enter Queue Name SubtaskSendQueue. Similarly enter Queue Name SubtaskReceiveQueue. In the JMS Provider options, select Embedded Messaging. In the WASQueueConnectionFactory entries, press the Add button to open the pop-up window (see Figure 15). In the Name field, enter QConnectionFactory. For JNDI Name, enter jms/QConnectionFactory and press OK to close the pop-up.

Figure 15: Press the Add Button to Open the Pop-up

Figure 16: The EJBConcurrency Server After All the Changes

In the WASQueue entries, press Add to open the pop-up window. Enter SubtaskSendQueue for Name and jms/SubtaskSendQueue for the JNDI name. Similarly, add SubtaskReceiveQueue and jms/SubtaskReceiveQueue. After all these changes your server setting will look like Figure 16.

Now press the EJB tab to enter Listener Port information for the SubtaskMessageBean. Press the Add button to open the pop-up window (see Figure 17) and enter subtaskListenerQ for Name, select jms/QConnectionFactory for Connection Factory JNDI Name, and select jms/SubtaskSendQueue for Destination for JNDI Name. Leave the rest of the fields as is. Press OK to close the window and save all your configuration changes.

Figure 17: The EJBConcurrency Server After All the Changes

After all the preceding setup steps, you are now ready to run your client and server programs in WSAD 5.1.

Run the Client and Server Programs
First you need to start the server so that when you start DemoClient it can send JMS messages to the SubtaskMessageBean.

From the Server perspective, select EJBConcurrency, right mouse click to open the pop-up, and select Start to start the EJBConcurrency Server. These steps start the SubtaskMessageBean, which listens for JMS Messages in the subtaskListenerQ port to process the subtasks the DemoClient sends.

Switch to the J2EE perspective. Open the Run menu (see Figure 7), choose DemoClient, and press Run to have the DemoClient send subtasks to SubtaskMessageBean.

The DemoClient (see Figure 18) communicates with the SimpleTaskManager, which sends the JMS messages to the SubtaskSendQueue. The SubtaskMessageBean listens in the port subtaskListenerQ for JMS messages that come into the SubtaskSendQueue. Then it retrieves the Subtask, processes it, and sends a JMS acknowledgement to SubtaskReceiveQueue. The SimpleTaskManager that sends the JMS subtask will get the acknowledgement from the SubtaskReceiveQueue and notify the DemoClient. Figure 13 shows two DemoClients started concurrently, each with its own SimpleTaskManager sending subtasks to SubtaskSendQueue and receiving acknowledgements from the SubtskReceiveQueue.

Figure 18: The DemoClient

Figure 19 and Figure 20 show the WSAD 5.1 Console window for DemoClient and Server, respectively.

Figure 19: The WSAD 5.1 Console Window for DemoClient

Figure 20: The WSAD 5.1 Console Window for Server

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date