Getting started with ActiveMQ isn’t difficult. You simply need to start up the broker and make sure that it’s capable of accepting connections and sending messages. ActiveMQ comes with some simple examples that will help you with this task, but first we need to install Java and download ActiveMQ.
In this section, you’ll download and install ActiveMQ, examine the ActiveMQ directory, and start up ActiveMQ for the first time. (Assume Java SE is ready)
Downloading ActiveMQ:
ActiveMQ is available from the Apache ActiveMQ website at the following URL: http://activemq.apache.org/download.html. Click on the link to the 5.4.1 release and you’ll find both tarball and zip formats available (the tarball is for Linux and Unix; the zip is for Windows). Once you’ve downloaded one of the archives, expand it and you’re ready to move along. Once you get to this point, you should have the Java SE all set up and working correctly, and you’re ready to take a peek at the ActiveMQ directory.
Examining the ActiveMQ directory:
From the command line, move into the apache-activemq-5.x.x directory and enter the command shown here.
- Listing 1.2 List the contents of the ActiveMQ directory
The contents of the directory are fairly straightforward:
- LICENSE
- NOTICE
- README.txt
- WebConsole-README.txt
- activemq-all-5.4.1.jar
- bin
- conf
- data
- docs
- example
- lib
- user-guide.html
- webapps
The next task is to start up ActiveMQ and verify it using the examples.
Starting up ActiveMQ:
After downloading and expanding the archive, ActiveMQ is ready for use. The binary distribution provides a basic configuration to get you started easily and that’s what we’ll use in the examples. So start up ActiveMQ now as shown next.
- Listing 1.3 Start up ActiveMQ
This command starts up the ActiveMQ broker and some of its connectors to expose it to clients via a few protocols, namely, TCP, SSL, STOMP, and XMPP. Just be aware that ActiveMQ has started and is available to clients via TCP on port 61616. This is all configurable and will be discussed later in chapter 4. For now, the preceding output tells you that ActiveMQ is up and running and ready for use. Now it’s ready to begin handling some messages. The best way to begin sending and receiving messages is by using some of the examples that come with ActiveMQ. The next section walks you through this in a step-by-step manner.
Running your first examples with ActiveMQ:
The previous section walked you through starting up ActiveMQ in one terminal. For verification of this, you should open two more terminals to run the ActiveMQ examples. In the second terminal, move into the example directory and look at its contents as shown in the following listing.
- Listing 1.4 List the contents of the ActiveMQ example directory
The example directory contains a few different items. Here’s a quick description of each item in that directory:
- build.xml
- conf
- perfharness
- ruby
- src
- transactions
Using the second terminal, start up a JMS consumer as shown here.
- Listing 1.5 Start up the ActiveMQ consumer example
The command compiles the Java examples and starts up a simple JMS consumer. As you can see from the output, this consumer is
Basically, the JMS consumer is connected to ActiveMQ and waiting for messages. Now you can send some messages to the TEST.FOO destination.
In the third terminal, move into the example directory and start up a JMS producer as shown below. This will immediately begin to send messages.
- Listing 1.6 Start up the ActiveMQ producer example
Although the output has been truncated for readability, the command starts up a simple JMS producer and you can see from the output that it
Once the JMS producer is connected, it then sends 2,000 messages and shuts down. This is the number of messages the consumer is waiting to consume before it shuts down. So as the messages are being sent by the producer in terminal three, flip back to terminal two and watch the JMS consumer as it consumes those messages. Here’s the output you’ll see in terminal two:
At this time, both the consumer and the producer should be shut down, but the ActiveMQ broker is still running in the first terminal. Take a look at the first terminal again and you’ll see that ActiveMQ appears to not have budged at all. This is because the default logging configuration doesn’t output anything beyond what’s absolutely necessary. If you’d like to tweak the logging configuration to output more information as messages are sent and received, you can do so. Logging will be covered further in chapter 14.
Through the use of the Java examples that come with ActiveMQ, it has been proven that the broker is up and running and can mediate messages. This doesn’t seem like much but it’s an important first step. If you were able to successfully run the Java examples, then you know that you have no networking problems on the machine you’re using and you know that ActiveMQ is behaving properly. If you were unable to successfully run the Java examples, then you’ll need to troubleshoot the situation. If you need some help, heading over to the ActiveMQ mailing lists is the best way to find help. These examples are just to get you started but can be used to test many scenarios. Throughout the rest of the book, some different examples surrounding a couple of common use cases will be used to demonstrate ActiveMQ and its features. These examples are explained further in chapter 3.
沒有留言:
張貼留言