In this article we will see how to run a JMS message to WebSphere 6.0.
First, open the WebSphere Administration Console and go to define a bus,
which is the layer logically interposed between producer and consumer, can be deployed on different machines or processors.
BUILDING BUS
On the console go
Integration Services> Bus> Insert a new bus, NOSTROBUS . ( not enable security).
After creating the Bus let's add our server as a member of the bus
( NOSTROBUS > members of the bus)
Once finished creating the bus going to define the destination, which is nothing but a transit point for any post in the bus.
DESTINATION CREATION
go on to define a destination:
Integration Services> bus > NOSTROBUS Destinations
New> Queue> Next> NOSTRADESTINAZIONE
After defining the target bus and let's create a JMS Connection Factory that will help us to retrieve the connection when reading and writing messages.
CREATION JMS connection factory
To do this we go:
Resources> JMS Providers> Default Messaging> JMS Connection Production> New
At this point we are going to enter the name of our Factory Connection NOSTRACF , the reference jndi JMS / NOSTRACF and select the bus NOSTROBUS
At this point we can go to define our Queue (Queue) :
CREATE QUEUE
To do this we go:
Resources> JMS Providers> Default Messaging> JMS Code> New
Now let's enter the name of our Coda NOSTRACODA , jndi reference
JMS / NOSTRACODA and select the bus NOSTROBUS and our destination NOSTRADESTINAZIONE .
At this point we have finished the configuration of our JMS queue, now let's see how to handle them.
Add a message on the queue
To post a message on the queue using
private void inviaMessaggio (String message) {
/ / First declare Connection, Session and MessageProducer of javax.jms.
Connection conn = null;
Session session = null;
MessageProducer mp = null;
try {/ / Get the InitialContext
InitialContext context = new InitialContext ();
/ / do the lookup of the ConnectionFactory by jndi
ConnectionFactory cf = (ConnectionFactory) contesto.lookup ("JMS / NOSTRACF");
/ / do the lookup via JNDI Destination Queue
Destination destination = (Destination) contesto.lookup ("JMS / NOSTRACODA) ;
/ / Create a connection from the Factory Connection conn =
cf.createConnection ();
/ / Create the session from the connection
conn.createSession session = (false, Session.AUTO_ACKNOWLEDGE)
/ / Create the producer passing as a parameter to our destination
session.createProducer mp = (target);
/ / Create message TextMessage message =
session.createTextMessage ();
/ / Put the message text
message.setText (message);
/ / Set the message destination
message.setJMSDestination (target);
/ / Set the mode on delivery (NON_PERSISTENT means that the message will be read only by an individual consumer and then deleted from the queue)
mp.setDeliveryMode (DeliveryMode.NON_PERSISTENT)
/ / Sending Message
mp.send (message);
System.out.println ("Message Sent! !!!");
} catch (Exception e) {
System.out.println ("Message Exception !!!!"+ e.getMessage ());
} finally {if (conn! = null) try { {
Session.close ();
mp.close ();
conn.Close ();
} catch (Exception e1) {
e1.printStackTrace ();}
}}}
Read a message from the queue
To read a message from the queue using
private String leggiCoda (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
/ / First, declare Connection, and Session of MessageConsumer javax.jms
Connection conn = null;
Session session = null;
MessageConsumer mc = null;
try {/ / Get the InitialContext
InitialContext context = new InitialContext ();
/ / do the lookup of the ConnectionFactory via JNDI
ConnectionFactory cf = (ConnectionFactory) contesto.lookup ("JMS / NOSTRACF" )
/ / do the lookup via JNDI Destination Queue
Destination destination = (Destination) contesto.lookup ("JMS / NOSTRACODA");
/ / Create a connection from the Connection Factory
cf.createConnection conn = ( )
/ / Create the session from the connection
conn.createSession session = (false, Session.AUTO_ACKNOWLEDGE)
/ / Creo parameter passing to the consumer as our destination
session.createConsumer mc = (target);
/ / Open the connection
conn.start ();
/ / Create a while loop to listen for messages in the queue
TextMessage message = null;
while (true) {Message m =
mc.receive (1);
/ / If there is a message we return the text.
if (m! = Null) {
message = (TextMessage) m;
System.out.println ("Message Received !!!!");
message.getText return ();}
}
} catch (Exception e) {
System.out.println ("Message Exception !!!!"+ e.getMessage ());} finally {
if (conn! = null) {try {
Session.close ();
mc.close ();
conn.Close ();
} catch (Exception e1) {
e1.printStackTrace ();}
}}
return null;}
0 comments:
Post a Comment