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
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.