1.JMS概述JMS是JavaEE技术规范中的一个重要组成部分,它是一种企业消息处理的规范。JMS提供了一组通用的Java应用程序接口(API),开发者可以通过这种通用的API来创建、发送、接收、读取消息,JMS是一种与具体实现厂商无关的API,不管底层采用何种消息服务器实现,应用程序总是面向通用的JMS API编程。JMS主要支持两种消息模型:点对点消息模型(PTP)和发布/订阅消息模型(pub/sub)。点对点消息模型只能把消息传递给一个消息消费者,而发布/订阅模型可以将消息传递给多个消息消费者。2.点对点消息模型在点对点消息模型中,消息发送者将消息发送到一个特定的队列,而消息消费者从这个特定的队列中获取消息。多个消息发送者和多个消息消费者可以与同一个队列相关联,但是一个单独的消息只能有一个消息消费者接收。如果有多个消息消费者监听同一个队列中的消息时,WebLogic JMS本着先到先服务的原则,确定哪个消息消费者会收到下一条消息。如果没有消息消费者监听队列,消息保留在队列中,直到消息消费者连接到队列中。3.编程实例我们在WebLogic服务器中配置了一个JNDI名为“queue”的消息队列,它可以作为PTP消息模型的消息目的地来使用。WebLogic服务器中提供了默认的连接工厂,其JNDI名为“weblogic.jms.ConnectionFactory”。向消息队列发送消息的程序如下:public class QueueMessageSender{
public void sendMessage() throws Exception{
final String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";
Context ctx = getInitialContext();
ConnectionFactory connFactory = (ConnectionFactory)ctx.lookup(CONNECTION_FACTORY_JNDI);
Destination dest = (Destination)ctx.lookup("queue");
Connection conn = connFactory.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(dest);
sender.setDeliveryMode(DeliveryMode.PERSISTENT);
sender.setTimeToLive(20000);
TextMessage msg = session.createTextMessage();
msg.setText("Hello JMS");
sender.send(msg);
msg.setText("The time is "+new Date().toString());
sender.send(msg);
session.close();
conn.close();
}
private Context getInitialContext(){
final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
final String SERVER_URL = "t3://localhost:7001";
Context ctx = null;
try{
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY);
props.put(Context.PROVIDER_URL, SERVER_URL);
ctx = new InitialContext(props);
}catch(NamingException ne){
System.out.println("can't connect the WebLogic " + SERVER_URL);
ne.printStackTrace();
}
return ctx;
}
public static void main(String[] args) throws Exception{
QueueMessageSender mp = new QueueMessageSender();
mp.sendMessage();
}
}
从消息队列中同步读取消息的程序如下:
public class SyncConsumer {
public void receiveMessage() throws Exception {
final String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";
Context ctx = getInitialContext();
ConnectionFactory connFactory = (ConnectionFactory) ctx
.lookup(CONNECTION_FACTORY_JNDI);
Destination dest = (Destination) ctx.lookup("queue");
Connection conn = connFactory.createConnection();
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer receiver = session.createConsumer(dest);
TextMessage msg = (TextMessage) receiver.receive();
System.out.println(msg);
System.out.println("The message: " + msg.getText());
session.close();
conn.close();
}
private Context getInitialContext() {
final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
final String SERVER_URL = "t3://localhost:7001";
Context ctx = null;
try {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY);
props.put(Context.PROVIDER_URL, SERVER_URL);
ctx = new InitialContext(props);
} catch (NamingException ne) {
System.out.println("can't connect the WebLogic " + SERVER_URL);
ne.printStackTrace();
}
return ctx;
}
public static void main(String[] args) throws Exception {
SyncConsumer mp = new SyncConsumer();
mp.receiveMessage();
}
}