一、初始化
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);
}

}

1
package jmssample;
2
3
import java.util.Hashtable;
4
import javax.jms.*;
5
import javax.naming.Context;
6
import javax.naming.InitialContext;
7
import 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
*/
19
public class QueueReceive implements MessageListener
20

{
21
// Defines the JNDI context factory.
22
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
23
24
// Defines the JNDI provider url.
25
public final static String PROVIDER_URL=" t3://localhost:7001";
26
27
// Defines the JMS connection factory for the queue.
28
public final static String JMS_FACTORY="SendJMSFactory";
29
30
// Defines the queue.
31
public final static String QUEUE="SendJMSQueue";
32
33
private QueueConnectionFactory qconFactory;
34
private QueueConnection qcon;
35
private QueueSession qsession;
36
private QueueReceiver qreceiver;
37
private Queue queue;
38
private boolean quit = false;
39
40
/** *//**
41
* Message listener interface.
42
* @param msg message
43
*/
44
public void onMessage(Message msg)
45

{
46
try
{
47
String msgText;
48
if (msg instanceof TextMessage)
{
49
msgText = ((TextMessage)msg).getText();
50
} else
{
51
msgText = msg.toString();
52
}
53
54
System.out.println("Message Received: "+ msgText );
55
56
if (msgText.equalsIgnoreCase("quit"))
{
57
synchronized(this)
{
58
quit = true;
59
this.notifyAll(); // Notify main thread to quit
60
}
61
}
62
} catch (JMSException jmse)
{
63
jmse.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
*/
76
public void init(Context ctx, String queueName)
77
throws NamingException, JMSException
78

{
79
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
80
qcon = qconFactory.createQueueConnection();
81
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
82
queue = (Queue) ctx.lookup(queueName);
83
qreceiver = qsession.createReceiver(queue);
84
qreceiver.setMessageListener(this);
85
qcon.start();
86
}
87
88
/** *//**
89
* Closes JMS objects.
90
* @exception JMSException if JMS fails to close objects due to internal error
91
*/
92
public void close()throws JMSException
93

{
94
qreceiver.close();
95
qsession.close();
96
qcon.close();
97
}
98
/** *//**
99
* main() method.
100
*
101
* @param args WebLogic Server URL
102
* @exception Exception if execution fails
103
*/
104
105
public static void main(String[] args) throws Exception
{
106
107
InitialContext ic = getInitialContext();
108
QueueReceive qr = new QueueReceive();
109
qr.init(ic, QUEUE);
110
111
System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
112
113
// Wait until a "quit" message has been received.
114
synchronized(qr)
{
115
while (! qr.quit)
{
116
try
{
117
qr.wait();
118
} catch (InterruptedException ie)
{}
119
}
120
}
121
qr.close();
122
}
123
124
private static InitialContext getInitialContext()
125
throws NamingException
126

{
127
Hashtable env = new Hashtable();
128
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
129
env.put(Context.PROVIDER_URL, PROVIDER_URL);
130
return new InitialContext(env);
131
}
132
133
134
}
135