JMS 开发示例。 使用weblogic8作为jms server。
1、在weblogic中配置JMS。
配置JMS connection factory
配置JMS File Store
配置JMS Server
配置JMS destination
2、创建JMS Sender
package jms;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JMSSender {
/**
* @param args
* @throws NamingException
* @throws JMSException
*/
public static void main(String[] args) throws NamingException, JMSException {
//init JNDI context
String JNDIFactory = "weblogic.jndi.WLInitialContextFactory";//define JNDI context factory
String providerUrl = "t3://127.0.0.1:7001"; //define weblogic JMS url
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDIFactory);
env.put(Context.PROVIDER_URL, providerUrl);
Context ctx = new InitialContext(env);
//find connection factory
String connFactoryJNDI = "JMSConnectionFactory"; //jms connectionFactory JNDI name
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(connFactoryJNDI);
//create queue connection
QueueConnection qConn = (QueueConnection) connFactory.createConnection();
//create session
QueueSession qSession = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//find queue by JNDI lookup
Queue queue = (Queue) ctx.lookup("JMSQueue");
//create sender
QueueSender qSender = qSession.createSender(queue);
//create message
// Message msg = qSession.createTextMessage("Message is from JMS Sender4!");
Message msg = qSession.createTextMessage("quit1");
qSender.send(msg);
qSender.close();
qSession.close();
qConn.close();
}
}
3、创建JMS Receiver
package jms;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JMSReceiver implements MessageListener {
// 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 CONNECTION_FACTORY_JNDI_NAME = "JMSConnectionFactory";
// Defines the queue, use the queue JNDI name
public final static String QUEUE_JNDI_NAME = "JMSQueue";
private QueueConnectionFactory qconFactory;
private QueueConnection queueConnection;
private QueueSession queueSession;
private QueueReceiver queueReceiver;
private Queue queue;
private boolean quit = false;
/**
* get the context object.
*
* @return context object
* @throws NamingException if operation cannot be performed
*/
private static InitialContext getInitialContext() throws NamingException {
Hashtable table = new Hashtable();
table.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
table.put(Context.PROVIDER_URL, PROVIDER_URL);
InitialContext context = new InitialContext(table);
return context;
}
/**
* Creates all the necessary objects for receiving messages from 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(CONNECTION_FACTORY_JNDI_NAME);
queueConnection = qconFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
queueReceiver = queueSession.createReceiver(queue);
queueReceiver.setMessageListener(this);
// second thread: message reveive thread.
queueConnection.start();
}
/**
* implement from MessageListener.
* when a message arrived, it will be invoked.
*
* @param message message
*/
public void onMessage(Message message) {
try {
String msgStr = "";
int age = 0;
if (message instanceof TextMessage) {
msgStr = ((TextMessage) message).getText();
} else if (message instanceof StreamMessage) {
msgStr = ((StreamMessage) message).readString();
age = ((StreamMessage) message).readInt();
} else if (message instanceof BytesMessage) {
byte[] block = new byte[1024];
((BytesMessage) message).readBytes(block);
msgStr = String.valueOf(block);
} else if (message instanceof MapMessage) {
msgStr = ((MapMessage) message).getString("name");
} else if (message instanceof ObjectMessage)
{
}
System.out.println("Message Received: " + msgStr + ", " + age);
if (msgStr.equalsIgnoreCase("quit")) {
synchronized (this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException e) {
throw new RuntimeException("error happens", e);
}
}
/**
* release resources.
*
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
queueReceiver.close();
queueSession.close();
queueConnection.close();
}
/**
* test client.
* first thread(main thread)
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
InitialContext ctx = getInitialContext();
JMSReceiver receiver = new JMSReceiver();
receiver.init(ctx, QUEUE_JNDI_NAME);
// Wait until a "quit" message has been received.
synchronized (receiver) {
while (!receiver.quit) {
try {
receiver.wait();
} catch (InterruptedException e) {
throw new RuntimeException("error happens", e);
}
}
}
receiver.close();
}
}
启动weblogic 后就可以运行JMS Sender 和JMS Receiver。