Lab 8 - Java Messaging and EJB Message Beans

In this lab you'll implement a Java Messaging client that will interface with a JMS Queue.  Remember that JMS supports two modes of messaging, publish-subscribe, which broadcasts the same message to any and all registered clients, and point-to-point messaging, which selects a single (one and only one) consumer to deliver a given message to, regardless of the number of consumers.  In effect, point-to-point messaging guarantees that each message is consumed by one and only one consumer, whereas the goal of publish-subscribe is to guarantee that all registered subscribers receive each and every message. More specifically the following example demonstrates: General documentation
Part 1. Study BEA Weblogic examples.
From $WL_HOME/samples/server/examples/src/examples/ejb/index.html: From $WL_HOME/samples/server/examples/src/examples/jms/index.html: Please, read and understand the documentation for each example.  Make sure you look at the code for each example and understand what it's doing.
Compile, deploy and run the examples. Remember to navigate to the directory of the example before you execute the commands!

The section "Developing a Basic JMS Application" from the documention can help you to understand the major parts of the code.

Part 2.  Create a new point-to-point message queue within your weblogic server.
Start the Weblogic Server Console as you did in the previous lab (http://localhost:7001/) and log in (default user and password are "weblogic").

Go to Services > Messaging > JMS Modules > examples-jms. Select Lock and edit.

Create a New JMS System Module Resource. Now you will have to click "New" and then select "Queue" from the list of resources.

For the Name, enter "mynewqueue".  For the JNDI Name, enter "weblogic.examples.jms.mynewqueue".   Select "examplesJMSServer" as subdeployment.

Part 3. Build, deploy and run the starter code. The source code is in /stage/classes/current/51024-1/pub/EJB/myMessageBean/.

To copy the code, change to the directory: $WL_HOME/samples/server/examples/src/examples/jms/. Then, copy the code:
$ cp -r /stage/classes/current/51024-1/pub/EJB/myMessageBean/ .

To run the example, type:

ant send

This will generate 10 new messages onto the queue.  The queue that is used for sending the messages is the example queue already defined in weblogic:  exampleQueue.  The Producer creates 10 messages "Hello World-[date hash code]" and posts the messages onto the exampleQueue queue.   You can monitor this activity in the console under JMS Servers > examplesJMSServer > Monitoring .
To see message details in a queue, go to Services > Messaging > JMS Modules > examples-jms. Then select a queue, go to Monitoring, select the destination and press "Show Messages". Now you should be able to pick a message and see the details.

Basically, what the example application does is this:  Producer.java attaches to exampleQueue and sends messages to the queue.  A Message EJB is attached to exampleQueue (implemented in MyMessageBean.java) and it retrieves messages off the exampleQueue queue, and then turns right around at sends the message to the mynewqueue queue.  After running ant send, you will have 0 messages on exampleQueue (cause all 10 were removed by MyMessageBean) and you'll have 10 NEW messages just sitting on mynewqueue.

Part 3.  Create your new java source file for retreiving point-to-point messages from your new queue mynewqueue.  This source file should be named "Consumer.java" and it should retrieve all messages from mynewqueue and print them out.:

Define the following static data strings in your code:

   public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
   public final static String JMS_FACTORY="weblogic.examples.jms.QueueConnectionFactory";
   public final static String QUEUE="weblogic.examples.jms.mynewqueue";

Then, put the Context.INITIAL_CONTEXT_FACTORY assigned to JNDI_FACTORY in your environment (see weblogic source code examples on how to do this).  Then set the context's PROVIDER_URL to be the Weblogic URL (generally passed in to your application, something like "t3://localhost:7001".

Then, define a new Context by calling new InitialContext (passing in the environment that you established above--again, see Weblogic example code on how to do this).  Next, use the context to look up the QUEUE:

queue = (Queue)context.lookup(QUEUE);

Next, create a QueueConnectionFactory by using the context to lookup the factory:

queueconnectionfactory = (QueueConnectionFactory)context.lookup(JMS_FACTORY);

Then, create a new WLQueueSession by making a createQueueSession QueueConnection object.  You can set the session to AUTO_ACKNOWLEDGE.

Next, create a QueueReceiver object like this:

      QueueReceiver queueconsumer = null;
      queueconsumer = queuesession.createReceiver(queue);

Finally, ask the queueconsumer for a new TextMessage by making a call with a 1000 millisecond timeout:

mytextmessage = (TextMessage)queueconsumer.receive(1000);

Then, just print out the TextMessage received from the queue and check again for the next message, and loop printing out messages found until you find no more messages on the queue, and then exit the Consumer.

Part 4.  Build everything and run it.

Add the following section of code to your build.xml file (just under the section for target name="send"):

  <target name="receive" depends="get_host">
    <java classname="examples.jms.mymessagebean.Consumer" failonerror="yes">
      <arg line="${myurl}"/>
    </java>
  </target>

Run:

ant

to rebuild everything.  Fix any typos or errors that occured.

Then, to run it all, type:

ant send

to generate 10 new messages onto the queue.  Then, type

ant receive

to consume all the messages from the queue.  Essentially, the overall dataflow that you will be creating will run like this:

Producer.java generates a message and sends it to the queue "exampleQueue".  An EJB Message bean, implemented in MyMessageBean, has an onMessage operation that is called for every message that appears on exampleQueue (to which it is bound).  onMessage() processes that message, and then turns right around and sends the message to mynewqueue.  Your new Consumer.java program will act as a point-to-point client to that queue, and will retrieve all messages on mynewqueue, and will print them out.  That's all there is to it. ;-)

Assignment:

Develop the above code and then tar up your resulting application and deliver it to the TA by 11:59 pm, Sunday, May 20th, 2007.

Hints:

You will find the  $WL_HOME/samples/server/examples/src/examples/jms/messageformat example most helpful.