ActiveMQ provides all the features from the JMS specification and adds many more powerful features on top of that. This is depicted in figure 3.1 and these features will be discussed through the rest of the book. In order to best demonstrate these features, two new examples have been created that are modeled after real business domains. Compared to the example that’s part of the ActiveMQ distribution, these examples lend themselves to demonstrating the features in ActiveMQ in a more complete and easy manner.
Figure 3.1 ActiveMQ implements all the features from the JMS specification, as well as many additional features.
One of the examples is based on a stock portfolio and the other is based on a job queue. These two examples are more extensive than the examples that come with ActiveMQ. The use case for each of these examples is introduced briefly, followed by a deeper discussion of how to use them. You can refer back to this chapter at any time throughout the book if you need a refresher on the examples.
For the source code used in this chapter, you can download from here.
Downloading Maven and compiling the examples:
Here are the steps to download and install Maven:
1. Download Maven from the Apache Software Foundation: http://maven.apache.org/
2. Expand the downloaded archive to a permanent location on your computer.
3. Create an environment variable named M2_HOME that points to the Maven directory.
4. On Unix, add the $M2_HOME/bin directory to the PATH environment variable
5. Verify the Maven installation by running the following command from the command line:
If you’ve successfully installed Maven, the examples need to be unzipped and compiled. After expanding the zip file containing the example source code, you’ll be ready to compile the examples. To do so, move into the amq-in-action-example-src directory and run the command shown next.
As long as you see the BUILD SUCCESSFUL message, you’re ready to move on to the next section. If, on the other hand, you see the BUILD FAILURE message, you’ll need to troubleshoot and correct the situation before proceeding.
Use case one: the stock portfolio example:
As mentioned earlier in the chapter, the first use case revolves around a stock portfolio use case for demonstrating publish/subscribe messaging. This example is simple and utilizes a Publisher class for sending stock price messages to a topic, as well as a Consumer class for registering a Listener class to consume messages from topics in an asynchronous manner. These three classes embody the functionality of generating ever-changing stock prices that are published to topics on which the consumer is subscribed.
In this example, stock prices are published to an arbitrary number of topics. The number of topics is based on the number of arguments sent to the Publisher and the Consumer on the command line. Each class will dynamically send and receive to/from the topics (an example is provided next). Take a look at figures 3.2 and 3.3 to see at a high level what the examples seek to achieve.
For the sake of this demonstration, two topics will be used. The Publisher class uses a single JMS MessageProducer to send 1,000 fictitious stock price messages in blocks of 10, randomly distributed across the topics named in the command-line argument. After it sends 1,000 messages, it shuts down. The Consumer class creates one JMS MessageConsumer per topic and registers a JMS MessageListener for each topic. Because this example demonstrates publish/subscribe, theConsumers must be online to consume messages being sent by the Publisher, because durable consumers aren’t used in the basic stock portfolio example. The next step is to actually run the example so that you can see them in action.
Running the stock portfolio example
There are three basic steps to running this example:
These steps appear to be simple, and they are. The only item of note is that the Consumer should be started before the Publisher, in order to receive all messages that are published. This is because this example demonstrates pub/sub messaging and topics won’t hold messages unless the consumer makes a durable subscription, and we’re not using durable subscriptions here. So let’s get started with the stock portfolio example.
The first task is to open a terminal or command line and execute ActiveMQ. This only requires a single command as demonstrated in the following listing.
- Listing 3.2 Start up ActiveMQ
The next task is to open a second terminal or command line to execute the Consumer class. The Consumer is executed using the maven-exec-plugin (http://mng.bz/bf7g) by passing it some system properties as arguments using the exec.args property. An example of running the Consumer is shown next.
- Listing 3.3 Run the stock portfolio consumer
You can see in listing 3.3 that Maven downloads the necessary artifacts it needs to run the examples. Once this has completed, the Publisher can start up and begin publishing stock prices to the two topics named on the command line, CSCO and ORCL. These two topic names were picked at random and can be replaced with any Strings you desire. The important part is that the same arguments be used for both the Consumer and the Publisher (the Publisher is shown next) via the system property exec.args.
Note that the output just seems to stop as the Consumer hangs there. This behavior is correct because it’s waiting for messages to arrive in the topics to be consumed. When the Publisher begins sending messages, the Consumer will begin to consume them.
The next task is to open a third terminal or command line to execute the Publisher class. Note that the same arguments are used in exec.args that were used for executing the Consumer class earlier, because the maven-exec-plugin is used to execute the Publisher class as well. An example of running Publisher is shown here.
- Listing 3.4 Running the stock portfolio publisher
When executing the Publisher class, Maven already has all the necessary dependencies from the earlier execution of the Consumer class, so nothing should be downloaded. The lower portion of the output shows the stock price messages being sent to the two topics in blocks of 10. The example output is truncated for space, so just know that the Publisher will run until it sends a total of 1,000 messages.
After running the Publisher, if you switch back to the second terminal where the Consumer was started, you should see that it’s now consuming messages:
The preceding output comes from the Listener class that’s registered by the Consumer on the two topics named ORCL and CSCO. This output shows the consumption of the stock price messages from the same two topics to which the Publisher is sending messages. Once the Publisher reaches 1,000 messages sent, it’ll shut down. But the Consumer will continue to run and just hang there waiting for more messages to arrive on those two topics. You can press CTRL-C in the second terminal to shut down the Consumer at this point.
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 aConsumer 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.
Running the job queue example
The steps for executing the job queues example are nearly identical to the previous example:
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 before and none of the default configuration has been changed. Next, open a second terminal or command line to execute the Producer as shown here.
- Listing 3.5 Running the job queue publisher
Note that no arguments are necessary to execute the Producer in listing 3.5. The Producer 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.
- Listing 3.6 Running the job queue consumer
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 Producer 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.
- [ 英文學習 ] (7)
- [ 計算機概論 ] (1)
- [ 深入雲計算 ] (8)
- [ 雜七雜八 ] (5)
- [ Algorithm in Java ] (26)
- [ Data Structures with Java ] (82)
- [ IR Class ] (19)
- [ Java 文章收集 ] (21)
- [ Java 代碼範本 ] (42)
- [ Java 套件 ] (11)
- [ JVM 應用 ] (7)
- [ LFD Note ] (2)
- [ MangoDB ] (7)
- [ Math CC ] (3)
- [ MongoDB ] (5)
- [ MySQL 小學堂 ] (1)
- [ Python 考題 ] (2)
- [ Python 常見問題 ] (6)
- [ Python 範例代碼 ] (7)
- [心得扎記] (1)
- [網路教學] (3)
- [C 常見考題] (2)
- [C 範例代碼] (4)
- [C/C++ 範例代碼] (18)
- [Intro Alg] (4)
- [Java 代碼範本] (25)
- [Java 套件] (15)
- [Linux 命令] (60)
- [Linux 小技巧] (33)
- [Linux 小學堂] (31)
- [ML In Action] (14)
- [ML] (32)
- [MLP] (7)
- [Python 學習筆記] (1)
- [Quick Python] (20)
- [Software Engineering] (8)
- [The python tutorial] (7)
- 工具收集 (21)
- 設計模式 (18)
- 資料結構 (68)
- ActiveMQ In Action (13)
- AI (6)
- Algorithm (4)
- Android (11)
- Big Data 研究 (15)
- C/C++ (68)
- C++ (19)
- CCDH (20)
- Coursera (2)
- Database (1)
- Design Pattern (1)
- Device Driver Programming (42)
- Docker (31)
- Docker 工具 (1)
- Docker Practice (6)
- Eclipse (1)
- English Writing (52)
- ExtJS 3.x (4)
- FP (1)
- FreeBSD (1)
- GCC (2)
- Git (4)
- Git Pro (4)
- GNU (30)
- Groovy (80)
- Hadoop (65)
- Hadoop. Hadoop Ecosystem (1)
- Java (256)
- Java Framework (1)
- Java UI (3)
- JavaIDE (2)
- JFreeChart (2)
- Kali/Metasploit (6)
- KVM (1)
- Learn Spark (10)
- Linux (230)
- Lucene (19)
- Math (8)
- MPI (3)
- Nachos (4)
- Network (3)
- NLP (1)
- OO (28)
- OpenCL (1)
- OpenMP (3)
- OSC (1)
- OSGi (10)
- Perl (24)
- Python (168)
- Python Std Library (35)
- Python tools (4)
- QEMU (1)
- R (1)
- RIA (14)
- RTC (5)
- Ruby (68)
- Ruby Packages (8)
- Scala (75)
- ScalaIA (15)
- Tools (11)
- UML (2)
- Unix (18)
- Verilog (3)
- Vmware (2)
- Windows 技巧 (10)
- ► 2016 (273)
- [ Big Data 研究 ] 04 管理 Linux Container 虛擬網路 - Part2...
- [ DM Practical MLT] (4) Algorithms - Inferring rud...
- [ Big Data 研究 ] 04 管理 Linux Container 虛擬網路 - Part1...
- [ 常見問題 ] Limit disk size and bandwidth of a Docker...
- [ VMware 常見問題 ] Command Line Application for Autom...
- [ Python 文章收集 ] Dive Into Python - Dynamically imp...
- [ Big Data 研究 ] 03 管理 Linux Container 虛擬主機
- [ Big Data 研究 ] 02 安裝與認識 Linux Container
- [ InAction Note ] Ch4. Connecting ActiveMQ - Conne...
- [ Python 常見問題 ] How to check if a python module ex...
- [ Python 常見問題 ] How to list imported modules?
- [ In Action ] Working with XML - Reading XML docum...
- [Linux 常見問題] How to get pid given the process name...
- [ ML Foundation ] Section2 : Learning to Answer Ye...
- [ InAction Note ] Ch4. Connecting ActiveMQ - Conne...
- [ ML Foundation ] Section2 : Learning to Answer Ye...
- [ GroovyGN ] Working with Files
- [ InAction Note ] Ch4. Connecting ActiveMQ - Trans...
- [ Java 代碼範本 ] JFreeChart : XY Charts - XYLineAndSh...
- [文章收集] 網際網路上用來找尋設備的方法
- [ InAction Note ] Ch3. The stock portfolio example...
- [ Java 套件 ] Selenium - Introduction
- ▼ 八月 (22)
- ► 2014 (245)
- ► 2013 (112)
- ► 2012 (197)
- ► 2011 (265)