…Contd
To check if the messages are distributed among the queues, we can enqueue some messages and check. Below is a sample code to enqueue some messages.
Enqueue Example
1: package com.redstacklogic.examples.jms;
2:
3: import java.util.Hashtable;
4:
5: import javax.jms.Queue;
6: import javax.jms.QueueConnection;
7: import javax.jms.QueueConnectionFactory;
8: import javax.jms.QueueSender;
9: import javax.jms.QueueSession;
10: import javax.jms.Session;
11: import javax.jms.TextMessage;
12:
13: import javax.naming.Context;
14: import javax.naming.InitialContext;
15:
16: /*
17: * @author: Rohith Puchalapalli
18: */
19: public class JMSEnqueuer {
20: public JMSEnqueuer() {
21: super();
22: }
23: public static void main(String[] args) throws Exception {
24: Hashtable env = new Hashtable();
25: env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
26: env.put(Context.PROVIDER_URL, "t3://localhost:7004,localhost:7003");
27: InitialContext ctx = new InitialContext(env);
28:
29: QueueConnectionFactory qcf =(QueueConnectionFactory)ctx.lookup("jms/MYJMSCF");
30: QueueConnection qc = qcf.createQueueConnection();
31: QueueSession qsession = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
32: Queue q = (Queue)ctx.lookup("jms/MYDISTQUEUE");
33: QueueSender qs = qsession.createSender(q);
34: TextMessage msg = qsession.createTextMessage();
35: msg.setText("Message hello world");
36: qs.send(msg);
37: qs.close();
38: qsession.close();
39: qc.close();
40: }
41: }
To Dequeue the messages we will use an MDB. Below is some sample code
MDB code
1: package com.redstacklogic.examples.jms;
2:
3: import java.util.Hashtable;
4:
5: import javax.jms.Queue;
6: import javax.jms.QueueConnection;
7: import javax.jms.QueueConnectionFactory;
8: import javax.jms.QueueSender;
9: import javax.jms.QueueSession;
10: import javax.jms.Session;
11: import javax.jms.TextMessage;
12:
13: import javax.naming.Context;
14: import javax.naming.InitialContext;
15:
16: /*
17: * @author: Rohith Puchalapalli
18: */
19: public class JMSEnqueuer {
20: public JMSEnqueuer() {
21: super();
22: }
23: public static void main(String[] args) throws Exception {
24: Hashtable env = new Hashtable();
25: env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
26: env.put(Context.PROVIDER_URL, "t3://localhost:7004,localhost:7003");
27: InitialContext ctx = new InitialContext(env);
28:
29: QueueConnectionFactory qcf =(QueueConnectionFactory)ctx.lookup("jms/MYJMSCF");
30: QueueConnection qc = qcf.createQueueConnection();
31: QueueSession qsession = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
32: Queue q = (Queue)ctx.lookup("jms/MYDISTQUEUE");
33: QueueSender qs = qsession.createSender(q);
34: TextMessage msg = qsession.createTextMessage();
35: msg.setText("Message hello world");
36: qs.send(msg);
37: qs.close();
38: qsession.close();
39: qc.close();
40: }
41: }
weblogic-ejb-jar.xml
1: <?xml version = '1.0' encoding = 'windows-1252'?>
2: <weblogic-ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3: xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-ejb-jar http://www.bea.com/ns/weblogic/weblogic-ejb-jar/1.0/weblogic-ejb-jar.xsd"
4: xmlns="http://www.bea.com/ns/weblogic/weblogic-ejb-jar">
5: <weblogic-enterprise-bean>
6: <ejb-name>TestMDB</ejb-name>
7: <message-driven-descriptor>
8: <destination-jndi-name>jms/MYDISTQUEUE</destination-jndi-name>
9: <connection-factory-jndi-name>jms/MYJMSCF</connection-factory-jndi-name>
10: </message-driven-descriptor>
11: </weblogic-enterprise-bean>
12: </weblogic-ejb-jar>
The messages are dequeued by both the servers
You can download the jdeveloper project from here RedStackLogicWLSQueueExamples.zip
PREV ….