このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring Boot 4.1.1 を使用してください!

AMQP

高度メッセージキューイングプロトコル(AMQP)は、メッセージ指向ミドルウェア向けのプラットフォーム非依存のワイヤレベルプロトコルです。Spring AMQP プロジェクトは、Spring の中核概念を AMQP ベースのメッセージングソリューションの開発に適用しています。Spring Boot は、3 つの異なる AMQP スタイルに対応した自動構成機能を提供します。

  • Generic AMQP 1.0 (spring-boot-starter-amqp) — Qpid ProtonJ2 クライアントライブラリに基づいた、ブローカーに依存しない AMQP 1.0 サポート。ActiveMQ、Azure Service Bus、RabbitMQ などの AMQP 1.0 互換ブローカー間での移植性が重要な場合に使用します。

  • RabbitMQ AMQP 1.0 (spring-boot-starter-amqp-rabbitmq) — ネイティブの RabbitMQ AMQP 1.0 Java client [GitHub] (英語) による RabbitMQ 固有のサポート。アプリケーションが RabbitMQ のみを対象とし、AMQP 1.0 プロトコルを使用する場合に使用します。

  • RabbitMQ AMQP 0.9 (spring-boot-starter-rabbitmq) — 従来の AMQP 0.9.1 プロトコルによる RabbitMQ のサポート。Streams サポートを含む、最も実績があり機能豊富な RabbitMQ 統合にご利用ください。

汎用 AMQP 1.0 サポート

AMQP 1.0 is supported by several brokers and messaging services beyond RabbitMQ, including ActiveMQ, Azure Service Bus, and others. Spring AMQP provides generic support for AMQP 1.0 via org.springframework.amqp:spring-amqp-client that is based on the Qpid ProtonJ2 Client Library [GitHub] (英語) .

AMQP configuration is controlled by external configuration properties in spring.amqp.*. For example, you might declare the following in your configuration:

  • プロパティ

  • YAML

spring.amqp.host=localhost
spring.amqp.port=5672
spring.amqp.username=admin
spring.amqp.password=secret
spring:
  amqp:
    host: "localhost"
    port: 5672
    username: "admin"
    password: "secret"

メッセージの送信

Spring の AmqpClient (Javadoc) は自動構成されており、次の例に示すように、独自の Bean に直接自動接続できます。

  • Java

  • Kotlin

import org.springframework.amqp.client.AmqpClient;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	private final AmqpClient amqpClient;

	public MyBean(AmqpClient amqpClient) {
		this.amqpClient = amqpClient;
	}

	// ...

	public void sendMessage(String msg) {
		this.amqpClient.to("/queues/test").body(msg).send();
	}

}
import org.springframework.amqp.client.AmqpClient
import org.springframework.stereotype.Component

@Component
class MyBean(private val amqpClient: AmqpClient) {

	// ...

	fun someOtherMethod(msg: String) {
		amqpClient.to("/queues/test").body(msg).send()
	}

}

If a MessageConverter (Javadoc) bean is defined, it is associated automatically with the auto-configured AmqpClient (Javadoc) . If no such converter is defined and Jackson is available, JacksonJsonMessageConverter (Javadoc) is used.

Client-specific settings can be configured as follows:

  • プロパティ

  • YAML

spring.amqp.client.default-to-address=/queues/default_queue
spring.amqp.client.completion-timeout=500ms
spring:
  amqp:
    client:
      default-to-address: "/queues/default_queue"
      completion-timeout: "500ms"

To further configure the auto-configured AmqpClient (Javadoc) , define a AmqpClientCustomizer (Javadoc) bean.

メッセージの受信

When the generic AMQP 1.0 infrastructure is present, any bean can be annotated with @AmqpListener (Javadoc) to create a listener endpoint. If no MethodAmqpMessageListenerContainerFactory (Javadoc) has been defined, a default one is automatically configured. If a MessageConverter (Javadoc) or a AmqpListenerErrorHandler (Javadoc) bean is defined, it is automatically associated with the default factory.

The following sample component creates a listener endpoint on the /queues/someQueue address:

  • Java

  • Kotlin

import org.springframework.amqp.client.annotation.AmqpListener;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	@AmqpListener(addresses = "/queues/someQueue")
	public void processMessage(String content) {
		// ...
	}

}
import org.springframework.amqp.client.annotation.AmqpListener
import org.springframework.stereotype.Component

@Component
class MyBean {

	@AmqpListener(addresses = ["/queues/someQueue"])
	fun processMessage(content: String?) {
		// ...
	}

}
詳細については、@EnableAmqp (Javadoc) を参照してください。

If you need to create more MethodAmqpMessageListenerContainerFactory (Javadoc) instances or if you want to override the default, Spring Boot provides a AmqpMessageListenerContainerFactoryConfigurer (Javadoc) that you can use to initialize a MethodAmqpMessageListenerContainerFactory (Javadoc) with the same settings as the factory used by the auto-configuration.

たとえば、次の構成クラスは、特定の MessageConverter (Javadoc) を使用する別のファクトリを公開します。

  • Java

  • Kotlin

import org.springframework.amqp.client.AmqpConnectionFactory;
import org.springframework.amqp.client.config.MethodAmqpMessageListenerContainerFactory;
import org.springframework.boot.amqp.autoconfigure.AmqpMessageListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyAmqpConfiguration {

	@Bean
	public MethodAmqpMessageListenerContainerFactory myFactory(AmqpMessageListenerContainerFactoryConfigurer configurer,
			AmqpConnectionFactory connectionFactory) {
		MethodAmqpMessageListenerContainerFactory factory = new MethodAmqpMessageListenerContainerFactory(
				connectionFactory);
		configurer.configure(factory);
		factory.setMessageConverter(new MyMessageConverter());
		return factory;
	}

}
import org.springframework.amqp.client.AmqpConnectionFactory
import org.springframework.amqp.client.config.MethodAmqpMessageListenerContainerFactory
import org.springframework.boot.amqp.autoconfigure.AmqpMessageListenerContainerFactoryConfigurer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyAmqpConfiguration {

	@Bean
	fun myFactory(
		configurer: AmqpMessageListenerContainerFactoryConfigurer,
		connectionFactory: AmqpConnectionFactory
	): MethodAmqpMessageListenerContainerFactory {
		val factory = MethodAmqpMessageListenerContainerFactory(connectionFactory)
		configurer.configure(factory)
		factory.setMessageConverter(MyMessageConverter())
		return factory
	}

}

次に、次のように、任意の @AmqpListener (Javadoc) アノテーション付きメソッドでファクトリを使用できます。

  • Java

  • Kotlin

import org.springframework.amqp.client.annotation.AmqpListener;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	@AmqpListener(addresses = "/queues/someQueue", containerFactory = "myFactory")
	public void processMessage(String content) {
		// ...
	}

}
import org.springframework.amqp.client.annotation.AmqpListener
import org.springframework.stereotype.Component

@Component
class MyBean {

	@AmqpListener(addresses = ["/queues/someQueue"], containerFactory = "myFactory")
	fun processMessage(content: String?) {
		// ...
	}

}

RabbitMQ AMQP 1.0 サポート

Spring AMQP provides RabbitMQ dedicated support for AMQP 1.0 via org.springframework.amqp:spring-rabbitmq-client.

RabbitMQ AMQP 1.0 configuration is controlled by external configuration properties in spring.amqp.rabbitmq.*. For example, you might declare the following section in application.properties:

  • プロパティ

  • YAML

spring.amqp.rabbitmq.host=localhost
spring.amqp.rabbitmq.port=5672
spring.amqp.rabbitmq.username=admin
spring.amqp.rabbitmq.password=secret
spring:
  amqp:
    rabbitmq:
      host: "localhost"
      port: 5672
      username: "admin"
      password: "secret"

または、address 属性を使用して同じ接続を構成できます。

  • プロパティ

  • YAML

spring.amqp.rabbitmq.address=amqp://admin:secret@localhost
spring:
  amqp:
    rabbitmq:
      address: "amqp://admin:secret@localhost"
When specifying an address that way, the host and port properties are ignored. If the address uses the amqps scheme, an SSL バンドル must be configured.

See AmqpRabbitProperties (Javadoc) for more of the supported property-based configuration options. To configure lower-level details of the auto-configured Environment (英語) , define a AmqpEnvironmentCustomizer (Javadoc) bean.

If a CredentialsProvider (英語) bean exists in the context, it will be automatically used by the auto-configured Environment (英語)

SSL

To use SSL with RabbitMQ AMQP 1.0, set spring.amqp.rabbitmq.ssl.bundle to configure the SSL バンドル to use.

メッセージの送信

Spring の RabbitAmqpTemplate (Javadoc) RabbitAmqpAdmin (Javadoc) は自動構成されており、次の例に示すように、独自の Bean に直接自動接続できます。

  • Java

  • Kotlin

import org.springframework.amqp.rabbitmq.client.RabbitAmqpAdmin;
import org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	private final RabbitAmqpAdmin amqpAdmin;

	private final RabbitAmqpTemplate amqpTemplate;

	public MyBean(RabbitAmqpAdmin amqpAdmin, RabbitAmqpTemplate amqpTemplate) {
		this.amqpAdmin = amqpAdmin;
		this.amqpTemplate = amqpTemplate;
	}

	// ...

	public void someOtherMethod() {
		this.amqpTemplate.convertAndSend("hello");
	}

}
import org.springframework.amqp.rabbitmq.client.RabbitAmqpAdmin
import org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate
import org.springframework.stereotype.Component

@Component
class MyBean(private val amqpAdmin: RabbitAmqpAdmin, private val amqpTemplate: RabbitAmqpTemplate) {

	// ...

	fun someOtherMethod() {
		amqpTemplate.convertAndSend("hello")
	}

}

If a MessageConverter (Javadoc) bean is defined, it is associated automatically to the auto-configured RabbitAmqpTemplate (Javadoc)

You can set properties for the template as follows:

  • プロパティ

  • YAML

spring.amqp.rabbitmq.template.exchange=my-exchange
spring.amqp.rabbitmq.template.routing-key=my-key
spring.amqp.rabbitmq.template.default-receive-queue=my-queue
spring:
  amqp:
    rabbitmq:
      template:
        exchange: "my-exchange"
        routing-key: "my-key"
        default-receive-queue: "my-queue"

To further configure the auto-configured RabbitAmqpTemplate (Javadoc) , declare a RabbitAmqpTemplateCustomizer (Javadoc) bean.

メッセージの受信

When the RabbitMQ AMQP 1.0 infrastructure is present, any bean can be annotated with @RabbitListener (Javadoc) to create a listener endpoint. If no RabbitAmqpListenerContainerFactory (Javadoc) has been defined, a default one is automatically configured. If a MessageConverter (Javadoc) bean is defined, it is associated automatically with the default factory.

次のサンプルコンポーネントは、someQueue キューにリスナーエンドポイントを作成します。

  • Java

  • Kotlin

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "someQueue")
public class MyBean {

	@RabbitHandler
	public void processMessage(String content) {
		// ...
	}

}
import org.springframework.amqp.rabbit.annotation.RabbitHandler
import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.stereotype.Component

@Component
@RabbitListener(queues = ["someQueue"])
class MyBean {

	@RabbitHandler
	fun processMessage(content: String?) {
		// ...
	}

}
詳細については、@EnableRabbit (Javadoc) を参照してください。

If you need to create more RabbitAmqpListenerContainerFactory (Javadoc) instances or if you want to override the default, Spring Boot provides a RabbitAmqpListenerContainerFactoryConfigurer (Javadoc) that you can use to initialize a RabbitAmqpListenerContainerFactory (Javadoc) with the same settings as the factory used by the auto-configuration.

たとえば、次の構成クラスは、特定の MessageConverter (Javadoc) を使用する別のファクトリを公開します。

  • Java

  • Kotlin

import org.springframework.amqp.rabbitmq.client.AmqpConnectionFactory;
import org.springframework.amqp.rabbitmq.client.config.RabbitAmqpListenerContainerFactory;
import org.springframework.boot.amqp.rabbitmq.autoconfigure.RabbitAmqpListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyRabbitAmqpConfiguration {

	@Bean
	public RabbitAmqpListenerContainerFactory myFactory(RabbitAmqpListenerContainerFactoryConfigurer configurer,
			AmqpConnectionFactory connectionFactory) {
		RabbitAmqpListenerContainerFactory factory = new RabbitAmqpListenerContainerFactory(connectionFactory);
		configurer.configure(factory);
		factory.setMessageConverter(new MyMessageConverter());
		return factory;
	}

}
import org.springframework.amqp.rabbitmq.client.AmqpConnectionFactory
import org.springframework.amqp.rabbitmq.client.config.RabbitAmqpListenerContainerFactory
import org.springframework.boot.amqp.rabbitmq.autoconfigure.RabbitAmqpListenerContainerFactoryConfigurer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyRabbitAmqpConfiguration {

	@Bean
	fun myFactory(
		configurer: RabbitAmqpListenerContainerFactoryConfigurer,
		connectionFactory: AmqpConnectionFactory
	): RabbitAmqpListenerContainerFactory {
		val factory = RabbitAmqpListenerContainerFactory(connectionFactory)
		configurer.configure(factory)
		factory.setMessageConverter(MyMessageConverter())
		return factory
	}

}

次に、次のように、任意の @RabbitListener (Javadoc) アノテーション付きメソッドでファクトリを使用できます。

  • Java

  • Kotlin

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	@RabbitListener(queues = "someQueue", containerFactory = "myFactory")
	public void processMessage(String content) {
		// ...
	}

}
import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.stereotype.Component

@Component
class MyBean {

	@RabbitListener(queues = ["someQueue"], containerFactory = "myFactory")
	fun processMessage(content: String?) {
		// ...
	}

}

To customize the listener container, define a ContainerCustomizer (Javadoc) bean parameterized with RabbitAmqpListenerContainer (Javadoc)

RabbitMQ AMQP 0.9 サポート

RabbitMQ (英語) は、AMQP プロトコルに基づく、軽量で信頼性が高く、スケーラブルでポータブルなメッセージブローカーです。Spring は、RabbitMQ を使用して AMQP プロトコルを介して通信します。

RabbitMQ 構成は、spring.rabbitmq.* の外部構成プロパティによって制御されます。例: application.properties で次のセクションを宣言できます。

  • プロパティ

  • YAML

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=secret
spring:
  rabbitmq:
    host: "localhost"
    port: 5672
    username: "admin"
    password: "secret"

または、addresses 属性を使用して同じ接続を構成できます。

  • プロパティ

  • YAML

spring.rabbitmq.addresses=amqp://admin:secret@localhost
spring:
  rabbitmq:
    addresses: "amqp://admin:secret@localhost"
その方法でアドレスを指定する場合、host および port プロパティは無視されます。アドレスが amqps プロトコルを使用している場合、SSL サポートは自動的に有効になります。

サポートされているプロパティベースの構成オプションの詳細については、RabbitProperties (Javadoc) を参照してください。Spring AMQP で使用される RabbitMQ ConnectionFactory (英語) の下位レベルの詳細を構成するには、ConnectionFactoryCustomizer (Javadoc) Bean を定義します。

コンテキスト内に ConnectionNameStrategy (Javadoc) Bean が存在する場合、自動構成された CachingConnectionFactory (Javadoc) によって作成された接続の名前として自動的に使用されます。

RabbitTemplate (Javadoc) に対してアプリケーション全体の追加カスタマイズを行うには、RabbitTemplateCustomizer (Javadoc) Bean を使用します。

詳細については、RabbitMQ で使用されるプロトコルである AMQP を理解する (英語) を参照してください。

メッセージの送信

Spring の AmqpTemplate (Javadoc) AmqpAdmin (Javadoc) は自動構成されており、次の例に示すように、独自の Bean に直接自動接続できます。

  • Java

  • Kotlin

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	private final AmqpAdmin amqpAdmin;

	private final AmqpTemplate amqpTemplate;

	public MyBean(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
		this.amqpAdmin = amqpAdmin;
		this.amqpTemplate = amqpTemplate;
	}

	// ...

	public void someMethod() {
		this.amqpAdmin.getQueueInfo("someQueue");
	}

	public void someOtherMethod() {
		this.amqpTemplate.convertAndSend("hello");
	}

}
import org.springframework.amqp.core.AmqpAdmin
import org.springframework.amqp.core.AmqpTemplate
import org.springframework.stereotype.Component

@Component
class MyBean(private val amqpAdmin: AmqpAdmin, private val amqpTemplate: AmqpTemplate) {

	// ...

	fun someMethod() {
		amqpAdmin.getQueueInfo("someQueue")
	}

	fun someOtherMethod() {
		amqpTemplate.convertAndSend("hello")
	}

}
RabbitMessagingTemplate (Javadoc) も同様の方法で注入できます。MessageConverter (Javadoc) Bean が定義されている場合、自動構成された AmqpTemplate (Javadoc) に自動的に関連付けられます。

必要に応じて、Bean として定義されている Queue (Javadoc) は、RabbitMQ インスタンス上の対応するキューを宣言するために自動的に使用されます。

操作を再試行するには、AmqpTemplate (Javadoc) で再試行を有効にします (たとえば、ブローカー接続が失われた場合など)。

  • プロパティ

  • YAML

spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.template.retry.initial-interval=2s
spring:
  rabbitmq:
    template:
      retry:
        enabled: true
        initial-interval: "2s"

再試行はデフォルトで無効になっています。RabbitTemplateRetrySettingsCustomizer (Javadoc) Bean を宣言して、プログラムで RetryTemplate (Javadoc) をカスタマイズすることもできます。

さらに多くの RabbitTemplate (Javadoc) インスタンスを作成する必要がある場合、またはデフォルトをオーバーライドする場合、Spring Boot は、自動構成で使用されるファクトリと同じ設定で RabbitTemplate (Javadoc) を初期化するために使用できる RabbitTemplateConfigurer (Javadoc) Bean を提供します。

コンテキスト内に型 RabbitTemplateObservationConvention (Javadoc) の Bean がある場合、RabbitTemplate (Javadoc) に自動的に構成されます。

ストリームへのメッセージの送信

特定のストリームにメッセージを送信するには、次の例に示すように、ストリームの名前を指定します。

  • プロパティ

  • YAML

spring.rabbitmq.stream.name=my-stream
spring:
  rabbitmq:
    stream:
      name: "my-stream"

さらに多くの RabbitStreamTemplate (Javadoc) インスタンスを作成する必要がある場合、またはデフォルトをオーバーライドする場合、Spring Boot は、自動構成で使用されるファクトリと同じ設定で RabbitStreamTemplate (Javadoc) を初期化するために使用できる RabbitStreamTemplateConfigurer (Javadoc) Bean を提供します。

SSL

RabbitMQ Streams で SSL を使用するには、spring.rabbitmq.stream.ssl.enabled を true に設定するか、spring.rabbitmq.stream.ssl.bundle を設定して使用する SSL バンドルを構成します。

メッセージの受信

Rabbit インフラストラクチャが存在する場合、任意の Bean に @RabbitListener (Javadoc) アノテーションを付与してリスナーエンドポイントを作成できます。RabbitListenerContainerFactory (Javadoc) が定義されていない場合は、デフォルトの SimpleRabbitListenerContainerFactory (Javadoc) が自動的に構成され、spring.rabbitmq.listener.type プロパティを使用して直接コンテナーに切り替えることができます。MessageConverter (Javadoc) MessageRecoverer (Javadoc) RabbitListenerObservationConvention (Javadoc) の Bean が定義されている場合は、デフォルトのファクトリに自動的に関連付けられます。

次のサンプルコンポーネントは、someQueue キューにリスナーエンドポイントを作成します。

  • Java

  • Kotlin

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	@RabbitListener(queues = "someQueue")
	public void processMessage(String content) {
		// ...
	}

}
import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.stereotype.Component

@Component
class MyBean {

	@RabbitListener(queues = ["someQueue"])
	fun processMessage(content: String?) {
		// ...
	}

}
詳細については、@EnableRabbit (Javadoc) を参照してください。

さらに多くの RabbitListenerContainerFactory (Javadoc) インスタンスを作成する必要がある場合、またはデフォルトをオーバーライドする場合、Spring Boot は、自動構成で使用されるファクトリと同じ設定で SimpleRabbitListenerContainerFactory (Javadoc) および DirectRabbitListenerContainerFactory (Javadoc) を初期化するために使用できる SimpleRabbitListenerContainerFactoryConfigurer (Javadoc) および DirectRabbitListenerContainerFactoryConfigurer (Javadoc) を提供します。

選択したコンテナーの種類は関係ありません。これらの 2 つの Bean は、自動構成によって公開されます。

たとえば、次の構成クラスは、特定の MessageConverter (Javadoc) を使用する別のファクトリを公開します。

  • Java

  • Kotlin

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.rabbitmq.autoconfigure.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyRabbitConfiguration {

	@Bean
	public SimpleRabbitListenerContainerFactory myFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer) {
		SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
		ConnectionFactory connectionFactory = getCustomConnectionFactory();
		configurer.configure(factory, connectionFactory);
		factory.setMessageConverter(new MyMessageConverter());
		return factory;
	}

	private ConnectionFactory getCustomConnectionFactory() {
		return ...
	}

}
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory
import org.springframework.amqp.rabbit.connection.ConnectionFactory
import org.springframework.boot.rabbitmq.autoconfigure.SimpleRabbitListenerContainerFactoryConfigurer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration(proxyBeanMethods = false)
class MyRabbitConfiguration {

	@Bean
	fun myFactory(configurer: SimpleRabbitListenerContainerFactoryConfigurer): SimpleRabbitListenerContainerFactory {
		val factory = SimpleRabbitListenerContainerFactory()
		val connectionFactory = getCustomConnectionFactory()
		configurer.configure(factory, connectionFactory)
		factory.setMessageConverter(MyMessageConverter())
		return factory
	}

	fun getCustomConnectionFactory() : ConnectionFactory {
		return ...
	}

}

次に、次のように、任意の @RabbitListener (Javadoc) アノテーション付きメソッドでファクトリを使用できます。

  • Java

  • Kotlin

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	@RabbitListener(queues = "someQueue", containerFactory = "myFactory")
	public void processMessage(String content) {
		// ...
	}

}
import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.stereotype.Component

@Component
class MyBean {

	@RabbitListener(queues = ["someQueue"], containerFactory = "myFactory")
	fun processMessage(content: String?) {
		// ...
	}

}

リスナーが例外をスローする状況に対処するために、再試行を有効にすることができます。デフォルトでは RejectAndDontRequeueRecoverer (Javadoc) が使用されますが、独自の MessageRecoverer (Javadoc) を定義することもできます。再試行が失敗すると、メッセージは拒否され、ドロップされるか、ブローカーがそうするように構成されている場合はデッドレター交換にルーティングされます。デフォルトでは、再試行は無効になっています。RabbitListenerRetrySettingsCustomizer (Javadoc) Bean を宣言して、プログラムで RetryPolicy (Javadoc) をカスタマイズすることもできます。

デフォルトでは、再試行が無効でリスナーが例外をスローすると、配信は無期限に再試行されます。この動作は、次の 2 つの方法で変更できます。defaultRequeueRejected プロパティを false に設定して再配信を 0 回試行するか、AmqpRejectAndDontRequeueException (Javadoc) をスローしてメッセージを拒否するように通知します。後者は、再試行が有効で配信試行の最大回数に達した場合に使用されるメカニズムです。