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:~/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
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.