せっかちな人のためのクイックツアー
導入
これは、Spring AMQP を開始するための 5 分間のツアーです。
前提条件: RabbitMQ ブローカー ( https://www.rabbitmq.com/download.html (英語) ) をインストールして実行します。次に、spring-rabbit JAR とそのすべての依存関係を取得します。これを行う最も簡単な方法は、ビルドツールで依存関係を宣言することです。例: Maven の場合、次のようなことができます。
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>3.1.6</version>
</dependency>
Gradle の場合、次のようなことができます。
compile 'org.springframework.amqp:spring-rabbit:3.1.6'
互換性
Spring Framework の最小バージョン依存関係は 6.1.0 です。
amqp-client
Java クライアントライブラリの最小バージョンは 5.18.0 です。
ストリームキュー用の最小 stream-client
Java クライアントライブラリは 0.12.0 です。
とても、とても速い
このセクションでは、最速の導入を提供します。
最初に、次の import
ステートメントを追加して、このセクションで後述する例を機能させます。
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
次の例では、プレーンな命令型 Java を使用してメッセージを送受信します。
ConnectionFactory connectionFactory = new CachingConnectionFactory();
AmqpAdmin admin = new RabbitAdmin(connectionFactory);
admin.declareQueue(new Queue("myqueue"));
AmqpTemplate template = new RabbitTemplate(connectionFactory);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
ネイティブ Java Rabbit クライアントにも ConnectionFactory
があることに注意してください。前のコードでは Spring 抽象化を使用しています。再利用のためにチャネル (およびオプションで接続) をキャッシュします。ブローカーのデフォルトの交換 (送信で何も指定されていないため) と、名前によるデフォルトの交換へのすべてのキューのデフォルトのバインディングに依存します (したがって、送信でキュー名をルーティングキーとして使用できます)。これらの動作は、AMQP 仕様で定義されています。
XML 構成あり
次の例は前の例と同じですが、リソース設定を XML に外部化します。
ApplicationContext context =
new GenericXmlApplicationContext("classpath:/rabbit-context.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
https://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<rabbit:connection-factory id="connectionFactory"/>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>
<rabbit:admin connection-factory="connectionFactory"/>
<rabbit:queue name="myqueue"/>
</beans>
デフォルトでは、<rabbit:admin/>
宣言は自動的に型 Queue
、Exchange
、Binding
の Bean を探し、ユーザーに代わってブローカーに宣言します。その結果、単純な Java ドライバーでその Bean を明示的に使用する必要はありません。XML スキーマのコンポーネントのプロパティを構成するためのオプションが多数あります。XML エディターの自動補完機能を使用して、調べたり、ドキュメントを参照したりできます。
Java 構成を使用
次の例では、前の例と同じ例を繰り返しますが、Java で外部構成が定義されています。
ApplicationContext context =
new AnnotationConfigApplicationContext(RabbitConfiguration.class);
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
........
@Configuration
public class RabbitConfiguration {
@Bean
public CachingConnectionFactory connectionFactory() {
return new CachingConnectionFactory("localhost");
}
@Bean
public RabbitAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}
@Bean
public Queue myQueue() {
return new Queue("myqueue");
}
}
Spring Boot 自動構成と非同期 POJO リスナーを使用
次の例に示すように、Spring Boot はインフラストラクチャ Bean を自動的に構成します。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public ApplicationRunner runner(AmqpTemplate template) {
return args -> template.convertAndSend("myqueue", "foo");
}
@Bean
public Queue myQueue() {
return new Queue("myqueue");
}
@RabbitListener(queues = "myqueue")
public void listen(String in) {
System.out.println(in);
}
}