@Grab(group='com.rabbitmq', module='amqp-client', version='2.6.1') import com.rabbitmq.client.* factory = new ConnectionFactory() factory.host = "localhost" connection = factory.newConnection() channel = connection.createChannel() // queueを作成 queueName = "myqueue" channel.queueDeclare(queueName, true /*= durable */, false/*= exclusive*/, false /*= autoDelete*/, null /*= arguments */) // メッセージを送信 channel.basicPublish("", queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, "サンプルメッセージ".getBytes("UTF-8")) channel.basicPublish("", queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, "another message".getBytes("UTF-8")) channel.close() connection.close()メッセージの受信を行うには以下のコードを実行する際にmyqueueをコマンドライン引数で指定します。
@Grab(group='com.rabbitmq', module='amqp-client', version='2.6.1') import com.rabbitmq.client.* factory = new ConnectionFactory() factory.host = "localhost" connection = factory.newConnection() channel = connection.createChannel() channel.queueDeclare(args[0], true /*= durable */, false/*= exclusive*/, false /*= autoDelete*/, null /*= arguments*/) // 1つだけメッセージを受信 response = channel.basicGet(args[0], false) if( response == null ){ // メッセージなし println "there's no message." } else { println "${args[0]} received:" + new String(response.getBody(), "UTF-8") channel.basicAck(response.getEnvelope().getDeliveryTag(), false) } channel.close() connection.close()
動作環境
groovy 1.8.2, JDK6 Update26, rabbitmq 2.6.1
0 件のコメント:
コメントを投稿