一、初始化
String CTX_FACTORY="com.sun.fscontext.RefContextFactory";
String INIT_URL = TAppServerInit.getString("bindingURL","file:/d:/IBM/WMQ/java/bin"); //从配置文件中取得
java.util.Hashtable env=new java.util.Hashtable();
// 初始化JNDI TREE
env.put(Context.INITAL_CONTEXT_FACTORY,CTX_FACTORY);
env.put(Context.PROVIDE_URL,INIT_URL);
Context ctx=new InitiaContext(env);
二、根据名称得到队列连接工厂 队列
QueueConnectionFactory qcf=(QueueConnectionFactory)ctx.lookup("qcf_name");
Queue queue=(Queue)ctx.lookup("q_name");
三、根据队列连接工厂和队列创建连接 再用连接创建session
QueneConnection qc=qcf.createQueneConnection();
QueueSession session=qc.createQueueSession(false,Session.AUTO_AKNOWLEDGE);
四、根据session创建发送者、接收者、消息
Queue sender = ctx.loopup("sender");
session.createSender(sender);
Queue receiver = ctx.loopup("receiver");
session.createReceiver("receiver");
Message message=session.createMessage();
五、发送消息 接收消息
sender.send(message,javax.jms.DeliveryMode.PERSISTENT,0,(long)24*60*60*days);
Message message=receiver.receiverNoWait();
六、完整的消息收发程序
package jmssample;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/** *//** This example shows how to establish a connection
* and send messages to the JMS queue. The classes in this
* package operate on the same JMS queue. Run the classes together to
* witness messages being sent and received, and to browse the queue
* for messages. The class is used to send messages to the queue.
*
* @author Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved.
*/
public class QueueSend
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL=" t3://localhost:7001";
// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY="SendJMSFactory";
// Defines the queue.
public final static String QUEUE="SendJMSQueue";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
/** *//**
* Creates all the necessary objects for sending
* messages to a JMS queue.
*
* @param ctx JNDI initial context
* @param queueName name of queue
* @exception NamingException if operation cannot be performed
* @exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}
/** *//**
* Sends a message to a JMS queue.
*
* @param message message to be sent
* @exception JMSException if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}
/** *//**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
/** *//** main() method.
*
* @param args WebLogic Server URL
* @exception Exception if operation fails
*/
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(QueueSend qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: "+line+"\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (! quitNow);
}
private static InitialContext getInitialContext()
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
}
1package jmssample;
2
3import java.util.Hashtable;
4import javax.jms.*;
5import javax.naming.Context;
6import javax.naming.InitialContext;
7import javax.naming.NamingException;
8
9/** *//**
10* This example shows how to establish a connection to
11* and receive messages from a JMS queue. The classes in this
12* package operate on the same JMS queue. Run the classes together to
13* witness messages being sent and received, and to browse the queue
14* for messages. This class is used to receive and remove messages
15* from the queue.
16*
17* @author Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved.
18*/
19public class QueueReceive implements MessageListener
20{
21// Defines the JNDI context factory.
22public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
23
24// Defines the JNDI provider url.
25public final static String PROVIDER_URL=" t3://localhost:7001";
26
27// Defines the JMS connection factory for the queue.
28public final static String JMS_FACTORY="SendJMSFactory";
29
30// Defines the queue.
31public final static String QUEUE="SendJMSQueue";
32
33private QueueConnectionFactory qconFactory;
34private QueueConnection qcon;
35private QueueSession qsession;
36private QueueReceiver qreceiver;
37private Queue queue;
38private boolean quit = false;
39
40/** *//**
41* Message listener interface.
42* @param msg message
43*/
44public void onMessage(Message msg)
45{
46try {
47String msgText;
48if (msg instanceof TextMessage) {
49msgText = ((TextMessage)msg).getText();
50} else {
51msgText = msg.toString();
52}
53
54System.out.println("Message Received: "+ msgText );
55
56if (msgText.equalsIgnoreCase("quit")) {
57synchronized(this) {
58quit = true;
59this.notifyAll(); // Notify main thread to quit
60}
61}
62} catch (JMSException jmse) {
63jmse.printStackTrace();
64}
65}
66
67/** *//**
68* Creates all the necessary objects for receiving
69* messages from a JMS queue.
70*
71* @param ctx JNDI initial context
72* @param queueName name of queue
73* @exception NamingException if operation cannot be performed
74* @exception JMSException if JMS fails to initialize due to internal error
75*/
76public void init(Context ctx, String queueName)
77throws NamingException, JMSException
78{
79qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
80qcon = qconFactory.createQueueConnection();
81qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
82queue = (Queue) ctx.lookup(queueName);
83qreceiver = qsession.createReceiver(queue);
84qreceiver.setMessageListener(this);
85qcon.start();
86}
87
88/** *//**
89* Closes JMS objects.
90* @exception JMSException if JMS fails to close objects due to internal error
91*/
92public void close()throws JMSException
93{
94qreceiver.close();
95qsession.close();
96qcon.close();
97}
98/** *//**
99* main() method.
100*
101* @param args WebLogic Server URL
102* @exception Exception if execution fails
103*/
104
105public static void main(String[] args) throws Exception {
106
107InitialContext ic = getInitialContext();
108QueueReceive qr = new QueueReceive();
109qr.init(ic, QUEUE);
110
111System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
112
113// Wait until a "quit" message has been received.
114synchronized(qr) {
115while (! qr.quit) {
116try {
117qr.wait();
118} catch (InterruptedException ie) {}
119}
120}
121qr.close();
122}
123
124private static InitialContext getInitialContext()
125throws NamingException
126{
127Hashtable env = new Hashtable();
128env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
129env.put(Context.PROVIDER_URL, PROVIDER_URL);
130return new InitialContext(env);
131}
132
133
134}
135