spring-bootとgroovyでActiveMQ
spring-bootの人気がすごいと聞いてgroovyでサンプルを書きかえてしまうシリーズ!(シリーズかするのか???)
元ネタは、 Messaging with JMS
http://spring.io/guides/gs/messaging-jms/
おっと。
このエントリはG*Advent Calendar(Groovy,Grails,Gradle,Spock...) Advent Calendar 2014 - Qiitaの12/16 担当分です。
では、先ずは準備!
spring bootのインストールをgroovy文化のgvmで!
gvm i springboot
何!gvmしらない?? コチラを参照: http://gvmtool.net/
元記事の http://spring.io/guides/gs/messaging-jms/ では、Gradle、Maven、STSでそれぞれのセットアップ方法が書かれています。
元記事の内容を要約すると
です。
あらヤダ、やることいっぱい。
では、ザックリ。
これはspring-boot 1.1.9でやってみた内容。"spring-boot 1.2 でもやってみた。"も追記しました。コードがさらに短く。
適当なディレクトリを作成して、次のコードを amq-example.groovy として作成。
@Grab("org.springframework:spring-jms:4.0.8.RELEASE") @Grab("org.apache.activemq:activemq-broker:5.9.1") @Grab("org.apache.activemq:activemq-pool:5.9.1") import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Bean import org.springframework.context.ConfigurableApplicationContext import org.springframework.jms.listener.SimpleMessageListenerContainer import org.springframework.jms.listener.adapter.MessageListenerAdapter import javax.jms.ConnectionFactory class App { static String destination = "amq-example" @Bean Receiver receiver() { return new Receiver() } @Bean MessageListenerAdapter adapter(Receiver receiver) { def messageListener = new MessageListenerAdapter(receiver) messageListener.setDefaultListenerMethod("message") return messageListener } @Bean SimpleMessageListenerContainer container(MessageListenerAdapter messageListener, ConnectionFactory connectionFactory) { def container = new SimpleMessageListenerContainer() container.setMessageListener(messageListener) container.setConnectionFactory(connectionFactory) container.setDestinationName(destination) return container } } class Receiver { @Autowired ConfigurableApplicationContext context void message(String message) { println "Received <${message}>" } }
同じ階層にactivemqサーバ参照の設定。application.properties を作成。
※application.properties が書いてないと勝手に組込ActiveMQになるっぽい。
spring.activemq.broker-url=tcp://127.0.0.1:61616 spring.activemq.pooled=true
あ、ちなみに、activemqインストールして起動しておいてね。方法は省略。いや簡易説明。
http://activemq.apache.org/download.html からダウンロードして解凍。
解凍したディレクトリに入って "./bin/activemq console" と実行。
では、起動!
springコマンドで起動。
spring run amq-example.groovy
ランタイムパッケージ作成と起動は、
spring jar amq-example.jar amq-example.groovy java -jar amq-example.jar
動作確認は、ActiveMQの
http://localhost:8161/admin/queues.jsp などの画面から。Send To実行サクッとね。
みんな試したとして。
ほら、簡単ですね。
Grails 3でも、ほぼ同じコードでサクッと動作します。この件はGrails 3のベータが出たくらいに書こうかな。
まとめ。
- groovyでspring-bootは簡単!
- groovyとspring-bootコマンドだとspring-boot-starter-webの依存定義も省略可能!
- groovyでspring-bootすると、@Configuration @EnableAutoConfiguration を省略可能なのと main にデフォ内容以外何も書かないのであれば mainも省略可能。
- groovyでspring-bootはコードを1ファイルにまとめてかけるから、本気のマイクロ!
マイクロサービスを作るのですから〜。コードもマイクロに〜!
ほら、みなさんも! spring-boot は是非groovyで!
追記:
spring-boot 1.2 だともっと短くできるかもね。
http://www.slideshare.net/makingx/spring-boot12/25
追記:
spring-boot 1.2 でもやってみた。かっけー!
@Grab("org.springframework:spring-jms:4.1.3.RELEASE") @Grab("org.apache.activemq:activemq-broker:5.9.1") @Grab("org.apache.activemq:activemq-pool:5.9.1") import org.springframework.jms.annotation.JmsListener @Component class Receiver { @JmsListener(destination = "amq-example") public void message(String message){ println "Received <${message}>" } }