Lab 8 - Java Messaging and EJB Message Beans

INSTRUCTIONS FOR LAST YEAR'S LAB. THIS FILE NEEDS TO BE UPDATED

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: Part 1. Build and run the following Weblogic examples, and read and understand the package-summary.html file for each project.  Make sure you look at the code for each example and understand what it's doing:

~/weblogic/weblogic81/samples/server/examples/src/examples/ejb20/message (an EJB Message bean)
~/weblogic/weblogic81/samples/server/examples/src/examples/jms/topic (a pub-sub example)
~/weblogic/weblogic81/samples/server/examples/src/examples/jms/queue (a point-to-point example)
~/weblogic/weblogic81/samples/server/examples/src/examples/jms/messageformat (a more complex example that is relavant to our purposes for this lab)

To do this you will need to make a few modifications:
1. Copy ~arjuna/cspp-ta/51024/examples.properties to the following directory:
      ~/weblogic/weblogic81/samples/server/examples/src

2. Modify the contents of examples.properties:
     replace /home/arjuna with your home directory, /home/{your username}, where appropriate

3. Now go the the directories that are described above and execute the following command:
     (prompt)% ant

This will build the source files and place the complied files in the proper places.

4. Now you are ready to run the examples. Bring up a browser and type:
     http://{hostname}.cs.uchicago.edu:7001/

This will bring you to the index.jsp page for the running weblogic server. Follow the first link on the page and find the example you wish to run. Follow the directions that will instruct you to bring up new consoles (shells) to run the example code. Remember to navigate to the directory of the example before you execute the commands!

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").  Navigate down to examples:Services:JMS.  Then navigate to Servers under examples:Services:JMS.  Then, navigate to examplesJMSServer, and then to Destinations.  You will see several example destinations already present, including "exampleQueue", "exampleQueueReceive", "exampleQueueSend", "exampleTopic", and "quotes".  You are going to create a new message queue in weblogic.  To do this,  click on the "Destinations" node, and choose from the options on the right pane "Configure a new JMSQueue...".

For the Name, enter "mynewqueue".  For the JNDI Name, enter "weblogic.examples.jms.mynewqueue".  Accept all the other defaults and click on the "Create" button at the bottom right of the panel.  That's all there is to it, and you will have created a new JMS point-to-point message queue.

Part 3. Build, deploy and run the starter code. The source code is in /home/mark/pub/51024/myMessageBean/myMessageBean.tgz . Change to your directory:

~/weblogic/weblogic81/samples/server/examples/src/examples/jms/

Then execute this command:

tar xzvf /home/mark/pub/51024/myMessageBean/myMessageBean.tgz

Then, to run the example, type:

ant send

to generate 10 new messages onto the queue(s).  The queue that is used for sending the messages is the example queue already defined in weblogic:  exampleQueueSend.  The Producer creates 10 messages "Hello World-[date hash code]" and posts the messages onto the exampleQueueSend queue.  You can monitor this activity in the Console by selecting exampleQueueSend under "Destinations" and selecting "Monitor all active JMS destinations".

Basically, what the example application does is this:  Producer.java attaches to exampleQueueSend and sends messages to the queue.  A Message EJB is attached to exampleQueueSend (implemented in MyMessageBean.java) and it retrieves messages off the exampleQueueSend queue, and then turns right around at sends the message to the mynewqueue queue.  After running ant send, you will have 0 messages on exampleQueueSend (cause all 10 were removed by MyMessageBean) and you'll have 10 NEW messages just sitting on mynewqueue.  You can monitor this activity in the Console by selecting mynewqueue under "Destinations" and selecting "Monitor all active JMS destinations".

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 "exampleQueueSend".  An EJB Message bean, implemented in MyMessageBean, has an onMessage operation that is called for every message that appears on exampleQueueSend (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 21st, 2005.

Hints:

You will find the ~/weblogic/weblogic81/samples/server/examples/src/examples/jms/messageformat example most helpful.