Spring AMQPでHelloWorldしてみる

■ メッセージキューイング そこそこの規模のアプリケーションを作ろうとすると、どうしても非同期メッセージング的な仕掛けが欲しくなってきます。 先日KVS(Redis)も簡単にSpringから使えたし、MQを簡単に使えるフレームワークもあるだろうと思って Springのプロジェクト一覧見てたら(http://www.springsource.org/projects)、Spring AMQPというプロジェクトを発見。 AMQPはAdvanced Message Queuing Protocolの略で、今までベンダーごとにインタフェースが違かったり、 みたいのをオープンな標準仕様で統一しましょう的なノリみたいです。   ■ RabbitMQ http://www.amqp.orgがその団体のホームページになりますが、 事例をみていくと、RabbitMQってのが人気みたいです。 で、 http://www.rabbitmq.com/ にアクセスしてみると、右上にSpringSource的な。 なるほど、そういう事か、、と。 何はともあれインストールしてMQサーバを起動したい衝動にかられます、と。   RabbitMQはErlangで動作するソフトウエアなのでErlangのランタイムが必要です。 http://www.erlang.org/download.htmlにはWindows用のインストーラもあるので、落としてきて叩きます。 自分は C:erl5.8.4 にインストールして、環境変数ERLANG_HOMEを追加しました。   続いていよいよRabbitMQ。こちらもhttp://www.rabbitmq.com/server.htmlWindows用のインストーラがあるので、 それに従っていくだけです。インストールが終わったところで起動させてみます。 ↓コレだけ。なんとも味気ないw

C:RabbitMQ Serverrabbitmq_server-2.5.1sbin>rabbitmq-service.bat start
C:erl5.8.4erts-5.8.4binerlsrv: Service RabbitMQ started.

二重起動させようとすると↓のように怒られるのでちゃんと起動してるようです。

C:RabbitMQ Serverrabbitmq_server-2.5.1sbin>rabbitmq-service.bat start
C:erl5.8.4erts-5.8.4binerlsrv: Failed to start service RabbitMQ.
Error: サービス インスタンスは既に実行されています。

  ■ HelloWorld 何はともあれ、プログラマであればHelloWorldしたくなります。 Spring AMQPのリファレンスマニュアルのPDFを流し読みしてみると、 Chapter 3. Sample Applications に 3.2. Hello World の文字がw   どうやら↓のGithubにあるプロジェクトをimportして実行すれば動くみたいなのでさっそくやってみます。 https://github.com/SpringSource/spring-amqp-samples/tree/master/helloworld #この辺Mavenプロジェクトだとアレやコレややらなくていいのでほんと楽チンです。   1. メッセージを送る Redisの時もStringRedisTemplateってのをbean定義ファイルに~とかやりましたが、 こちらもAmqpTemplateってのがあって、それを使います。 元のソースは1メッセージだけでしたが、せっかくなので100メッセージ突っ込んでみます。

public class Producer {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
        AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
        for (int i = 0; i < 100; i++) {
            amqpTemplate.convertAndSend("Hello World " + i);
            System.out.println("Sent: Hello World " + i);
        }

    }
}

↓のような感じで動いてる風です。

INFO  [ation.AnnotationConfigApplicationContext]: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@66edc3a2: startup date [Thu Aug 11 14:24:07 JST 2011]; root of context hierarchy
INFO  [ctory.support.DefaultListableBeanFactory]: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@16aa37a6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,helloWorldConfiguration,connectionFactory,amqpAdmin,rabbitTemplate,helloWorldQueue]; root of factory hierarchy
Sent: Hello World 0
Sent: Hello World 1
~略~
Sent: Hello World 98
Sent: Hello World 99

  2. メッセージを受け取る こちらもテンプレートクラスを使います。上記でつっこんだ100個のメッセージが全部取れるか? ってのと、100回以上受け取ったらどうなるか?ってのがみたかったので101回ループさせてみます。

public class Consumer {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
        AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
        for (int i = 0; i < 101; i++) {
            System.out.println("Received: " + amqpTemplate.receiveAndConvert());
        }
    }
}

結果は↓のようなイイ感じになりました。

INFO  [ation.AnnotationConfigApplicationContext]: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@24a37368: startup date [Thu Aug 11 14:28:35 JST 2011]; root of context hierarchy
INFO  [ctory.support.DefaultListableBeanFactory]: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@444cee32: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,helloWorldConfiguration,connectionFactory,amqpAdmin,rabbitTemplate,helloWorldQueue]; root of factory hierarchy
Received: Hello World 0
Received: Hello World 1
~略~
Received: Hello World 98
Received: Hello World 99
Received: null ★ 無ければnullになる、と。

 

運用の事かんがえると、キューイングされたデータをどんな風に永続化するかーとか、 考えなきゃいけなくなりそうな予感がする今日この頃です。

 

もしこの手の技術を扱うのが初めてだったら予備知識的に ↓のJMSの章を読んでおくと良さげです。とてもコンパクトにイイ感じにまとまってます。

マスタリングJavaEE5 第2版 (DVD付) (Programmer’s SELECTION)
三菱UFJインフォメーションテクノロジー株式会社 斉藤 賢哉
翔泳社
売り上げランキング: 193680