`
单眼皮大娘
  • 浏览: 112778 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

JMS 基础概念(二)

    博客分类:
  • JMS
阅读更多
本节讲述:The JMS API Programming Model



上图为JMS Programming Model 的框架图


一、首先阐述上一篇文档的遗留问题:Administered Objects.

Administered Objects

    Two parts of a JMS application--destinations and connection factories--are best maintained administratively rather than programmatically . 【Administered Objects 是通过文件配置的,包含两部分destinations 和 connection factories】

    1.1 Connection Factories
    A connection factory is the object a client uses to create a connection to a provider. A connection factory encapsulates a set of connection configuration parameters that has been defined by an administrator. Each connection factory is an instance of the ConnectionFactory, QueueConnectionFactory, or TopicConnectionFactory interface.
  
    如下图:



     At the beginning of a JMS client program, you usually perform a JNDI lookup of a connection factory, then cast and assign it to a ConnectionFactory object.

    
     Context ctx = new InitialContext(); 
     ConnectionFactory connectionFactory = (ConnectionFactory)
     ctx.lookup("jms/ConnectionFactory"); 
     


     1.2 Destinations
     A destination is the object a client uses to specify the target of messages it produces and the source of messages it consumes. In the PTP messaging domain, destinations are called queues. In the pub/sub messaging domain, destinations are called topics.
   
    


    
     Destination myDest = (Destination) ctx.lookup("jms/MyTopic"); 
     Queue myQueue = (Queue) ctx.lookup("jms/MyQueue");
     


    
二、Connections
Connections

     A connection encapsulates a virtual connection with a JMS provider. A connection could represent an open TCP/IP socket between a client and a provider service daemon. You use a connection to create one or more sessions.

     Connections implement the Connection interface. When you have a ConnectionFactory object, you can use it to create a Connection:
   
Connection connection = connectionFactory.createConnection(); 

     Before an application completes, you must close any connections that you have created. Failure to close a connection can cause resources not to be released by the JMS provider.Closing a connection also closes its sessions and their message producers and message consumers.

    
connection.close(); 

     Before your application can consume messages, you must call the connection's start method.

三、Sessions
Sessions
     A session is a single-threaded context for producing and consuming messages. You use sessions to create message producers, message consumers, and messages.
     A session provides a transactional context with which to group a set of sends and receives into an atomic unit of work.
     Sessions implement the Session interface. After you create a Connection object, you use it to create a Session:
    
Session session = connection.createSession(false, 
  Session.AUTO_ACKNOWLEDGE);


     The first argument means that the session is not transacted; the second means that the session automatically acknowledges messages when they have been received successfully.
     To create a transacted session, use the following code:
    
Session session = connection.createSession(true, 0); 


四、Message Producers
     A message producer is an object that is created by a session and used for sending messages to a destination. It implements the MessageProducer interface.
     You use a Session to create a MessageProducer for a destination. Here, the first example creates a producer for the destination myQueue, and the second for the destination myTopic:

    
     MessageProducer producer = session.createProducer(myQueue); 
     MessageProducer producer = session.createProducer(myTopic); 
     

     After you have created a message producer, you can use it to send messages by using the send method:
    
producer.send(message); 


五、Message Consumers

    A message consumer is an object that is created by a session and used for receiving messages sent to a destination. It implements the MessageConsumer interface.
    A message consumer allows a JMS client to register interest in a destination with a JMS provider. The JMS provider manages the delivery of messages from a destination to the registered consumers of the destination.
    For example, you use a Session to create a MessageConsumer for either a queue or a topic:
   
MessageConsumer consumer = session.createConsumer(myQueue); 
MessageConsumer consumer = session.createConsumer(myTopic); 

    You use the Session.createDurableSubscriber method to create a durable topic subscriber. This method is valid only if you are using a topic.

    After you have created a message consumer, it becomes active, and you can use it to receive messages. You can use the close method for a MessageConsumer to make the message consumer inactive. Message delivery does not begin until you start the connection you created by calling its start method.
    ou use the receive method to consume a message synchronously. You can use this method at any time after you call the start method:
   
connection.start();
Message m = consumer.receive(); 
connection.start();
Message m = consumer.receive(1000); // time out after a second 

     To consume a message asynchronously, you use a message listener.
    
Message Listeners
     A message listener is an object that acts as an asynchronous event handler for messages. This object implements the MessageListener interface, which contains one method, onMessage. In the onMessage method, you define the actions to be taken when a message arrives.
     You register the message listener with a specific MessageConsumer by using the setMessageListener method. For example, if you define a class named Listener that implements the MessageListener interface, you can register the message listener as follows:
    
Listener myListener = new Listener();
consumer.setMessageListener(myListener); 


     After you register the message listener, you call the start method on the Connection to begin message delivery.
     When message delivery begins, the JMS provider automatically calls the message listener's onMessage method whenever a message is delivered. The onMessage method takes one argument of type Message, which your implementation of the method can cast to any of the other message types.
    
六、Messages
     The ultimate purpose of a JMS application is to produce and to consume messages that can then be used by other software applications. JMS messages have a basic format that is simple but highly flexible.
     A JMS message has three parts: a header, properties, and a body. Only the header is required.

     6.1 Message Headers
     A JMS message header contains a number of predefined fields that contain values that both clients and providers use to identify and to route messages. For example, every message has a unique identifier, which is represented in the header field JMSMessageID. The value of another header field, JMSDestination, represents the queue or the topic to which the message is sent. Other fields include a timestamp and a priority level.
     Each header field has associated setter and getter methods, which are documented in the description of the Message interface. Some header fields are intended to be set by a client, but many are set automatically by the send or the publish method, which overrides any client-set values.

    


     6.2 Message Bodies
     The JMS API defines five message body formats, also called message types, which allow you to send and to receive data in many different forms and provide compatibility with existing messaging formats.

    


     The JMS API provides methods for creating messages of each type and for filling in their contents. For example, to create and send a TextMessage, you might use the following statements:
    
TextMessage message = session.createTextMessage();
message.setText(msg_text);     // msg_text is a String
producer.send(message); 


      At the consuming end, a message arrives as a generic Message object and must be cast to the appropriate message type. You can use one or more getter methods to extract the message contents. The following code fragment uses the getText method:
     
Message m = consumer.receive(); 
if (m instanceof TextMessage) {
  TextMessage message = (TextMessage) m;
  System.out.println("Reading message: " + message.getText());
} else {
  // Handle error
} 

    
      至于整个的流程和例子,以及JSM的优缺点会在后续的文章中给出.
    
  • 大小: 23.2 KB
  • 大小: 73.9 KB
  • 大小: 74.4 KB
  • 大小: 48.1 KB
  • 大小: 96.3 KB
分享到:
评论

相关推荐

    Jms基础知识整理创建消息 -> 发送消息 -> 接收消息 -> 读取消息 ()

    【JMS基础知识详解:创建、发送、接收与读取消息】 Java消息服务(Java Message Service,简称JMS)是一个标准接口,它定义了应用程序如何创建、发送、接收和读取消息的标准API。JMS允许分布式系统中的不同组件通过...

    JMS demo 及 资料

    在这个"JMS demo 及 资料"的压缩包中,我们可能找到了一些关于JMS的基础教程和示例代码,帮助初学者了解和掌握JMS的基本用法。 JMS的核心概念主要包括以下几个部分: 1. **消息**: 消息是JMS中的基本数据单元,它...

    JMS消息模型 JMS学习.doc

    JMS的核心概念包括: 1. **JMS Provider**:JMS提供者是实现JMS规范的具体产品,它是消息服务的核心。它为应用提供消息的创建、发送、接收以及管理等功能。JMS提供者通常是跨平台的,可以在各种环境中运行,比如在...

    JBOSS建立JMS应用实例

    一、JMS基础知识 1. 概念理解:JMS是Java平台中用于企业级消息传递的API,它定义了生产、发送、接收和消费消息的标准接口。 2. 消息模型:JMS支持两种消息模型——点对点(Point-to-Point)和发布/订阅(Publish/...

    JMS基础

    **JMS基础** Java消息服务(Java Message Service,简称JMS)是Java平台中用于企业级应用集成(Enterprise Integration)的一个标准API,它为应用程序提供了一种标准的方式来创建、发送、接收和读取消息。JMS是Java...

    Jms基础知识整理(转)

    **JMS基础知识整理** JMS(Java Message Service)是Java平台中用于企业级应用间进行异步消息传递的一种标准API。它提供了一种统一的消息接口,使得开发人员可以在不同的消息中间件之间进行选择,而无需关心底层...

    JMS规范教程pdf

    ### JMS规范教程知识点解析 #### 一、JMS基本概念 **1.1 什么是JMS(JAVA MESSAGE SERVICE)?...JMS不仅提供了消息传递的基础结构,还涵盖了消息的管理和控制,使得开发者能够构建高效、可靠和灵活的企业级应用。

    spring-jms入门

    本文将深入探讨Spring-JMS的基础知识,包括它的核心概念、配置以及如何与ActiveMQ这样的消息中间件进行集成。 **1. JMS简介** Java消息服务(Java Message Service,简称JMS)是一个标准,定义了应用程序如何创建、...

    JMS标准规范培训教程

    **正文** JMS(Java Message Service)是...SUN JMS 1.1规范是这个领域的基础,而SUN MQ则是这个规范的一个实现例证。通过深入学习"JMS规范培训教程",开发者能够更好地理解和应用JMS,构建高效、稳定的分布式系统。

    Weblogic JMS 依赖jar包下载

    Weblogic JMS的核心概念包括: 1. **消息**:JMS中的消息是数据的载体,它可以携带任何类型的数据,并在生产者和消费者之间传递。 2. **消息生产者**:负责创建和发送消息到目的地的组件。 3. **消息消费者**:接收...

    JMS 学习资源

    #### 一、JMS 基本概念与通信模型 JMS(Java Message Service)是一种广泛应用于企业级应用程序之间的消息传递标准,它定义了一系列接口,用于创建、发送、接收消息。JMS 的设计目标是简化应用程序之间的通信流程,...

    ActiveMQ学习笔记(二) JMS与Spring

    1. **JMS基础概念** - **消息模型**:JMS定义了两种基本的消息模型——点对点(Queue)和发布/订阅(Topic)。点对点模式下,消息由一个生产者发送到队列,一个消费者接收;发布/订阅模式下,多个消费者可以订阅一...

    JMS简单示例1

    **JMS核心概念** 1. **消息(Message)**:JMS中的基本数据单元,包含数据和元数据(如目标地址、优先级等)。消息可以是文本、对象、文件或其他形式的数据。 2. **消息生产者(Message Producer)**:创建并发送...

    JMS 使用 ActiveMQ 传送文件

    3. **jms规范教程.pdf** - 这是一个关于JMS规范的教程,可能详细讲解了JMS接口、消息类型(如点对点和发布/订阅模型)、消息队列和主题等核心概念,为理解JMS和ActiveMQ的工作原理提供了基础。 综上所述,这个主题...

    jms整合spring工程

    1. **JMS基础概念** - **消息**:在JMS中,消息是数据传输的基本单位,包含要传递的信息。 - **消息队列(Message Queue)**:存储消息的临时容器,确保消息被正确地发送和接收。 - **生产者(Producer)**:创建...

    Sun 官方JMS教程

    JMS API定义了一系列关键的接口和类,它们是实现消息通信的基础。其中最重要的包括: - **ConnectionFactory**:用于创建连接。 - **Connection**:表示到消息服务器的连接。 - **Session**:在一个连接中创建消息的...

    JMS学习手册

    JMS学习手册是一份宝贵的参考资料,它涵盖了JMS的基础概念、程序开发、配置和监控等方面的知识,尤其适合初学者以及希望加深理解JMS原理和实践的开发者。 JMS规范定义了一组接口和相关语义,这些接口用于创建、发送...

    java-jms小例子

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

    JMS规范培训教程 中文版

    本“JMS规范培训教程”涵盖了JMS的基础知识、高级特性以及最佳实践。PDF文档中可能包含了以下章节:JMS简介、消息模型、消息格式、消息代理的配置与管理、JMS API详解、案例研究和实战演练等内容。通过学习这份教程...

Global site tag (gtag.js) - Google Analytics