3.3 用例2: job queue 示例-Use case two: the job queue example
The second use case focuses on job queues to illustrate point-to-point messaging. This
example uses a Producer class to send job messages to a job queue and a Consumer
class for registering a Listener class to consume messages from queues in an asynchronous
manner. These three classes provide the functionality necessary to show
how JMS point-to-point messaging should work. The classes in this example are
extremely similar to those used in the stock portfolio example. The difference
between the two examples is the JMS messaging domain that each uses.
The Producer class in this example sends messages to the JOBS.suspend and
JOBS.delete queues and the Consumer class consumes.
Figure 3.3 contains a high-level diagram of the job queue example’s functionality.
The Producer class uses a single JMS MessageProducer to send 1,000 job messages
in blocks of 10 randomly across the two queues. After sending 1,000 messages total,
it’ll shut down. The Consumer class uses one JMS MessageConsumer per queue and registers
a JMS MessageListener on each queue to utilize the message and output its contents.
第二个关于job queue的例子旨在说明点对点消息系统.这个例子使用Producer类发送job消息到
消息队列,同时使用Consumer类来注册Listener类以便以异步方式处理消息队列中的消息.
这三个类提供的功能展示了JMS点对点消息是如何发送和接收的.这个例子中的三个类和stock portfolio
例子中相应的类非常相似,两个例子的不同点在于各自使用了不同的消息系统.本例中Producer类发送
消息到消息队列JOBS.suspend和JOBS.delete,Consumer类从消息队列接收和处理消息.
图3.3概述了job queue例子的功能.Producer类使用单一的JMS MessageProducer发送1000个job消息,
这些消息以10个为一组随机的发送到两个消息队列之一.在发送1000个消息之后,Producer类自动关闭.
Consumer使用单一的JMS MessageConsumer为每一个消息队列注册JMS MessageListener,
JMS MessageListener在处理消息之后输出消息内容.
3.3.1 Running the job queue example
3.3.1 运行job queue实例
The steps for executing the job queues example are nearly identical to the previous
example:
1 Start up ActiveMQ
2 Run the Producer class
3 Run the Consumer class
Again, these steps are simple, but there’s one exception to note. When using PTP messaging,
queues will hold messages until they’re consumed or the messages expire. So
the Producer can be started before the Consumer and the Consumer won’t miss any
messages.
Just as in the stock portfolio example, the first task is to start up ActiveMQ. You’ll
be spared the output from this task, as it’s the same as shown in section1.6 and none
of the default configuration has been changed.
Next, open a second terminal or command line to execute the Producer as shown
here.
运行job queue这个例子的步骤和运行前面一个例子的步骤几乎相同:
1 启动ActiveMQ
2 运行Consumer类
3 运行Publisher类
同样,这些步骤很简单,但是有一点需要注意.当使用点对点消息时,消息队列会保存消息知道消息被处理
或者过期.所以,Producer类可以在Consumer类之前运行,并且Consumer类不会错过任何消息.
如同在stock portfolio实例中那样,第一步是启动ActiveMQ.这里省略了启动ActiveMQ时控制台的输出,
因为这里的输出和清单1.6中一样,ActiveMQ使用的默认配置信息没有修改.
下一步,打开第二个终端或命令行窗口,输入下面的命令运行Producer类.
Listing 3.5 Running the job queue publisher
[amq-in-action-example-src] $ mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch3.jobs.Publisher
[INFO] Scanning for projects…
[INFO] Searching repository for plugin with prefix: ‘exec’.
[INFO] ——————————————————————-
—–
[INFO] Building ActiveMQ in Action Examples
[INFO] task-segment: [exec:java]
[INFO] ——————————————————————-
—–
[INFO] Preparing exec:java
[INFO] No goals needed for project – skipping
[WARNING] POM for ‘woodstox:wstx-asl:pom:3.2.7:compile’ is invalid.
Its dependencies (if any) will NOT be available to the current build.
[INFO] [exec:java {execution: default-cli}]
Sending: id: 1000000 on queue: queue://JOBS.delete
Sending: id: 1000001 on queue: queue://JOBS.delete
Sending: id: 1000002 on queue: queue://JOBS.delete
Sending: id: 1000003 on queue: queue://JOBS.delete
Sending: id: 1000004 on queue: queue://JOBS.delete
Sending: id: 1000005 on queue: queue://JOBS.delete
Sending: id: 1000006 on queue: queue://JOBS.delete
Sending: id: 1000007 on queue: queue://JOBS.delete
Sending: id: 1000008 on queue: queue://JOBS.delete
Sending: id: 1000009 on queue: queue://JOBS.delete
Published ’10’ of ’10’ job messages
Sending: id: 1000010 on queue: queue://JOBS.delete
Sending: id: 1000011 on queue: queue://JOBS.suspend
…
Published ’10’ of ’30’ job messages
…
Note that no arguments are necessary to execute the Producer in listing 3.5. The
Publisher class contains two queues to which it publishes named delete and suspend;
hence, the use of those words in the output. The Producer will continue until it sends
a total of 1,000 messages to the two queues and then it’ll shut down.
The third task is to open another terminal or command line and execute the Consumer
to consume the messages from the two queues. This command is shown next.
注意,清单3.5中运行Producer类不需要任何参数.Producer发送消息到两个名称分别为delete和suspend的消息队列.
因此,在输出中会用到两个消息对列的名称.Producer发送1000个消息到两个消息队列后自动关闭.
第三步是打开第三个终端或命令行控制台并执行Consumer类以接受和发送消息.执行Consumer类的命令如下:
Listing 3.6 Running the job queue consumer
[amq-in-action-example-src] $ mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch3.jobs.Consumer
[INFO] Scanning for projects…
[INFO] Searching repository for plugin with prefix: ‘exec’.
[INFO] ——————————————————————-
—–
[INFO] Building ActiveMQ in Action Examples
[INFO] task-segment: [exec:java]
[INFO] ——————————————————————-
—–
[INFO] Preparing exec:java
[INFO] No goals needed for project – skipping
[WARNING] POM for ‘woodstox:wstx-asl:pom:3.2.7:compile’ is invalid.
Its dependencies (if any) will NOT be available to the current build.
[INFO] [exec:java {execution: default-cli}]
suspend id:1000003
suspend id:1000010
suspend id:1000012
suspend id:1000013
suspend id:1000015
suspend id:1000022
suspend id:1000025
suspend id:1000027
delete id:1000000
delete id:1000001
delete id:1000002
delete id:1000004
delete id:1000005
…
The Consumer will run fast at first, consuming all the messages already on the queues.
When it catches up to where the Producer is in sending the 1,000 messages, the
Consumer slows down and keeps up with the Publisher until it completes. When all
the messages have been sent and the Producer shuts itself down, you’ll need to press
CTRL-C in the third terminal where the Consumer is running to shut it down.
This concludes the job queue example. Now you’ve seen how well ActiveMQ works
in a point-to-point messaging scenario.
Consumer类在开始的时候快速执行,处理消息队列中的的消息.当Producer发送的1000个消息处理完后,
Consumer慢下来,等待Publisher类发送消息直到Publisher完成发送1000个消息并自动关闭.当Producer
发送完1000个消息后,Producer自动关闭后,你需要在第三个终端或命令行控制台窗口按下CTRL+C键以便
关闭Consumer类.这就是job queue的例子.现在你已经知道点对点消息是如何在ActiveMQ中工作的.
赞 赏微信赞赏 支付宝赞赏
本文固定链接: https://www.jack-yin.com/coding/translation/activemq-in-action/510.html | 边城网事