此内容没有您所选择的语言版本。
Chapter 3. Getting Started
This chapter guides you through a simple exercise to help you get started using AMQ JMS. Before starting, make sure you have completed the steps in the Chapter 2, Installation chapter for your environment.
To build the examples, you must first configure Maven to discover the client repository.
3.1. Preparing the Broker 复制链接链接已复制到粘贴板!
The example programs require a running broker with a queue named queue
. Follow these steps to define the queue and start the broker.
- Install the broker.
- Create a broker instance. Enable anonymous access.
Start the broker instance and check the console for any critical errors logged during startup.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
artemis queue
command to create a queue calledqueue
.BROKER_INSTANCE_DIR/bin/artemis queue create --name queue --auto-create-address --anycast
$ BROKER_INSTANCE_DIR/bin/artemis queue create --name queue --auto-create-address --anycast
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You are prompted to answer a series of questions. For yes|no questions, type
N
; otherwise, press Enter to accept the default value.
3.2. Running Hello World 复制链接链接已复制到粘贴板!
Use Maven to build the examples by running the following command in the INSTALL_DIR/examples
directory.
In this example, the addition of dependency:copy-dependencies
results in the dependencies being copied into the target/dependency
directory.
mvn clean package dependency:copy-dependencies -DincludeScope=runtime -DskipTests
mvn clean package dependency:copy-dependencies -DincludeScope=runtime -DskipTests
Run the HelloWorld
example by using one of the following commands.
Linux: java -cp "target/classes/:target/dependency/*" org.apache.qpid.jms.example.HelloWorld Windows: java -cp "target\classes\;target\dependency\*" org.apache.qpid.jms.example.HelloWorld
Linux: java -cp "target/classes/:target/dependency/*" org.apache.qpid.jms.example.HelloWorld
Windows: java -cp "target\classes\;target\dependency\*" org.apache.qpid.jms.example.HelloWorld
The HelloWorld
example creates a connection to the broker, creates a MessageConsumer
and MessageProducer
for the queue called queue
, sends a Hello world! TextMessage
, receives it, and prints its contents to the terminal.
For example, running it on Linux results in the following output.
java -cp "target/classes/:target/dependency/*" org.apache.qpid.jms.example.HelloWorld
$ java -cp "target/classes/:target/dependency/*" org.apache.qpid.jms.example.HelloWorld
Hello world!
The source code for the example can be found in the INSTALL_DIR/src/main/java
directory, with the JNDI and logging configuration found in the INSTALL_DIR/src/main/resources
directory.