@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() // direct exchangeを作成 exchangeName = "my_direct_exchange" channel.exchangeDeclare(exchangeName, "direct") // queueを作成&bind queueName1 = "groovy_queue" channel.queueDeclare(queueName1, true /*= durable */, false/*= exclusive*/, false /*= autoDelete*/, null /*= arguments */) channel.queueBind(queueName1, exchangeName, "groovy") queueName2 = "java_queue" channel.queueDeclare(queueName2, true /*= durable */, false/*= exclusive*/, false /*= autoDelete*/, null /*= arguments */) channel.queueBind(queueName2, exchangeName, "java") queueName3 = "languages_queue" channel.queueDeclare(queueName3, true /*= durable */, false/*= exclusive*/, false /*= autoDelete*/, null /*= arguments */) channel.queueBind(queueName3, exchangeName, "java") channel.queueBind(queueName3, exchangeName, "groovy") // メッセージを送信 channel.basicPublish(exchangeName, "groovy", MessageProperties.PERSISTENT_TEXT_PLAIN, "direct exchange サンプルメッセージ:groovy".getBytes("UTF-8")) // メッセージを送信 channel.basicPublish(exchangeName, "java", MessageProperties.PERSISTENT_TEXT_PLAIN, "direct exchange サンプルメッセージ:java".getBytes("UTF-8")) channel.close() connection.close()メッセージを受信するにはgroovyとrabbitmqでメッセージの送信・受信をおこなうの受信プログラムと同様にgroovy_queue, java_queue, langurages_queueをコマンドライン引数に指定します。
動作環境
groovy 1.8.2, JDK6 Update26, rabbitmq 2.6.1
0 件のコメント:
コメントを投稿