程式扎記: [ InAction Note ] Ch1. Introduction - Getting started with ActiveMQ

標籤

2014年6月16日 星期一

[ InAction Note ] Ch1. Introduction - Getting started with ActiveMQ

Preface: 
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 
A file required by the Apache Software Foundation (ASF) for legal purposes; contains the licenses of all libraries used by ActiveMQ.

- NOTICE 
Another ASF-required file for legal purposes; it contains copyright information of all libraries used by ActiveMQ.

- README.txt 
A file containing some URLs to documentation to get new users started with ActiveMQ.

- WebConsole-README.txt 
Contains information about using the ActiveMQ web console.

- activemq-all-5.4.1.jar 
A jar file that contains all of ActiveMQ; it’s placed here for convenience if you need to grab it and use it.

- bin 
The bin directory contains binary/executable files for ActiveMQ; the startup scripts live in this directory.

- conf 
The conf directory holds all the configuration information for ActiveMQ.

- data 
The data directory is where the log files and message persistence data is stored.

- docs 
Contains a simple index.html file referring to the ActiveMQ website.

- example 
The ActiveMQ examples; these are what we’ll use shortly to test out ActiveMQ quickly.

- lib 
The lib directory holds all the libraries needed by ActiveMQ.

- user-guide.html 
A brief guide to starting up ActiveMQ and running the examples.

- webapps 
The webapps directory holds the ActiveMQ web console and some other web-related demos.

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 
$ ./bin/activemq console
INFO: Using default configuration
(you can configure options in one of these file: /etc/default/activemq /home/nlg/.activemqrc)

INFO: Invoke the following command to create a configuration file
./bin/activemq setup [ /etc/default/activemq | /home/nlg/.activemqrc ]
...
Loading message broker from: xbean:activemq.xml
INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@d09ad3: startup date [Wed Jun 26 14:36:58 CST 2013]; root of context hierarchy
INFO | PListStore:[/root/ActiveMQ/apache-activemq-5.8.0/data/localhost/tmp_storage] started
INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[/root/ActiveMQ/apache-activemq-5.8.0/data/kahadb]
INFO | Apache ActiveMQ 5.8.0 (localhost, ID:nlg-System-Product-Name-56894-1372228619131-0:1) is starting
INFO | Listening for connections at: tcp://nlg-System-Product-Name:61616?maximumConnect...eformat.maxFrameSize=104857600
INFO | Connector openwire Started
INFO | Listening for connections at: amqp://nlg-System-Product-Name:5672?maximumConnect...eformat.maxFrameSize=104857600
INFO | Connector amqp Started
INFO | Apache ActiveMQ 5.8.0 (localhost, ID:nlg-System-Product-Name-56894-1372228619131-0:1) started
INFO | For help or more information please see: http://activemq.apache.org
INFO | Web console type: embedded
INFO | ActiveMQ WebConsole initialized.
INFO | Initializing Spring FrameworkServlet 'dispatcher'
INFO | jolokia-agent: No access restrictor found at classpath:/jolokia-access.xml, access to all MBeans is allowed

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 
An Ant build configuration for use with the Java examples.

- conf 
The conf directory holds configuration information for use with the Java examples.

- perfharness 
The perfharness directory contains a script for running the IBM JMS performance harness against ActiveMQ.

- ruby 
The ruby directory contains some examples of using ActiveMQ with Ruby and the STOMP connector.

- src 
The src directory is where the Java examples live; this directory is used by the build.xml.

- transactions 
The transactions directory holds an ActiveMQ implementation of the TransactedExample from Sun’s JMS Tutorial.

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 
* Connecting to the broker using the TCP protocol (tcp://localhost:61616)
* Watching a queue named TEST.FOO
* Using nondurable subscription
* Waiting to receive 2000 messages before shutting down

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 
[example]$ ant producer
Buildfile: build.xml
init:
compile:
producer:
[echo] Running producer against server at $url =
tcp://localhost:61616 for subject $subject = TEST.FOO
[java] Connecting to URL: tcp://localhost:61616
[java] Publishing a Message with size 1000 to queue: TEST.FOO
[java] Using non-persistent messages
[java] Sleeping between publish 0 ms
[java] Running 1 parallel threads
[java] [Thread-2] Sending message: 'Message: 0 sent at: Thu Oct 14
21:24:07 MDT 2010 ...'
[java] [Thread-2] Sending message: 'Message: 1 sent at: Thu Oct 14
21:24:07 MDT 2010 ...'
[java] [Thread-2] Sending message: 'Message: 2 sent at: Thu Oct 14
21:24:07 MDT 2010 ...'

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 
* Connects to the broker using the TCP connector (tcp://localhost:61616)
* Publishes messages to a queue named TEST.FOO
* Uses nonpersistent messages
* Doesn’t sleep between publishing messages

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: 
...
[java] [Thread-1] Received: 'Message: 1998 sent at: Wed Jun 26 14:53:08 CST 201...' (length 1000)
[java] [Thread-1] Received: 'Message: 1999 sent at: Wed Jun 26 14:53:08 CST 201...' (length 1000)
[java] [Thread-1] Closing connection
[java] All threads completed their work

BUILD SUCCESSFUL
Total time: 5 minutes 16 seconds

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.

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!