首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

运用JMS API创建JMS应用程序

2012-07-24 
使用JMS API创建JMS应用程序1. 创建JMS应用程序的一般步骤;??? (1). 获取一个JMS ConnectionFactory??? (2

使用JMS API创建JMS应用程序

1. 创建JMS应用程序的一般步骤;

??? (1). 获取一个JMS ConnectionFactory

??? (2). 使用ConnectionFactory创建一个Connection

??? (3). 打开JMS Connection

??? (4). 从Connection创建一个Session

??? (5). 获取一个Destination

??? (6). 创建一个JMS Producer或创建一个JMS Message并指定Destination

??? (7). 创建一个JMD Consumer或注册一个JMS 消息Listener

??? (8). 发送或接受JMS消息

??? (9). 关闭所有的JMS资源(包括Connection, Session,Producer,Consumer等)

??? 实例代码:

??? public class MyMessageProducer {
???????? ...
???????? ConnectionFactory connectionFactory;
???????? Connection connection;
???????? Session session;
???????? Destination destination;
???????? MessageProducer producer;
???????? Message message;
???????? boolean useTransaction = false;

????
???????? try {
????????????? Context ctx = new InitialContext();
????????????? connectionFactory =
?????????????????? (ConnectionFactory) ctx.lookup("ConnectionFactoryName");
????????????? connection = connectionFactory.createConnection();
????????????? connection.start();
????????????? session = connection.createSession(useTransaction,Session.AUTO_ACKNOWLEDGE);
????????????? destination = session.createQueue("TEST.QUEUE");
????????????? producer = session.createProducer(destination);
????????????? message = session.createTextMessage("this is a test");
????????????? producer.send(message);
???????? } catch (JMSException jmsEx) {

????????????? ...
???????? } finally {
????????????? producer.close();
????????????? session.close();
????????????? connection.close();
???????? }
??? }

2. 同步方式接收一个JMS Message

??? public class MySyncMessageConsumer {
??????? ...
??????? ConnectionFactory connectionFactory;
??????? Connection connection;
??????? Session session;
??????? Destination destination;
??????? MessageConsumer consumer;
??????? Message message;
??????? boolean useTransaction = false;
??????? try {
???????????? Context ctx = new InitialContext();
???????????? connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactoryName");
???????????? connection = connectionFactory.createConnection();
???????????? connection.start();
???????????? session = connection.createSession(useTransaction,
???????????? Session.AUTO_ACKNOWLEDGE);
???????????? destination = session.createQueue("TEST.QUEUE");
???????????? consumer = session.createConsumer(destination);
???????????? message = (TextMessage) consumer.receive(1000);
???????????? System.out.println("Received message: " + message);

??????? } catch (JMSException jmsEx) {
???????????? ...
??????? } finally {
???????????? producer.close();
???????????? session.close();
???????????? connection.close();
??????? }
??? }

3. 异步方式接收一个JMS Message

??? public class MyAsyncMessageConsumer implements MessageListener {
??????? ...
??????? ConnectionFactory connectionFactory;
??????? Connection connection;
??????? Session session;
??????? Destination destination;
??????? MessageProducer producer;
??????? Message message;
??????? boolean useTransaction = false;
??????? try {
????????????? Context ctx = new InitialContext();
????????????? connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactoryName");
????????????? connection = connectionFactory.createConnection();
????????????? connection.start();
????????????? session = connection.createSession(useTransaction,
????????????????? Session.AUTO_ACKNOWLEDGE);
????????????? destination = session.createQueue("TEST.QUEUE");
????????????? consumer = session.createConsumer(destination);
????????????? consumer.setMessageListener(this);
??????? } catch (JMSException jmsEx) {
????????????? ...
??????? } finally {
????????????? producer.close();

????????????? session.close();
????????????? connection.close();
??????? }

???????
??????? public void onMessage(Message message) {
????????????? if (message instanceof TextMessage) {
????????????????? System.out.println("Received message: " + message);
????????????? }
??????? }
?? }

热点排行