﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-JAVA流通桥-文章分类-JMS文章</title><link>http://www.blogjava.net/zhuyan/category/24710.html</link><description>JAVA启发者</description><language>zh-cn</language><lastBuildDate>Fri, 10 Aug 2007 09:40:09 GMT</lastBuildDate><pubDate>Fri, 10 Aug 2007 09:40:09 GMT</pubDate><ttl>60</ttl><item><title>JMS入门介绍</title><link>http://www.blogjava.net/zhuyan/articles/135168.html</link><dc:creator>朱岩</dc:creator><author>朱岩</author><pubDate>Wed, 08 Aug 2007 02:12:00 GMT</pubDate><guid>http://www.blogjava.net/zhuyan/articles/135168.html</guid><wfw:comment>http://www.blogjava.net/zhuyan/comments/135168.html</wfw:comment><comments>http://www.blogjava.net/zhuyan/articles/135168.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhuyan/comments/commentRss/135168.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhuyan/services/trackbacks/135168.html</trackback:ping><description><![CDATA[<a href="http://www.itisedu.com/phrase/200604261605045.html" target=_new><u><font face=Verdana color=#800080>JMS</font></u></a><font face=Verdana>（</font><a href="http://www.itisedu.com/phrase/200604261607125.html" target=_new><u><font face=Verdana color=#0000ff>Java Message Service</font></u></a><font face=Verdana>，</font><a href="http://www.itisedu.com/phrase/200604261600355.html" target=_new><u><font face=Verdana color=#0000ff>Java消息服务</font></u></a><font face=Verdana>）是一组Java应用</font><a href="http://www.itisedu.com/phrase/200604232224305.html" target=_new><u><font face=Verdana color=#0000ff>程序</font></u></a><font face=Verdana>接口(Java </font><a href="http://www.itisedu.com/phrase/200604241228185.html" target=_new><u><font face=Verdana color=#0000ff>API</font></u></a><font face=Verdana>)，它提供创建、发送、接收、读取</font><a href="http://www.itisedu.com/phrase/200603090938465.html" target=_new><u><font face=Verdana color=#0000ff>消息</font></u></a><font face=Verdana>的服务。由Sun公司和它的合作伙伴设计的JMS API定义了一组公共的应用程序接口和相应语法，使得Java程序能够和其他消息</font><a href="http://www.itisedu.com/phrase/200603302222545.html" target=_new><u><font face=Verdana color=#0000ff>组件</font></u></a><font face=Verdana>进行通信。 </font>
<p><font face=Verdana>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JMS是一种与厂商无关的 API，用来访问消息收发系统。它</font><a href="http://www.itisedu.com/phrase/200603090857555.html" target=_new><font face=Verdana color=#0000ff><u>类</u></font></a><font face=Verdana>似于 </font><a href="http://www.itisedu.com/phrase/200604151904545.html" target=_new><font face=Verdana color=#0000ff><u>JDBC</u></font></a><font face=Verdana> (Java </font><a href="http://www.itisedu.com/phrase/200604231244235.html" target=_new><font face=Verdana color=#0000ff><u>Database</u></font></a><font face=Verdana> Connectivity)：这里，JDBC 是可以用来访问许多不同关系</font><a href="http://www.itisedu.com/phrase/200602271218062.html" target=_new><font face=Verdana color=#0000ff><u>数据库</u></font></a><font face=Verdana>的 API，而 JMS 则提供同样与厂商无关的访问方法，以访问消息收发服务。许多厂商目前都支持 JMS，包括 IBM 的 MQSeries、BEA的 Weblogic JMS service和 Progress 的 SonicMQ，这只是几个例子。 <br>JMS 使您能够通过消息收发服务（有时称为消息中介程序或路由器）从一个 JMS 客户机向另一个 JML 客户机发送消息。消息是 JMS 中的一种</font><a href="http://www.itisedu.com/phrase/200603051002565.html" target=_new><font face=Verdana color=#0000ff><u>类型</u></font></a><a href="http://www.itisedu.com/phrase/200603090845215.html" target=_new><font face=Verdana color=#0000ff><u>对象</u></font></a><font face=Verdana>，由两部分组成：报头和消息主体。报头由路由信息以及有关该消息的</font><a href="http://www.itisedu.com/phrase/200603141328355.html" target=_new><font face=Verdana color=#0000ff><u>元数据</u></font></a><font face=Verdana>组成。消息主体则携带着应用程序的数据或有效负载。根据有效负载的类型来划分，可以将消息分为几种类型，它们分别携带：简单文本 (TextMessage)、可序列化的对象 (ObjectMessage)、属性集合 (MapMessage)、字节流 (BytesMessage)、原始值流 (StreamMessage)，还有无有效负载的消息 (Message)。 </font></p>
<p><font face=Verdana>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 消息收发系统是异步的，也就是说，JMS 客户机可以发送消息而不必等待回应。比较可知，这完全不同于基于 RPC 的（基于远程过程的）系统，如 <a href="http://www.itisedu.com/phrase/200604241156485.html" target=_new><u><font color=#0000ff>EJB</font></u></a> 1.1、<a href="http://www.itisedu.com/phrase/200604031336425.html" target=_new><u><font color=#0000ff>CORBA</font></u></a> 和 Java <a href="http://www.itisedu.com/phrase/200604281025035.html" target=_new><u><font color=#0000ff>RMI</font></u></a> 的引用实现。在 RPC 中，客户机调用服务器上某个分布式对象的一个方法。在方法调用返回之前，该客户机被阻塞；该客户机在可以执行下一条指令之前，必须等待方法调用结束。在 JMS 中，客户机将消息发送给一个虚拟通道（主题或队列），而其它 JMS 客户机则预订或监听这个虚拟通道。当 JMS 客户机发送消息时，它并不等待回应。它执行发送操作，然后继续执行下一条指令。消息可能最终转发到一个或许多个客户机，这些客户机都不需要作出回应。 </font></p>
<p><font face=Verdana>　　JMS的通用接口集合以异步方式发送或接收消息。异步方式接收消息显然是使用间断网络连接的客户机，诸如移动电话和PDA的最好的选择。另外， JMS采用一种宽松结合方式整合企业系统的方法，其主要的目的就是创建能够使用跨平台数据信息的、可移植的企业级应用程序，而把开发人力解放出来。</font></p>
<p><font face=Verdana>　　Java消息服务支持两种消息模型：Point-to-Point消息(<a href="http://www.itisedu.com/phrase/200604022225155.html" target=_new><u><font color=#0000ff>P2P</font></u></a>)和发布订阅消息（Publish Subscribe messaging，简称Pu<a href="http://www.itisedu.com/phrase/200604291152445.html" target=_new><u><font color=#0000ff>b/S</font></u></a>ub）。JMS规范并不要求供应商同时支持这两种消息模型，但开发者应该熟悉这两种消息模型的优势与缺点。</font></p>
<p><font face=Verdana>　　P2P消息模型是在点对点之间传递消息时使用。如果应用程序开发者希望每一条消息都能够被处理，那么应该使用P2P消息模型。与Pub/Sub消息模型不同，P2P消息总是能够被传送到指定的位置。</font></p>
<p><font face=Verdana>　　Pub/Sub模型在一到多的消息广播时使用。如果一定程度的消息传递的不可靠性可以被接受的话，那么应用程序开发者也可以使用Pub/Sub消息模型。换句话说，它适用于所有的消息消费程序并不要求能够收到所有的信息或者消息消费程序并不想接收到任何消息的情况。</font></p>
<p><font face=Verdana>　　JMS通过允许创建持久订阅来简化时间相关性，即使消息预订者未激活也可以接收到消息。此外，使用持久订阅还可通过队列提供灵活性和可靠性，而仍然允许消息被发给许多的接收者。 Topic Subscriber topic Subscriber = topicSession.createDurableSubscriber(topic, subscriptionName); 　　Connection对象表示了到两种消息模型中的任一种的消息系统的连接。服务器端和客户机端对象要求管理创建的JMS连接的状态。连接是由Connection Factory创建的并且通过JNDI查寻定位。 //取得用于 P2P的 QueueConnectionFactory QueueConnectionFactory = queueConnectionFactory( ); Context messaging = new InitialContext( ); QueueConnectionFactory = (QueueConnectionFactory) Messaging.lookup(&#8220;QueueConnectionFactory&#8221;); //取得用于 pub/sub的 TopicConnectionFactory TopicConnectonFactory topicConnectionFactory; Context messaging = new InitialContext(); topicConnectionFactory = (TopicConnectionFactory) messaging.lookup(&#8220;TopicConnectionFactory&#8221;); 　　注意：用于P2P的代码和用于PublishSubscribe的代码非常相似。</font></p>
<p><font face=Verdana>　　如果session被标记为transactional的话，确认消息就通过确认和校正来自动地处理。如果session没有标记为 transactional，你有三个用于消息确认的选项。</font></p>
<p><font face=Verdana>　　&#183; AUTO_ACKNOWLEDGE session将自动地确认收到一则消息。</font></p>
<p><font face=Verdana>　　&#183; <a href="http://www.itisedu.com/phrase/200604231337375.html" target=_new><u><font color=#0000ff>CLIENT</font></u></a>_ACKNOWLEDGE <a href="http://www.itisedu.com/phrase/200603082208195.html" target=_new><u><font color=#0000ff>客户端</font></u></a>程序将确认收到一则消息，调用这则消息的确认方法。 　　　&#183; DUPS_OK_ACKNOWLEDGE 这个选项命令session&#8220;懒散的&#8221;确认消息传递，可以想到，这将导致消息提供者传递的一些复制消息可能会出错。这种确认的方式只应当用于消息消费程序可以容忍潜在的副本消息存在的情况。 queueSession = queueConnection.createQueueSession(false, session.AUTO_ACKNOWLEDGE);//P2P topicSession = topicConnection.createTopicSession(false, session.AUTO_ACKNOWLEDGE); //Pub-Sub</font></p>
<p><font face=Verdana>　　注意：在本例中，一个session目的从连结中创建，非值指出session是non-transactional的，并且 session将自动地确认收到一则消息。</font></p>
<p><font face=Verdana>　　JMS现在有两种传递消息的方式。标记为NON_PERSISTENT的消息最多投递一次，而标记为PERSISTENT的消息将使用暂存后再转送的机理投递。如果一个JMS服务离线，那么持久性消息不会丢失但是得等到这个服务恢复联机时才会被传递。所以默认的消息传递方式是非持久性的。即使使用非持久性消息可能降低内务和需要的存储器，并且这种传递方式只有当你不需要接收所有的消息时才使用。</font></p>
<p><font face=Verdana>　　虽然 JMS规范并不需要JMS供应商实现消息的优先级路线，但是它需要递送加快的消息优先于普通级别的消息。JMS定义了从0到9的优先级路线级别，0是最低的优先级而9则是最高的。更特殊的是0到4是正常优先级的变化幅度，而5到9是加快的优先级的变化幅度。举例来说： topicPublisher.publish (message, DeliveryMode.PERSISTENT, 8, 10000); //Pub-Sub 或 queueSender.send(message, DeliveryMode.PERSISTENT, 8, 10000);//P2P 　　这个代码片断，有两种消息模型，映射递送方式是持久的，优先级为加快型，生存周期是10000 (以毫秒<a href="http://www.itisedu.com/phrase/200604231331545.html" target=_new><u><font color=#0000ff>度量</font></u></a> )。如果生存周期设置为零，这则消息将永远不会过期。当消息需要时间限制否则将使其无效时，设置生存周期是有用的。</font></p>
<p><font face=Verdana>　　JMS定义了五种不同的消息正文格式，以及调用的消息类型，允许你发送并接收以一些不同形式的数据，提供现有消息格式的一些级别的兼容性。</font></p>
<p><font face=Verdana>　　&#183; StreamMessage -- Java原始值的数据流 </font></p>
<p><font face=Verdana>　　&#183; MapMessage--一套名称-值对</font></p>
<p><font face=Verdana>　　&#183; TextMessage--一个字符串对象</font></p>
<p><font face=Verdana>　　&#183; ObjectMessage--一个序列化的 Java对象</font></p>
<p><font face=Verdana>　　&#183; BytesMessage--一个未解释字节的数据流</font></p>
<p><font face=Verdana>　　JMS应用程序接口提供用于创建每种类型消息和设置荷载的方法例如，为了在一个队列创建并发送一个TextMessage实例，你可以使用下列语句： TextMessage message = queueSession.createTextMessage(); message.setText(textMsg); 　　以异步方式接收消息，需要创建一个消息监听器然后注册一个或多个使用MessageConsumer的JMS MessageListener接口。会话(主题或队列)负责产生某些消息，这些消息被传送到使用onMessage方法的监听者那里。 import javax.jms.*; public <a href="http://www.itisedu.com/phrase/200604231359565.html" target=_new><u><font color=#0000ff>class</font></u></a> ExampleListener implements MessageListener { //把消息强制转化为TextMessage格式 public void onMessage(Message message) { TextMessage textMsg = null; // 打开并处理这段消息 } } 　　当我们创建QueueReceiver和TopicSubscriber时，我们传递消息选择器字符串： //P2P QueueReceiver QueueReceiver receiver; receiver = session.createReceiver(queue, selector); //Pub-Sub TopicSubscriber TopicSubscriber subscriber; subscriber = session.createSubscriber(topic, selector); 　　为了启动消息的交付，不论是Pub/Sub还是P2P，都需要调用start方法。 TopicConnection.start( ); //pub-sub QueueConnection.start( ); //P2P TopicConnection.start ( );// pub-sub QueueConnection.start ( );// P2P</font></p>
<p><font face=Verdana>　　当一条消息被捕捉时，这条消息做为一条必须被强制转化为适当消息类型的普通Message对象到达。这是一个被用来提取或打开消息内容的getter方法。下列代码片段使用StreamMessage类型。 private void unPackMessage (Message message) { String eName; String position; double rate; StreamMessage message; Message = session.createStreamMessage( ); //注意下面的代码必须按照我给出的顺序书写 message.writeString(eName); message.writeString(position); message.writeDouble(rate); //实现处理消息的必要的程序逻辑 }</font></p>
<p><font face=Verdana>　　停止消息的传递，无论是Pub/Sub还是P2P，都调用stop方法。 TopicConnection.start( ); //pub-sub QueueConnection.start( ); //P2P TopicConnection.start ( );// pub-sub QueueConnection.start ( );// P2P 　　其他的<a href="http://www.itisedu.com/phrase/200603091447335.html" target=_new><u><font color=#0000ff>J2EE</font></u></a>组件--<a href="http://www.itisedu.com/phrase/200603091005185.html" target=_new><u><font color=#0000ff>servlet</font></u></a>或EJB--可以当作消息生产者；然而，它们可能只能同步操作，这可能是因为它们的请求-应答的性质决定的。虽然<a href="http://www.itisedu.com/phrase/200604231236585.html" target=_new><u><font color=#0000ff>XML</font></u></a>目前还不是被支持的消息类型，发送一个XML<a href="http://www.itisedu.com/phrase/200602282323195.html" target=_new><u><font color=#0000ff>文件</font></u></a>和创建一条文本类型消息以及把XML文件添加到消息的有效负载都一样简单，都是以非专有的方式传送数据。值得注意的是，一些JMS供应厂商已经提供了可用的XML消息类型。但是使用非标准的消息类型可能会出现可移植性问题。 String reportData; //reportData内容为XML 文档 TextMessage message; message = session.createTextMessage(); message.setText (reportData);</font></p>
<p><font face=Verdana>　　消息驱动组件(MDB)是一个当消息到达时被容器调用的异步消息消费程序。和entity和session EJB不同，MDB没有本地和远程接口并且是匿名的；它们对于客户是不可见的。MDB是JMS系统的一部分，作为消费者实现服务器上的商业逻辑程序。 一个客户程序可能通过使用JNDI定位一个与MDB相关联的JMS。 例如： Context initialContext = new InitialContext(); Queue reportInfoQueue = (javax.jms.Queue)initialContext.lookup (&#8220;java:comp/env/jms/reportInfoQueue&#8221;); 　　MDB是由Bean类和相应的XML部署描述符组成。 Bean 类实现MessageDriveBean 接口： import javax.ejb.*; import jms.Message.*; public interface MessageDriveBean { public void ejbCreate(); public void ejbRemove(); public void setMessageDrivenContext(MessageDrivenContext ctx); } 　　消息监听器接口： import javax.jms.*; public interface MessageListener { public void onMessage( ); }</font></p>
<p><font face=Verdana>　　部署描述符 ＜!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd"＞ ＜ejb-jar＞ ＜enterprise-beans＞ ＜message-driven＞ ＜ejb-name＞MDB＜/ejb-name＞ ＜ejb-class＞MDB＜/ejb-class＞ ＜transaction-type＞Container＜/transaction-type＞ ＜message-driven-destination＞ ＜jms-destination-type＞javax.jms.Queue＜/jms-destination-type＞ ＜/message-driven-destination＞ ＜security-identity＞ ＜run-as-specified-identity＞ ＜role-name＞everyone＜/role-name＞ ＜/run-as-specified-identity＞ ＜/security-identity＞ ＜/message-driven＞ ＜/enterprise-beans＞ ＜/ejb-jar＞</font></p>
<p><font face=Verdana>&nbsp;　　既然我们现在已经有了一些基本的JMS知识，那么我们可以使用JMS做什么呢？任何事情都可以。</font></p>
<p><font face=Verdana>&nbsp;　　例如，分别用于销售、库存、客户服务和账目处理的系统。这些部门之间的系统很可能已经存在了很长时间，这些处理要求把事务移动到系统中去，这并不是一个小的工作。这就是消息服务适用的地点。</font></p>
<p><font face=Verdana>　　当售货员完成销售的时候，一条消息被发给库存系统；一旦订单消息发送给收发货人员，就可以按照订单出货了。当订单成功地发货，系统将通知顾客服务和会计系统这个订单已经成功的交易了。所有对应的每个<a href="http://www.itisedu.com/phrase/200604161433025.html" target=_new><u><font color=#0000ff>子系统</font></u></a>都自动地根据收到的消息进行更新。</font></p>
<p><font face=Verdana>　　JMS一般都不是用来整合一个系统，而是整合许多可能参与消息驱动环境的系统。JMS是一个用于开发和集成企业应用程序的重要的工具。因为许多公司都有以前遗留下来的系统和新近开发的系统综合起来的系统，消息的使用是整合整个企业的重要的步骤。 </font></p>
<p><font face=Verdana>　　 </font></p>
<p><font face=Verdana><strong>JMS接口描述</strong></font></p>
<p><font face=Verdana>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JMS 支持两种消息类型PTP 和Pub/Sub，分别称作：PTP Domain 和Pub/Sub Domain，这两种接口都继承统一的JMS Parent 接口，JMS 主要接口如下所示：</font></p>
<font face=Verdana>
<p align=center>
<table class=table border=1>
    <tbody>
        <tr>
            <td class=th width=138><strong>JMS Parent</strong>&nbsp;&nbsp;</td>
            <td class=th width=210>&nbsp;<strong>PTPDomain</strong></td>
            <td class=th width=214><strong>Pub/Sub Domain</strong> </td>
        </tr>
        <tr>
            <td class=td width=138>ConnectionFactory</td>
            <td class=td width=210>QueueConnectionFactory</td>
            <td class=td width=214>TopicConnectionFactory</td>
        </tr>
        <tr>
            <td class=td width=138>Connection</td>
            <td class=td width=210>QueueConnection</td>
            <td class=td width=214>TopicConnection</td>
        </tr>
        <tr>
            <td class=td width=138>Destination</td>
            <td class=td width=210>Queue</td>
            <td class=td width=214>Topic</td>
        </tr>
        <tr>
            <td class=td width=138>Session</td>
            <td class=td width=210>QueueSession</td>
            <td class=td width=214>TopicSession</td>
        </tr>
        <tr>
            <td class=td width=138>MessageProducer</td>
            <td class=td width=210>QueueSender</td>
            <td class=td width=214>TopicPublisher</td>
        </tr>
        <tr>
            <td class=td width=138>MessageConsumer</td>
            <td class=td width=210>QueueReceiver,QueueBrowser</td>
            <td class=td width=214>TopicSubscriber</td>
        </tr>
    </tbody>
</table>
</p>
</font>
<p><font face=Verdana>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 以下是对这些接口的简单描述：<br>&nbsp;&nbsp;&nbsp; ConnectionFactory ：连接工厂，JMS 用它创建连接<br>&nbsp;&nbsp;&nbsp; Connection ：JMS 客户端到JMS Provider 的连接<br>&nbsp;&nbsp;&nbsp; Destination ：消息的目的地<br>&nbsp;&nbsp;&nbsp; Session： 一个发送或接收消息的<a href="http://www.itisedu.com/phrase/200603091754305.html" target=_new><u><font color=#0000ff>线程</font></u></a><br>&nbsp;&nbsp;&nbsp; MessageProducer： 由Session 对象创建的用来发送消息的对象<br>&nbsp;&nbsp;&nbsp; MessageConsumer： 由Session 对象创建的用来接收消息的对象</font></p>
<p><font face=Verdana></font>&nbsp;</p>
<p><font face=Verdana><strong>JMS消息模型</strong></font></p>
<p><font face=Verdana>JMS 消息由以下几部分组成：消息头，属性，消息体。</font></p>
<p><font face=Verdana>&nbsp;&nbsp;&nbsp; <strong>消息头(Header)</strong> - 消息头包含消息的识别信息和路由信息，消息头包含一些标准的属性如：JMSDestination,JMSMessageID 等。 </font></p>
<p align=center><font face=Verdana>
<table class=table border=1>
    <tbody>
        <tr>
            <td class=th width=150><strong><em>消息头</em></strong></td>
            <td class=th width=515><strong><em>由谁设置</em></strong></td>
        </tr>
        <tr>
            <td class=td width=150>JMSDestination</td>
            <td class=td width=515>send 或 publish 方法</td>
        </tr>
        <tr>
            <td class=td width=150>JMSDeliveryMode</td>
            <td class=td width=515>send 或 publish 方法</td>
        </tr>
        <tr>
            <td class=td width=150>JMSExpiration</td>
            <td class=td width=515>send 或 publish 方法</td>
        </tr>
        <tr>
            <td class=td width=150>JMSPriority</td>
            <td class=td width=515>send 或 publish 方法</td>
        </tr>
        <tr>
            <td class=td width=150>JMSMessageID</td>
            <td class=td width=515>send 或 publish 方法</td>
        </tr>
        <tr>
            <td class=td width=150>JMSTimestamp</td>
            <td class=td width=515>send 或 publish 方法</td>
        </tr>
        <tr>
            <td class=td width=150>JMSCorrelationID</td>
            <td class=td width=515>客户</td>
        </tr>
        <tr>
            <td class=td width=150>JMSReplyTo</td>
            <td class=td width=515>客户</td>
        </tr>
        <tr>
            <td class=td width=150>JMSType</td>
            <td class=td width=515>客户</td>
        </tr>
        <tr>
            <td class=td width=150>JMSRedelivered</td>
            <td class=td width=515>JMS Provider</td>
        </tr>
    </tbody>
</table>
</font></p>
<p><font face=Verdana><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>属性(Properties)</strong> - 除了消息头中定义好的标准属性外，JMS 提供一种机制增加新属性到消息头中，这种新属性包含以下几种：<br>&nbsp;&nbsp;&nbsp; 1. 应用需要用到的属性;<br>&nbsp;&nbsp;&nbsp; 2. 消息头中原有的一些可选属性;<br>&nbsp;&nbsp;&nbsp; 3. JMS Provider 需要用到的属性。<br>&nbsp;&nbsp;&nbsp; 标准的JMS 消息头包含以下属性： <br>&nbsp;&nbsp; JMSDestination --消息发送的目的地 <br>&nbsp;&nbsp; JMSDeliveryMode --传递<a href="http://www.itisedu.com/phrase/200603061709535.html" target=_new><u><font color=#0000ff>模式</font></u></a>， 有两种模式： PERSISTENT 和NON_PERSISTENT，PERSISTENT 表示该消息一定要被送到目的地，否则会导致应用错误。NON_PERSISTENT 表示偶然丢失该消息是被允许的，这两种模式使开发者可以在消息传递的可靠性和吞吐量之间找到平衡点。<br>&nbsp;&nbsp; JMSMessageID 唯一识别每个消息的标识，由JMS Provider 产生。<br>&nbsp;&nbsp; JMSTimestamp 一个消息被提交给JMS Provider 到消息被发出的时间。<br>&nbsp;&nbsp; JMSCorrelationID 用来连接到另外一个消息，典型的应用是在回复消息中连接到原消息。<br>&nbsp;&nbsp; JMSReplyTo 提供本消息回复消息的目的地址。<br>&nbsp;&nbsp; JMSRedelivered 如果一个客户端收到一个设置了JMSRedelivered 属性的消息，则表示可能该客户端曾经在早些时候收到过该消息，但并没有签收(acknowledged)。<br>&nbsp;&nbsp; JMSType 消息类型的识别符。<br>&nbsp;&nbsp; JMSExpiration 消息过期时间，等于QueueSender 的send 方法中的timeToLive 值或TopicPublisher 的publish 方法中的timeToLive 值加上发送时刻的GMT 时间值。如果timeToLive值等于零，则JMSExpiration 被设为零，表示该消息永不过期。如果发送后，在消息过期时间之后消息还没有被发送到目的地，则该消息被清除。<br>&nbsp;&nbsp; JMSPriority 消息优先级，从0-9 十个级别，0-4 是普通消息，5-9 是加急消息。JMS 不要求JMS Provider 严格按照这十个优先级发送消息，但必须保证加急消息要先于普通消息到达。</p>
<p>&nbsp;&nbsp;&nbsp; <strong>消息体(Body) </strong>- JMS API 定义了5种消息体格式，也叫消息类型，你可以使用不同形式发送接收数据并可以兼容现有的消息格式，下面描述这5种类型： </p>
<p align=center>
<table class=table border=1>
    <tbody>
        <tr>
            <td class=th width=150><strong><em>消息类型</em></strong></td>
            <td class=th width=515><strong><em>消息体</em></strong></td>
        </tr>
        <tr>
            <td class=td width=150>TextMessage</td>
            <td class=td width=515>java.lang.String对象，如xml文件内容</td>
        </tr>
        <tr>
            <td class=td width=150>MapMessage</td>
            <td class=td width=515>名/值对的集合，名是String对象，值类型可以是Java任何基本类型</td>
        </tr>
        <tr>
            <td class=td width=150>BytesMessage</td>
            <td class=td width=515>字节流</td>
        </tr>
        <tr>
            <td class=td width=150>StreamMessage</td>
            <td class=td width=515>Java中的输入输出流</td>
        </tr>
        <tr>
            <td class=td width=150>ObjectMessage</td>
            <td class=td width=515>Java中的可序列化对象</td>
        </tr>
        <tr>
            <td class=td width=150>Message</td>
            <td class=td width=515>没有消息体，只有消息头和属性。</td>
        </tr>
    </tbody>
</table>
</p>
<div class=para>下例演示创建并发送一个TextMessage到一个队列： </div>
<p align=center>&nbsp;</p>
<div class=programlisting style="WIDTH: 674px; HEIGHT: 29px">
<blockquote>
<pre>TextMessage message = queueSession.createTextMessage();
message.setText(msg_text); // msg_text is a String
queueSender.send(message);</pre>
</blockquote></div>
<p align=center>&nbsp;</p>
<div class=para>下例演示接收消息并转换为合适的消息类型： </div>
<p align=center>&nbsp;</p>
<div class=programlisting style="WIDTH: 674px; HEIGHT: 29px">
<blockquote>
<pre>Message m = queueReceiver.receive();
if (m instanceof TextMessage) {
TextMessage message = (TextMessage) m;
System.out.println("Reading message: " + message.getText());
} else {
// Handle error
}</pre>
</blockquote></div>
</font><font face=Verdana>（来源：club.169.netsprite.com；www.wxsf.net）</font>
<img src ="http://www.blogjava.net/zhuyan/aggbug/135168.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhuyan/" target="_blank">朱岩</a> 2007-08-08 10:12 <a href="http://www.blogjava.net/zhuyan/articles/135168.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>