简单的JMS实例,包括点对点和主题订阅。
2008-02-23 10:07:40来源:互联网 阅读 ()
三个Java文件:
JMSTest.java
package com.lizongbo.jmsdemo;
import javax.jms.MessageListener;
import javax.naming.Context;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.QueueSender;
import javax.jms.QueueReceiver;
import javax.jms.Queue;
import java.io.Serializable;
import java.util.Properties;
import javax.jms.Message;
import javax.jms.JMSException;
/**
<p>Title:JMS demo </p>
<p>Description: </p>
<p>Copyright: Copyright (c) 2005</p>
<p>Company: </p>
@author lizongbo
@version 1.0
* Use this class to send and receive point-to-point messages.
* To send a text message:
* <code>
* JMSTest jMSTest = new JMSTest();
* jMSTest.sendText("Hello world");
* jMSTest.close(); //Release resources
* </code>
* To receive a message:
* <code>
* JMSTest jMSTest = new JMSTest();
* jMSTest.getQueueReceiver();
* </code>
*/
public class JMSTest
implements MessageListener {
private static Context context;
private boolean transacted = true;
private int acknowledgementMode = javax.jms.Session.AUTO_ACKNOWLEDGE;
private QueueConnectionFactory queueConnectionFactory;
private QueueConnection queueConnection;
private QueueSession queueSession;
private QueueSender queueSender;
private QueueReceiver queueReceiver;
private Queue queue;
private String queueConnectionFactoryName = "testjms";
private String sendQueueName = "com.lizongbo.jms001";
private String recvQueueName = "com.lizongbo.jms001";
public void setTransacted(boolean transacted) {
this.transacted = transacted;
}
public void sendObject(Serializable message) throws Exception {
javax.jms.ObjectMessage objectMessage = getQueueSession().
createObjectMessage();
objectMessage.clearBody();
objectMessage.setObject(message);
getQueueSender().send(objectMessage);
if (isTransacted()) {
getQueueSession().commit();
}
}
public void sendText(String message) throws Exception {
javax.jms.TextMessage textMessage = getQueueSession().createTextMessage();
textMessage.clearBody();
textMessage.setText(message);
getQueueSender().send(textMessage);
if (isTransacted()) {
getQueueSession().commit();
}
}
public QueueReceiver getQueueReceiver() throws Exception {
if (queueReceiver == null) {
queueReceiver = getQueueSession().createReceiver(getRecvQueue());
queueReceiver.setMessageListener(this);
}
return queueReceiver;
}
public QueueSender getQueueSender() throws Exception {
if (queueSender == null) {
queueSender = getQueueSession().createSender(getSendQueue());
}
return queueSender;
}
public Queue getRecvQueue() throws Exception {
if (queue == null) {
Object obj = getContext().lookup(recvQueueName);
queue = (Queue) obj;
}
return queue;
}
public Queue getSendQueue() throws Exception {
if (queue == null) {
Object obj = getContext().lookup(sendQueueName);
queue = (Queue) obj;
}
return queue;
}
public QueueSession getQueueSession() throws Exception {
if (queueSession == null) {
queueSession = getQueueConnection().createQueueSession(isTransacted(),
getAcknowledgementMode());
}
return queueSession;
}
public QueueConnection getQueueConnection() throws Exception {
if (queueConnection == null) {
queueConnection = getQueueConnectionFactory().createQueueConnection();
queueConnection.start();
}
return queueConnection;
}
public QueueConnectionFactory getQueueConnectionFactory() throws Exception {
if (queueConnectionFactory == null) {
Object obj = getContext().lookup(queueConnectionFactoryName);
queueConnectionFactory = (QueueConnectionFactory) obj;
}
return queueConnectionFactory;
}
public void setRecvQueueName(String recvQueueName) {
this.recvQueueName = recvQueueName;
}
public String getRecvQueueName() {
return recvQueueName;
}
public void setSendQueueName(String sendQueueName) {
this.sendQueueName = sendQueueName;
}
public String getSendQueueName() {
return sendQueueName;
}
public void setQueueConnectionFactoryName(String queueConnectionFactoryName) {
this.queueConnectionFactoryName = queueConnectionFactoryName;
}
public String getQueueConnectionFactoryName() {
return queueConnectionFactoryName;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash