このコンテンツは選択した言語では利用できません。

Chapter 3. Getting started


This chapter guides you through a simple exercise to help you get started using AMQ Spring Boot Starter.

3.1. Prerequisites

To build the example, Maven must be configured to use the Red Hat repository.

3.2. Preparing the broker

The example programs require a running broker with a queue named example. Follow these steps to define the queue and start the broker:

Procedure

  1. Install the broker.
  2. Create a broker instance. Enable anonymous access.
  3. Start the broker instance and check the console for any critical errors logged during startup.

    $ <broker-instance-dir>/bin/artemis run
    ...
    14:43:20,158 INFO  [org.apache.activemq.artemis.integration.bootstrap] AMQ101000: Starting ActiveMQ Artemis Server
    ...
    15:01:39,686 INFO  [org.apache.activemq.artemis.core.server] AMQ221020: Started Acceptor at 0.0.0.0:5672 for protocols [AMQP]
    ...
    15:01:39,691 INFO  [org.apache.activemq.artemis.core.server] AMQ221007: Server is now live
    Copy to Clipboard
  4. Use the artemis queue command to create a queue called example.

    <broker-instance-dir>/bin/artemis queue create --name example --auto-create-address --anycast
    Copy to Clipboard

    You are prompted to answer a series of questions. For yes or no questions, type N. Otherwise, press Enter to accept the default value.

3.3. Hello World

This example sends a greeting to a queue called example and then receives it back.

Example: Sending and receiving "Hello World!" - HelloWorld.java

package net.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;

@EnableJms
@SpringBootApplication
public class HelloWorld implements CommandLineRunner {
    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        SpringApplication.run(HelloWorld.class, args);
    }

    @Override
    public void run(String... strings) throws Exception {
        sendMessage("Hello World!");
    }

    public void sendMessage(String text) {
        System.out.println(String.format("Sending '%s'", text));
        this.jmsTemplate.convertAndSend("example", text);
    }

    @JmsListener(destination = "example")
    public void receiveMessage(String text) {
        System.out.println(String.format("Received '%s'", text));
    }
}
Copy to Clipboard

Running the example

To compile and run the example program, use the following procedure:

Procedure

  1. Create a new project directory. This is referred to as <project-dir> in the steps that follow.
  2. Copy the example listing to the following location:

    <project-dir>/src/main/java/net/example/HelloWorld.java
    Copy to Clipboard
  3. Use a text editor to create a new <project-dir>/pom.xml file. Add the following XML to it:

    <project>
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>net.example</groupId>
      <artifactId>example</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
      </parent>
    
      <dependencies>
        <dependency>
          <groupId>org.amqphub.spring</groupId>
          <artifactId>amqp-10-jms-spring-boot-starter</artifactId>
          <version>2.2.2.redhat-00002</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>
    Copy to Clipboard
  4. Change to the project directory and use the mvn command to compile the program.

    mvn clean package
    Copy to Clipboard
  5. Use the java command to run the program.

    java -jar target/example-1.0.0-SNAPSHOT.jar
    Copy to Clipboard

    Running the Hello World example results in the following console output:

    $ java -jar target/example-1.0.0-SNAPSHOT.jar
    
      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.0.5.RELEASE)
    
    [...]
    2018-11-05 16:21:09.849  INFO 14805 --- [main] net.example.HelloWorld: Started HelloWorld in 1.074 seconds (JVM running for 1.38)
    Sending 'Hello World!'
    Received 'Hello World!'
    Copy to Clipboard

3.4. Additional examples

More example programs are available from the AMQP 1.0 JMS Spring Boot project and the Spring project.

トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat