`
单眼皮大娘
  • 浏览: 110992 次
  • 性别: 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
分享到:
评论

相关推荐

    WSAD环境下JMS异步通信全攻略

    一、JMS基本概念  1.1 P2P通信  1.2 Pub/Sub通信 二、JMS消息 三、JMS P2P编程  3.1 使用JMS QueueConnection对象  3.2 处理回退事件  3.3 关闭JMS对象  3.4 接收消息  3.5 消息驱动的Bean...

    RabbitMQ技术详解

    本文来自于网络,本文主要介绍了RabbitMQ是什么,RabbitMQ为何会出现,RabbitMQ基础概念,RabbitMQ集群等。RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python、Ruby、.NET、Java...

    从Java走向Java+EE+.rar

    5.1 Servlet的概念和生命周期 65 5.2 如何编写Servlet 67 5.3 使用Eclipse和Tomcat开发Servlet实例——输出字符串响应 72 5.4 小结 77 第6章 JSP——前后台更好地分离 79 6.1 JSP的概念 79 6.2 JSP页面...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    第二部分详细讲解了jsf ri、jta、jndi、rmi、jms、javamail、ejb 3的session bean、message driven bean、jpa、jax-ws 2、jaas等java ee知识,这部分知识以jsf+ejb 3+jpa整合开发为重点,通过使用netbeans ide工具...

    Spring in Action中文版 清晰pdf part2

    第二部分在第1章介绍的IoC和AOP基础之上,展示了如何将这两个概念应用到应用系统的中间层。第三部分走出中间层迈向显示层,在很多J2EE系统中显示层被称为Web层。附录A介绍如何开始自己的Spring应用系统,如何下载...

    Spring in Action中文版 清晰pdf part1

    第二部分在第1章介绍的IoC和AOP基础之上,展示了如何将这两个概念应用到应用系统的中间层。第三部分走出中间层迈向显示层,在很多J2EE系统中显示层被称为Web层。附录A介绍如何开始自己的Spring应用系统,如何下载...

    Spring攻略PDF版

    第二部分 基础主题  第7章 Spring对JDBC的支持   第8章 Spring中的事务管理   第9章 Spring对ORM的支持   第10章 Spring MVC框架   第11章 整合Spring与其他Web框架   第12章 Spring对测试...

    Spring攻略中文版PDF

    第二部分 基础主题  第7章 Spring对JDBC的支持   第8章 Spring中的事务管理   第9章 Spring对ORM的支持   第10章 Spring MVC框架   第11章 整合Spring与其他Web框架   第12章 Spring对测试...

    Java 2技术内幕(Part1)

    全书贯穿了一个用Java 编写的实际应用,可供读者理解Java的各种概念和编程方法,并且所有源代码可以从网上下载。本书适合有一定Java语言基础的编程人员使用。 主要内容: 使用Enterprise JavaBean(EJB)开发可伸缩的...

    Java 2技术内幕(Part2)

    全书贯穿了一个用Java 编写的实际应用,可供读者理解Java的各种概念和编程方法,并且所有源代码可以从网上下载。本书适合有一定Java语言基础的编程人员使用。 主要内容: 使用Enterprise JavaBean(EJB)开发可伸缩的...

    Java 2技术内幕(Part5)

    全书贯穿了一个用Java 编写的实际应用,可供读者理解Java的各种概念和编程方法,并且所有源代码可以从网上下载。本书适合有一定Java语言基础的编程人员使用。 主要内容: 使用Enterprise JavaBean(EJB)开发可伸缩的...

    Java 2技术内幕(Part3)

    全书贯穿了一个用Java 编写的实际应用,可供读者理解Java的各种概念和编程方法,并且所有源代码可以从网上下载。本书适合有一定Java语言基础的编程人员使用。 主要内容: 使用Enterprise JavaBean(EJB)开发可伸缩的...

    Java 2技术内幕(Part4)

    全书贯穿了一个用Java 编写的实际应用,可供读者理解Java的各种概念和编程方法,并且所有源代码可以从网上下载。本书适合有一定Java语言基础的编程人员使用。 主要内容: 使用Enterprise JavaBean(EJB)开发可伸缩的...

    Spring3.X编程技术与应用,完整扫描版

    全书分3篇共21章,具体内容包括:Spring环境的安装与使用、JSP与JSTL简介、 Spring基础概念与工具、用SpringJdbcTemplate访问数据库、使用Mayen工程、Spring MVC编程、基于 MVC的资源共享网站设计、Spring的AOP编程...

    J2EE学习笔记

    3.1:语言基础知识 118 3.2:声明和访问控制 120 3.3:运算符和赋值 121 3.4:流程控制、异常处理和断言 121 3.5:面向对象、重载和重写、构造函数和返回类型 121 3.6:Java.lang-----Math类、字符串和封装类 121 ...

    java基础题 很全面

    Java基础 6 1. 面向对象的特征有哪些方面 6 2. String是最基本的数据类型吗? 7 3. int 和 Integer 有什么区别 7 4. String 和StringBuffer的区别 7 5. 运行时异常与一般异常有何异同? 7 6. 说出ArrayList,Vector, ...

    Spring攻略英文版(附带源码)

    第二部分 基础主题  第7章 Spring对JDBC的支持   第8章 Spring中的事务管理   第9章 Spring对ORM的支持   第10章 Spring MVC框架   第11章 整合Spring与其他Web框架   第12章 Spring对测试的支持...

    JBPM4工作流应用开始指南.rar

    第一篇 jBPM工作流开发基础 1 第1章 工作流基础 2 1.1 工作流概念 2 1.1.1 工作流管理思想之于企业现代化管理 2 1.1.2 工作流技术在企业中的应用 5 1.1.3 如何从一个开发者的角度看工作流技术 6 1.2 工作流管理系统...

    Oracle WebLogic Server开发权威指南

    本书跳过了基础内容,避免重复很容易从其他地方找到的信息,关注其他地方没有 提到的信息和技术。本书由Oracle 公司权威人士和企业级Java EE 应用程序开发专家组成的作者 团队编写,是对其他书籍和参考资料的延伸。 ...

Global site tag (gtag.js) - Google Analytics