jms 使用(大概过一遍了解了解)(转载)

JMS是一个由AS提供的Message服务。它能接受消息产生者(Message Provider)所发出的消息,并把消息转发给消息消费者(Message  Consumer)。 
2、JMS提供2种类型的消息服务:(1)Queue,即点对点,每个消息只转发给一个消息消费者使用。(2)Topic,即发布和订阅,每个消息可以转发给所有的订阅者(消费者)。 
3、WEBLOGIC 8下的JMS配置: 
(1)配置JMS Connection Factory 
(2)配置JMS File Store(目前所找到的文档都是配置File Store,其实在具体的应用中,可能JMS JDBC Store更广泛,但暂时没有找到资料) 
(3)配置JMS Server 
(4)在JMS Server的destinations中配置JMS Queue或者JMS Topic 
其中提供给消息产生者和消息消费者使用的是JMS Connection Factory的JNDI和JMS Queue或者JMS Topic的JNDI。 
4、消息产生者向JMS发送消息的步骤: 
(1)使用JNDI查询对象JMS ConnectionFactory和Destination(JMS Queue/Topic) 
(2)使用管理对象JMS ConnectionFactory建立连接Connection 
(3)使用连接Connection 建立会话Session 
(4)使用会话Session和管理对象Destination创建消息生产者MessageSender 
(5)使用消息生产者MessageSender发送消息 
一个消息发送者的例子:

Java代码   复制代码
  1. package myjms;   
  2.   
  3. import java.util.*;   
  4. import javax.naming.*;   
  5. import javax.jms.*;   
  6.   
  7. public class MessageProducter {   
  8.   public static void main(String[] args) {   
  9.     String queueConnectionFactoryName = "myjmsconnectionfactory"//JMS Connection Factory的JNDI   
  10.     String queueName = "myjmsqueue"//JMS Queue或者JMS Topic的JNDI   
  11.   
  12.     boolean transacted = false;//transaction模式   
  13.     int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;//acknowledgement模式   
  14.     String message="Message need to send";//模拟需要发送的消息   
  15.   
  16.     Properties properties = new Properties();   
  17.     properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");   
  18.     properties.put(Context.PROVIDER_URL, "t3://localhost:7001");   
  19.   
  20.     try {   
  21.       Context context = new InitialContext(properties);   
  22.       Object obj = context.lookup(queueConnectionFactoryName);   
  23.       QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;//JMS Connection Factory的获得   
  24.         
  25.       obj = context.lookup(queueName);   
  26.       Queue queue = (Queue) obj;//JMS Queue或者JMS Topic的获得   
  27.   
  28.       QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();//产生连接  
  29.       queueConnection.start();   
  30.       QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);   
  31.       TextMessage textMessage = queueSession.createTextMessage();   
  32.       textMessage.clearBody();   
  33.       textMessage.setText(message);   
  34.       QueueSender queueSender = queueSession.createSender(queue);   
  35.       queueSender.send(textMessage);   
  36.       if (transacted) {   
  37.         queueSession.commit();   
  38.       }   
  39.   
  40.       if (queueSender != null) {   
  41.         queueSender.close();   
  42.       }   
  43.       if (queueSession != null) {   
  44.         queueSession.close();   
  45.       }   
  46.       if (queueConnection != null) {   
  47.         queueConnection.close();   
  48.       }   
  49.   
  50.     }   
  51.     catch(Exception ex){   
  52.       ex.printStackTrace();   
  53.     }   
  54.   }   
  55. }  
package myjms;

import java.util.*;
import javax.naming.*;
import javax.jms.*;

public class MessageProducter {
  public static void main(String[] args) {
    String queueConnectionFactoryName = "myjmsconnectionfactory"; //JMS Connection Factory的JNDI
    String queueName = "myjmsqueue"; //JMS Queue或者JMS Topic的JNDI

    boolean transacted = false;//transaction模式
    int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;//acknowledgement模式
    String message="Message need to send";//模拟需要发送的消息

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "t3://localhost:7001");

    try {
      Context context = new InitialContext(properties);
      Object obj = context.lookup(queueConnectionFactoryName);
      QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;//JMS Connection Factory的获得
     
      obj = context.lookup(queueName);
      Queue queue = (Queue) obj;//JMS Queue或者JMS Topic的获得

      QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();//产生连接
      queueConnection.start();
      QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);
      TextMessage textMessage = queueSession.createTextMessage();
      textMessage.clearBody();
      textMessage.setText(message);
      QueueSender queueSender = queueSession.createSender(queue);
      queueSender.send(textMessage);
      if (transacted) {
        queueSession.commit();
      }

      if (queueSender != null) {
        queueSender.close();
      }
      if (queueSession != null) {
        queueSession.close();
      }
      if (queueConnection != null) {
        queueConnection.close();
      }

    }
    catch(Exception ex){
      ex.printStackTrace();
    }
  }
}


5、消息消费者从JMS接受消息的步骤: 
(1)使用JNDI查询对象JMS ConnectionFactory和Destination(JMS Queue/Topic) 
(2)使用管理对象JMS ConnectionFactory建立连接Connection 
(3)使用连接Connection 建立会话Session 
(4)使用会话Session和管理对象Destination创建消息消费者MessageReceiver 
(5)使用消息消费者MessageReceiver接受消息,需要用setMessageListener将MessageListener接口绑定到MessageReceiver 
消息消费者必须实现了MessageListener接口,需要定义onMessage事件方法。 
一个消息消费者的例子:

Java代码   复制代码
  1. package myjms;   
  2.   
  3. import java.util.*;   
  4. import javax.naming.*;   
  5. import javax.jms.*;   
  6.   
  7. public class MessageReciever   
  8.     implements MessageListener {   
  9.   public void onMessage(Message message) {   
  10.     if (message instanceof TextMessage) {   
  11.       TextMessage textMessage = (TextMessage) message;   
  12.       try {   
  13.         System.out.println("Message content is:" + textMessage.getText());   
  14.       }   
  15.       catch (JMSException e) {   
  16.         e.printStackTrace();   
  17.       }   
  18.     }   
  19.   }   
  20.   
  21.   public static void main(String[] args) {   
  22.       
  23.     MessageReciever msgRcvr=new MessageReciever();   
  24.     String queueConnectionFactoryName = "myjmsconnectionfactory";   
  25.     String queueName = "myjmsqueue";   
  26.   
  27.     boolean transacted = false;   
  28.     int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;   
  29.   
  30.     Properties properties = new Properties();   
  31.     properties.put(Context.INITIAL_CONTEXT_FACTORY,   
  32.                    "weblogic.jndi.WLInitialContextFactory");   
  33.     properties.put(Context.PROVIDER_URL, "t3://localhost:7001");   
  34.   
  35.     try {   
  36.       Context context = new InitialContext(properties);   
  37.       Object obj = context.lookup(queueConnectionFactoryName);   
  38.       QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)   
  39.           obj;   
  40.   
  41.       obj = context.lookup(queueName);   
  42.       Queue queue = (Queue) obj;   
  43.   
  44.       QueueConnection queueConnection = queueConnectionFactory.   
  45.           createQueueConnection();   
  46.       queueConnection.start();   
  47.       QueueSession queueSession = queueConnection.createQueueSession(transacted,   
  48.           acknowledgementMode);   
  49.       QueueReceiver queueReceiver = queueSession.createReceiver(queue);   
  50.   
  51.       queueReceiver.setMessageListener(msgRcvr);   
  52.   
  53.       synchronized(msgRcvr){   
  54.         msgRcvr.wait(100000);   
  55.       }   
  56.   
  57.       if (queueReceiver != null) {   
  58.         queueReceiver.close();   
  59.       }   
  60.       if (queueSession != null) {   
  61.         queueSession.close();   
  62.       }   
  63.       if (queueConnection != null) {   
  64.         queueConnection.close();   
  65.       }   
  66.   
  67.     }   
  68.     catch (Exception ex) {   
  69.       ex.printStackTrace();   
  70.     }   
  71.   }   
  72. }  
package myjms;

import java.util.*;
import javax.naming.*;
import javax.jms.*;

public class MessageReciever
    implements MessageListener {
  public void onMessage(Message message) {
    if (message instanceof TextMessage) {
      TextMessage textMessage = (TextMessage) message;
      try {
        System.out.println("Message content is:" + textMessage.getText());
      }
      catch (JMSException e) {
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {
   
    MessageReciever msgRcvr=new MessageReciever();
    String queueConnectionFactoryName = "myjmsconnectionfactory";
    String queueName = "myjmsqueue";

    boolean transacted = false;
    int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,
                   "weblogic.jndi.WLInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "t3://localhost:7001");

    try {
      Context context = new InitialContext(properties);
      Object obj = context.lookup(queueConnectionFactoryName);
      QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)
          obj;

      obj = context.lookup(queueName);
      Queue queue = (Queue) obj;

      QueueConnection queueConnection = queueConnectionFactory.
          createQueueConnection();
      queueConnection.start();
      QueueSession queueSession = queueConnection.createQueueSession(transacted,
          acknowledgementMode);
      QueueReceiver queueReceiver = queueSession.createReceiver(queue);

      queueReceiver.setMessageListener(msgRcvr);

      synchronized(msgRcvr){
        msgRcvr.wait(100000);
      }

      if (queueReceiver != null) {
        queueReceiver.close();
      }
      if (queueSession != null) {
        queueSession.close();
      }
      if (queueConnection != null) {
        queueConnection.close();
      }

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}


6、Message-driven Bean 
MDB实际上就是一个消息消费者的客户端程序。它由AS EJB Container来管理。在JBUILDER生成一个MDB非常简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值