演習 - Azure Service Bus にメッセージを送信する

完了

このユニットでは、Azure Service Bus キューにメッセージを送信する Spring Boot アプリケーションを作成します。 以下の手順をローカルで実行します。

Spring Boot プロジェクトを作成する

Spring Boot プロジェクトを作成するには、次のようにコマンド ラインで Spring Initializr を使用します。

curl https://start.spring.io/starter.tgz -d type=maven-project -d dependencies=web -d baseDir=spring-sender-application -d bootVersion=3.3.0.RELEASE -d javaVersion=1.8 | tar -xzvf -

Service Bus キューにメッセージを送信する

では、いくつかのメッセージを Service Bus キューに送信してみましょう。

Service Bus の Spring Boot スターターのために Maven 依存関係を追加する

spring-sender-applicationpom.xml ファイルで、dependencies の下に次のコマンドを追加します。

		<!-- https://mvnrepository.com/artifact/com.azure.spring/spring-cloud-azure-starter-servicebus-jms -->
		<dependency>
		    <groupId>com.azure.spring</groupId>
		    <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId>
		    <version>5.18.0</version>
		</dependency>

構成パラメーターを追加する

  1. spring-sender-application\src\main\resources フォルダーの application.properties ファイルを編集し、次のパラメーターを追加します。

    spring.jms.servicebus.connection-string=<xxxxx>
    spring.jms.servicebus.idle-timeout=20000
    spring.jms.servicebus.pricing-tier=premium
    
  2. spring.jms.servicebus.connection-string プロパティを、前に保存した Service Bus 名前空間への接続文字列に設定します。

Service Bus にメッセージを送信するコードを追加する

次に、メッセージを Service Bus キューに送信するためのビジネス ロジックを追加します。

ディレクトリ src/main/java/com/example/demo 内に、次の内容を含む SendController.java ファイルを作成します。

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendController {
    
    private static final String queue = "test-queue-jms";

    @Autowired
    private JmsTemplate jmsTemplate;

    @GetMapping("/messages")
    public String postMessage(@RequestParam String message) {
        jmsTemplate.send(queue, s -> s.createTextMessage(message));
        return message;
    }
}

ローカルでアプリケーションを実行する

  1. pom.xml ファイルがあるサンプルの spring-sender-application フォルダーのルートに戻り、次のコマンドを実行して Spring Boot アプリケーションを起動します。 この手順では、mvn が Windows コンピューターにインストールされていて、PATH に含まれるものとします。

    mvn spring-boot:run
    
  2. アプリケーションの起動が完了した後、次のリンクを選択して Service Bus キューにメッセージを送信できます。

    http://localhost:8080/messages?message=Hello
    
    http://localhost:8080/messages?message=HelloAgain
    
    http://localhost:8080/messages?message=HelloOnceAgain
    

    メッセージ クエリ パラメーターの文字列値を変更し、任意のテキストを Service Bus キューに送信することができます。

    ブラウザーにはメッセージ クエリ文字列パラメーターとして渡したものが表示されます。これは、Service Bus がメッセージを受け取っていることを意味します。

Service Bus キューのメッセージを表示する

Note

メッセージを表示するとメッセージの送信側を理解するのに役立ちますが、このステップは省略可能です。

これらのメッセージは、このチュートリアルの次のステップで受信します。

続行して Azure portal の Service Bus Explorer でメッセージを表示できます。

  1. Azure portal に戻り、左側のメニューの [エンティティ][キュー] を選びます。

  2. 適切なキューを選びます。 たとえば、このデモのキューは test-queue-jms です。

  3. 左側のペインで、[Service Bus エクスプローラー] を選択します。

  4. [Peek from start] (最初からクイック表示) を選びます。 HTTP コマンドを使って送信した 3 つのメッセージがすべて表示されるはずです。

    Screenshot of the Service Bus explorer peek experience.

  5. メッセージを選ぶと、下のウィンドウにメッセージの本文が表示されます。

    Screenshot of the Service Bus explorer with peeked messages.