当前位置: 首页 > ActiveMQ in Action 读书笔记 > 正文

9.1 修改 ActiveMQ in action 的 stock portfolio示例程序

9.1 Adapting the stock portfolio example

In chapter 3, we defined a stock portfolio example that uses map messages to  exchange data between producers and consumers. For the purpose of this chapter,  we’ll modify this original example and make it a better fit for environments described  here. Instead of map messages, we’ll exchange XML data in text messages, as that’s the  more natural way of communication in some of these environments, such as dynamic  languages. So we’ll create a Java message producer that sends text messages with  appropriate XML data. Through the rest of the chapter, we’ll implement appropriate  consumers for each of the platforms described,  will show us how to connect the  specified platform with Java in an asynchronous way.

9.1 修改stock portfolio示例程序

在第3章中,我们创建了一个portfolio example示例程序.该示例程序使用了映射消息用于消息生产者  和消费者之间的数据交换.为了阐述本章内容,我们将修改远离的示例程序,使其更好的适应本章的开发  环境.我们将使用文本格式的XML消息替代映射消息(Map),因为xml格式消息是本章中开发环境(比如  动态语言)里面更常用通信载体.因此,我们将创建一个Java消息生产者用来发送适当的xml格式的文本  消息.本章的其余部分里,我们将根据不同的平台创建合适的消息消费者,它们将演示如何使用Java以  异步方式连接到指定的平台.

For starters, we have to modify our publisher to send XML data in text messages  instead of map messages. The only thing we have to change from our original publisher  is the createStockMessage() method. Listing 9.1 shows the method that creates  an appropriate XML representation of desired data and creates a TextMessage  instance out of it.
Listing 9.1 Modified stock portfolio publisher that sends messages as XML payloads

本章开始时,我们必须修改我们的消费生产者类publisher以便发送xml格式的文本消息替代  原来的映射消息(map).原始的publisher类中唯一需要修改的地方是createStockMessage()  方法.代码清单9.1中展示了创建xml格式消息表示所需的数据,并创建一个TextMessage实例.
代码清单9.1 修改后的stock portfolio例子中publisher用于发送xml格式的payload消息

protected Message createStockMessage(String stock, Session session) throws JMSException, XMLStreamException 
{
Double value = LAST_PRICES.get(stock);
if (value == null) 
{
value = new Double(Math.random() * 100);
}
 
// lets mutate the value by some percentage
double oldPrice = value.doubleValue();
value = new Double(mutatePrice(oldPrice));
LAST_PRICES.put(stock, value);
double price = value.doubleValue();
double offer = price * 1.001;
boolean up = (price > oldPrice);
StringWriter res = new StringWriter();
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(res);
writer.writeStartDocument();
writer.writeStartElement("stock");
writer.writeAttribute("name", stock);
writer.writeStartElement("price");
writer.writeCharacters(String.valueOf(price));
writer.writeEndElement();
writer.writeStartElement("offer");
writer.writeCharacters(String.valueOf(offer));
writer.writeEndElement();
writer.writeStartElement("up");
writer.writeCharacters(String.valueOf(up));
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndDocument();
TextMessage message = session.createTextMessage();
message.setText(res.toString());
return message;
}

As you can see, we’ve used a simple StAX API (http://mng.bz/0S2s) to create an XML  representation of our stock data. Next, we created a text message and used the set-  Text() method to associate this XML to the message.
Now we can start our publisher in the standard manner:

正如你看到的,我们使用了简单的StAX API (http://mng.bz/0S2s)创建了一个XML文档代表我们  的股票数据.然后,我们创建了一个文本消息,并使用setText()方法将这个XML文档内容设置到消息  中.
现在可以使用下面的命令启动publisher:

$ mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch9.Publisher -Dexec.args="IONA JAVA"
 and expect the following output: 下面是输出信息:
Sending: 
<?xml version="1.0" ?>
<stock name="JAVA">
<price>81.987225215383</price><offer>82.069212440599</offer>
<up>false</up>
</stock> 
on destination: topic://STOCKS.JAVA
 
Sending: 
<?xml version="1.0" ?><stock name="IONA">
<price>16.2205230479432</price><offer>16.236743570991</offer>
<up>false</up>
</stock>
on destination: topic://STOCKS.IONA
 
Sending: 
<?xml version="1.0" ?><stock name="JAVA">
<price>82.70353458512</price><offer>82.786238119706</offer>
<up>true</up>
</stock>
on destination: topic://STOCKS.JAVA
 
Sending:
<?xml version="1.0" ?><stock name="IONA">
<price>16.264366325962</price><offer>16.280630692288</offer>
<up>true</up>
</stock>
on destination: topic://STOCKS.IONA
 
Sending: 
<?xml version="1.0" ?><stock name="JAVA">
<price>83.341791666986</price><offer>83.425133458653</offer>
<up>true</up>
</stock>
on destination: topic://STOCKS.JAVA
 
Sending: 
<?xml version="1.0" ?><stock name="JAVA">
<price>83.891272205115</price><offer>83.975163477321</offer>
<up>true</up>
</stock>
on destination: topic://STOCKS.JAVA

As expected, the publisher sends a series of XML-formatted text messages to different  ActiveMQ topics. As they’re ready to be consumed, it’s time to see how we can consume  them using different programming languages and platforms.

正如所料,发布者publisher发送一系列XML格式的文本消息到不同的ActiveMQ的主题。  因为这些消息已经准备好被接收和处理了,下面是时候看看我们如何在不同的平台使用不同  编程语言来接收和处理这些消息.

赞 赏

   微信赞赏  支付宝赞赏


本文固定链接: https://www.jack-yin.com/coding/translation/activemq-in-action/1633.html | 边城网事

该日志由 边城网事 于2013年11月20日发表在 ActiveMQ in Action 读书笔记 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 9.1 修改 ActiveMQ in action 的 stock portfolio示例程序 | 边城网事
关键字: , , ,

9.1 修改 ActiveMQ in action 的 stock portfolio示例程序 暂无评论

发表评论

快捷键:Ctrl+Enter