62 / 66 …………………………………………………………Page 63…………………………………………………………… stockTime = message。getLong(〃Time〃); 如果客户端需要得到 MapMessage 的元素列表,那么它可以使用 MapMessage。getMapNames 方法。 9。4。7 创建StreamMessage 与MapMessage 类似,应用可以发送由按序写入消息的不同字段组成的消息,每个字段 都是原始数据类型。使用StreamMessage 来实现。下面的原始类型赋给股票报价消息的每一 项。 z 股票订价名称——String z 当前的值——double z 订价时间——long z 最后更新——double z 股票信息——String 客户端可能只对某些消息字段感兴趣,但在使用StreamMessage 的情况下,客户端必须 按顺序读并暗地抛弃每个字段。 在下面的例子中,已经设置了下面字段的值: String stockName; /* Name of the stock */ double stockValue; /* Current value of the stock */ long stockTime; /* Time of the stock update */ double stockDiff; /* +/hange in the stock quote */ String stockInfo; /* Information on this stock*/ StreamMessage message; /* Create message */ message = session。createStreamMessage(); 下面的元素必须按照它们被读取的顺序写入StreamMessage 。注意,它们不是单独命名 的属性,和MapMessage 中一样。 /* Set data for message */ message。writeString(stockName); message。writeDouble(stockValue); message。writeLong(stockTime); message。writeDouble(stockDiff); message。writeString(stockInfo); 9。4。8 解包StreamMessage StreamMessage 的元素必须按照写入的顺序被读取。 String stockName; /* Name of the stock quote */ double stockValue; /* Current value of the stock */ long stockTime; /* Time of the stock update */ double stockDiff; /* +/hange in the stock quote */ String stockInfo; /* Information on this stock */ stockName = message。readString(); 63 / 66 …………………………………………………………Page 64…………………………………………………………… stockValue = message。readDouble(); stockTime = message。readLong(); stockDiff = message。readDouble(); stockInfo = message。readString(); 9。4。9 创建ObjectMessage 股票信息可以以特殊的 StockObject 对象的形式被发送。这个对象然后作为 ObjectMessage 的消息体被发送。ObjectMessage 可以用于发送java 对象。 使用那些StockObject 实现中互不重复的方法来设置这些值。例如,StockObject 可以有 设置不同数据值的方法。使用StockObject 的应用可能看起来类似如下代码: String stockName; /* Name of the stock quote */ double stockValue; /* Current value of the stock */ long stockTime; /* Time of the stock update */ double stockDiff; /* +/hange in the stock quote */ String stockInfo; /* Information on this stock */ /* Create a StockObject */ StockObject stockObject = new StockObject(); /* Establish the values for the StockObject */ stockObject。setName(stockName); stockObject。setValue(stockValue); stockObject。setTime(stockTime); stockObject。setDiff(stockDiff); stockObject。setInfo(stockInfo); 为了创建ObjectMessage,将StockObject 传入消息,你将按下面的方式来实现: /* Create an ObjectMessage */ ObjectMessage message; message = session。createObjectMessage(); /* Set the body of the message to the StockObject */ message。setObject(stockObject); 9。4。10 解包ObjectMessage 为了解包ObjectMessage,使用ObjectMessage。getObject 方法来得到对象。一旦对象被 取出,客户端应用使用与对象类型匹配的方法从对象中取出数据。 StockObject stockObject; /* Retrieve the StockObject from the message */ stockObject = (StockObject)message。getObject(); /* Extract data from the StockObject by using StockObject methods */ String stockName; /* Name of the stock quote */ double stockValue; /* Current value of the stock */ long stockTime; /* Time of the stock update */ double stockDiff; /* +/hange in the stock quote */ 64 / 66 …………………………………………………………Page 65…………………………………………………………… String stockInfo; /* Information on this stock */ stockName = stockObject。getName(); stockValue = stockObject。getValue(); stockTime = stockObject。getTime(); stockDiff = stockObject。getDiff(); stockInfo = stockObject。getInfo(); 10 问题 10。1 已解决的问题 10。1。1 JDK1。1。x 兼容性 JMS 兼容JDK1。1。x 。 10。1。2 分布式Java 事件模型 一般情况下,JMS 可以被用作通知服务;但是它没有定义java 事件的分布版本。 实现分布式Java 事件的一个替代方案是将事件作为JavaBean 通过JMS 透明地分发它, 这种分发对事件的生产者和监听器bean 是透明的。 10。1。3 可以合并JMS 的两个域PTP 和Pub/Sub 吗? 尽管有许多类似点,提供分离的域似乎仍然是重要的。 这意味着供应商不必支持超出域的那些工具,且产品完全支持一个域会使客户端代码有 更好的移植性(相对于对合并域的子集做比较少的支持)。 10。1。4 JMS 应当指定一个JMS JavaBean 集合吗? JMS 是一个底层API ,和其它的Java 底层API 一样,它不直接作为JavaBean 出现。 10。1。5 与CORBA 通知服务对齐 通知服务增加了过滤、转发保证语义、永久连接和对 CORBA 事件服务的事件网络的组 装。它从CORBA 消息服务得到它的转发保证语义(这个服务定义了异步CORBA 方