`
jiasudu1649
  • 浏览: 719605 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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代码
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();   
    }   
  }   

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代码
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();   
    }   
  }   

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非常简单。
分享到:
评论
2 楼 xingmei_ok 2012-05-04  
请求解答。我把你的例子复制下拉,为什么工程报错。需要我配置什么吗?import java.jms.*包都找不到??配置文件那里呢,怎么配置。请求解答。java新手。。。
1 楼 xingmei_ok 2012-05-04  
hh

相关推荐

    java-jms小例子

    总结,"java-jms小例子"是一个基础教程,帮助开发者理解如何在Java应用程序中使用JMS进行异步通信。通过创建消息生产者和消费者,设置消息队列或主题,以及发送和接收不同类型的JMS消息,开发者能够掌握JMS的核心...

    Jms的例子 不错的例子

    在“jmstest”这个压缩包中,我们可能找到一个简单的JMS应用实例。通常,这样的例子会包含生产者(Producer)、消费者(Consumer)以及可能的消息中间件(Message Broker)配置。生产者是发送消息的组件,而消费者则...

    SpringBoot使用JMS的小例子(ActiveMQ实现)

    通过这个小例子,我们了解了如何在SpringBoot应用中集成ActiveMQ,创建JMS生产者和消费者。在实际项目中,可以根据需求调整队列、主题的使用,以及实现更复杂的消息处理逻辑。同时,ActiveMQ还提供了许多高级特性,...

    JMS OPENJMS的实现例子

    OPENJMS是JMS的一个开源实现,提供了一个轻量级的消息中间件,使得开发者可以方便地在Java应用程序之间实现消息交换。 JMS的核心概念包括生产者(Producer)、消费者(Consumer)、队列(Queue)和主题(Topic)。...

    JMS开发例子.rar

    这个"JMS开发例子.rar"压缩包文件很可能是包含了一个关于如何使用JMS进行开发的实例教程或代码示例。 在JMS中,有两个主要的角色:生产者(Producer)和消费者(Consumer)。生产者负责创建和发送消息,而消费者则...

    JMS完全实例(八个实例)

    在JMS中,队列保证消息的顺序传递,每个消息只能被一个消费者接收,适合一对一通信。而主题支持发布/订阅模式,消息可以被多个订阅者同时接收,适合一对多通信。实例中会展示这两种模型的使用场景和差异。 7. **...

    activeMQ JMS WEB 例子

    在Web环境中集成ActiveMQ,我们需要一个支持JMS的Web应用程序服务器,如Tomcat或Jetty。首先,要在Web应用中使用ActiveMQ,你需要将ActiveMQ的JAR文件添加到应用的类路径中。这些JAR文件通常可以在ActiveMQ的lib目录...

    jms+sping+activeMq的例子serd和recevice

    ActiveMQ是Apache软件基金会的一个开源项目,它是JMS的实现,作为一个消息中间件,它允许应用程序之间通过消息进行通信。ActiveMQ支持多种协议,并且可以在分布式环境中高效地处理大量消息。 **集成Spring与...

    JMS的一个非常好的demo

    **JMS(Java Message Service)** 是一个Java平台上的标准接口,用于在分布式环境中传递消息。它提供了一种可靠的、异步的通信机制,使得不同的应用程序之间可以通过消息进行通信,而无需直接相互依赖。JMS通常用于...

    JMS分布式例子

    总结来说,"JMS分布式例子"是一个关于如何使用Java消息服务在分布式环境中实现可靠通信的示例,涵盖了JMS的基本概念、API使用、不同消息模型以及在提升系统可扩展性和容错性方面的作用。通过分析提供的实例文件,...

    JMS相关,教程,例子,学习笔记

    PTP模型中,消息从一个生产者发送到一个队列,由一个消费者接收;而在Pub/Sub模型中,消息发布到主题,多个订阅者可以接收。 2. **消息和消息头**:消息包含实际的数据以及一组标准的消息头,如目的地(目的地可以是...

    Spring发送接收JMS消息

    在Java世界中,Java Message Service (JMS) 是一个标准接口,用于在分布式环境中发送和接收消息。Spring框架提供了一种简单而强大的方式来集成JMS,使得开发者可以轻松地在应用中实现异步通信和解耦。本篇文章将深入...

    JMS消息服务AcitveMQ的配置和测试小例子

    Apache ActiveMQ是JMS的一个实现,它是一个开源的消息代理,提供了高性能、高可用性和易于管理的消息传递功能。 在本文中,我们将深入探讨如何配置和测试ActiveMQ以及其与JMS的结合使用。首先,我们需要下载并安装...

    JBOSS建立JMS应用实例

    JBOSST(Java Business Open Source Solutions)是一个开源的企业级应用服务器,它提供了全面的中间件服务,包括对Java消息服务(JMS)的支持。JMS是一种标准,用于在分布式环境中传递消息,提供了一种可靠且异步的...

    JMS简单示例1

    在"JMS示例"中,通常会展示如何创建一个简单的消息生产和消费流程: 1. **创建连接工厂(ConnectionFactory)**:这是连接到消息代理的桥梁,JMS API提供创建连接工厂的方法。 2. **创建连接(Connection)**:...

    Java jms处理消息请求的例子.rar

    Java jms处理消息请求的例子,Java创建和初始化一个请求Requestor对象,初始化请求Requestor对象,接受由反馈者replier发回的消息,响应request产生要输出的信息,输出响应信息。

Global site tag (gtag.js) - Google Analytics