leftovers...

about grails groovy

調子にのって、grails3 web-micro プロファイルでActiveMQしてみた。

spring-bootで”spring-bootとgroovyでActiveMQ”したので。
以下の記事。
http://d.hatena.ne.jp/mottsnite/20141216/1418699308


調子にのってリリースもされていない、 Grails 3 でもやってみた。
しかも、なんとなくの、 web-microプロファイルで。
ん?それじゃ spring-boot ぢゃんとか言わないの。


Grails 3からのプロファイル付きアプリ生成で。

grails create-app amqm --profile=web-micro

アプリ名のフォルダと共に、Application.groovyとapplication.ymlが生成されます。

Application.groovyをの中身をザックリ変更。

@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

class Receiver { 
    @JmsListener(destination = "amq-example3") 
    public void message(String message){
        println "Received <${message}>"
    } 
}

grailsでは、@Component が不要なのですね。付けたら動かなかった。。

サーバ参照の設定は application.yml に以下を追記

spring:
    activemq:
        brokerUrl: tcp://127.0.0.1:61616
        pooled: true

起動方法は、 grails といつも通りgrailsの起動をしてインタラクティブモードに入り、 run-app !!
あら、サックリ動きました。
パッケージするときは、インタラクティブモードで、 package と実行。
あとは。

java -jar build/amqm-0.1.jar

ちなみに、現時点では、(正式リリースされてからも!?) 起動させるとなんと、 もう spring-boot です!

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、MavenSTSでそれぞれのセットアップ方法が書かれています。
元記事の内容を要約すると

  • Gradleなどのビルドファイルを作成
  • ライブラリ依存の設定
  • メッセージレシーバー: Receiver.java
  • メインファイル: Application.java
  • ビルド実行
  • 起動

です。
あらヤダ、やることいっぱい。



では、ザックリ。
これは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}>"
    } 
}