Jms bridge is like a forwarder which is used between two different versions or domains of weblogic and is used to forward messages from one destination(queues) to another destination(queue)
To create a bridge between two weblogics W1 and W2 you will have to create two destinations.
Bridge source is going to be your source queue on W1 and destination is going to be the target queue on the W2 server.
Source destination looks like this
Q3 is my local queueu in W1.
Now create bridge destination
Now use this code to send message to the q3 on W1 and it should forward to queue4 on W2
////////////////////
import javax.jms.*;
import javax.naming.*;
public class Consumer {
/**
* Main method.
*
* @param args the destination used by the example
* and, optionally, the number of
* messages to send
*/
static java.util.Hashtable env;
public static void main(String[] args) {
final int NUM_MSGS;
/* if ((args.length < 1) || (args.length > 2)) {
System.out.println("Program takes one or two arguments: " +
"<dest_name> [<number-of-messages>]");
System.exit(1);
}*/
// String destName = new String(args[0]);
/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
Context jndiContext = null;
try {
env = new java.util.Hashtable();
env.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
System.out.println("-- ok 1 -- ");
/*env.put("java.naming.provider.url", "t3://10.105.98.8:7001");
env.put("java.naming.security.principal", "weblogic");
env.put("java.naming.security.credentials", "weblogic");
*/
env.put("java.naming.provider.url", "t3://localhost:7004");
env.put("java.naming.security.principal", "weblogic");
env.put("java.naming.security.credentials", "weblogic@123");
System.out.println("-- ok 2 -- ");
jndiContext = new InitialContext(env);
// jndiContext.lookup("weblogic.jndi.WLInitialContextFactory");
System.out.println(" jndiContext - " + jndiContext);
} catch (NamingException e) {
System.out.println("Could not create JNDI API context: " +
e.toString());
System.exit(1);
}
/*
* Look up connection factory and destination. If either
* does not exist, exit. If you look up a
* TopicConnectionFactory or a QueueConnectionFactory,
* program behavior is the same.
*/
ConnectionFactory connectionFactory = null;
Destination dest = null;
try {
System.out.println(" -- ok 3 -- ");
// System.out.println("Lookup done---------- " + obj);
Context ctx = new InitialContext(env);
//ctx.lookup("CreateVASErrorQ20");
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory1"); //Connection Factory JNDI
QueueConnection con = factory.createQueueConnection();
QueueSession session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup("Queue1"); //Find the Queue
QueueSender sender = session.createSender(queue); //Instantiating the message sender
TextMessage message = session.createTextMessage("Message sent again");
con.start();
sender.send(message);
System.out.println(" -- Message sent -- ");
// Now receiving the message:
int count = 0 ;
QueueReceiver receiver = session.createReceiver(queue);
while(true) {
TextMessage receivedMessage = (TextMessage)receiver.receiveNoWait();
if(receivedMessage != null) {
if(receivedMessage instanceof javax.jms.TextMessage) {
System.out.println("Received message = " + receivedMessage);
System.out.println(" -- Message Received -- "+receivedMessage.getJMSMessageID());
count++;
} /*else {
break;
}*/
}
}
// System.out.println(" -- Message Received -- "+receivedMessage.getJMSMessageID());
/*connectionFactory = (ConnectionFactory) jndiContext.lookup("PrepaidActValDS");
dest = (Destination) jndiContext.lookup(destName);
System.out.println( "dest "+ dest);*/
} catch (Exception e) {
System.out.println("JNDI API lookup failed: " + e.toString());
e.printStackTrace();
//System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create producer and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
// Connection connection = null;
// MessageProducer producer = null;
// try {
// connection = connectionFactory.createConnection();
//
// Session session =
// connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// producer = session.createProducer(dest);
//
// TextMessage message = session.createTextMessage();
//
// for (int i = 0; i < NUM_MSGS; i++) {
// message.setText("This is message " + (i + 1));
// System.out.println("Sending message: " + message.getText());
// producer.send(message);
// }
//
// /*
// * Send a non-text control message indicating end of
// * messages.
// */
// producer.send(session.createMessage());
// } catch (JMSException e) {
// System.out.println("Exception occurred: " + e.toString());
// } finally {
// if (connection != null) {
// try {
// connection.close();
// } catch (JMSException e) {
// }
// }
// }
}
}
To create a bridge between two weblogics W1 and W2 you will have to create two destinations.
Bridge source is going to be your source queue on W1 and destination is going to be the target queue on the W2 server.
Source destination looks like this
Q3 is my local queueu in W1.
Now create bridge destination
Now use this code to send message to the q3 on W1 and it should forward to queue4 on W2
////////////////////
import javax.jms.*;
import javax.naming.*;
public class Consumer {
/**
* Main method.
*
* @param args the destination used by the example
* and, optionally, the number of
* messages to send
*/
static java.util.Hashtable env;
public static void main(String[] args) {
final int NUM_MSGS;
/* if ((args.length < 1) || (args.length > 2)) {
System.out.println("Program takes one or two arguments: " +
"<dest_name> [<number-of-messages>]");
System.exit(1);
}*/
// String destName = new String(args[0]);
/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
Context jndiContext = null;
try {
env = new java.util.Hashtable();
env.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
System.out.println("-- ok 1 -- ");
/*env.put("java.naming.provider.url", "t3://10.105.98.8:7001");
env.put("java.naming.security.principal", "weblogic");
env.put("java.naming.security.credentials", "weblogic");
*/
env.put("java.naming.provider.url", "t3://localhost:7004");
env.put("java.naming.security.principal", "weblogic");
env.put("java.naming.security.credentials", "weblogic@123");
System.out.println("-- ok 2 -- ");
jndiContext = new InitialContext(env);
// jndiContext.lookup("weblogic.jndi.WLInitialContextFactory");
System.out.println(" jndiContext - " + jndiContext);
} catch (NamingException e) {
System.out.println("Could not create JNDI API context: " +
e.toString());
System.exit(1);
}
/*
* Look up connection factory and destination. If either
* does not exist, exit. If you look up a
* TopicConnectionFactory or a QueueConnectionFactory,
* program behavior is the same.
*/
ConnectionFactory connectionFactory = null;
Destination dest = null;
try {
System.out.println(" -- ok 3 -- ");
// System.out.println("Lookup done---------- " + obj);
Context ctx = new InitialContext(env);
//ctx.lookup("CreateVASErrorQ20");
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory1"); //Connection Factory JNDI
QueueConnection con = factory.createQueueConnection();
QueueSession session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup("Queue1"); //Find the Queue
QueueSender sender = session.createSender(queue); //Instantiating the message sender
TextMessage message = session.createTextMessage("Message sent again");
con.start();
sender.send(message);
System.out.println(" -- Message sent -- ");
// Now receiving the message:
int count = 0 ;
QueueReceiver receiver = session.createReceiver(queue);
while(true) {
TextMessage receivedMessage = (TextMessage)receiver.receiveNoWait();
if(receivedMessage != null) {
if(receivedMessage instanceof javax.jms.TextMessage) {
System.out.println("Received message = " + receivedMessage);
System.out.println(" -- Message Received -- "+receivedMessage.getJMSMessageID());
count++;
} /*else {
break;
}*/
}
}
// System.out.println(" -- Message Received -- "+receivedMessage.getJMSMessageID());
/*connectionFactory = (ConnectionFactory) jndiContext.lookup("PrepaidActValDS");
dest = (Destination) jndiContext.lookup(destName);
System.out.println( "dest "+ dest);*/
} catch (Exception e) {
System.out.println("JNDI API lookup failed: " + e.toString());
e.printStackTrace();
//System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create producer and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
// Connection connection = null;
// MessageProducer producer = null;
// try {
// connection = connectionFactory.createConnection();
//
// Session session =
// connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// producer = session.createProducer(dest);
//
// TextMessage message = session.createTextMessage();
//
// for (int i = 0; i < NUM_MSGS; i++) {
// message.setText("This is message " + (i + 1));
// System.out.println("Sending message: " + message.getText());
// producer.send(message);
// }
//
// /*
// * Send a non-text control message indicating end of
// * messages.
// */
// producer.send(session.createMessage());
// } catch (JMSException e) {
// System.out.println("Exception occurred: " + e.toString());
// } finally {
// if (connection != null) {
// try {
// connection.close();
// } catch (JMSException e) {
// }
// }
// }
}
}
No comments:
Post a Comment