Rabbitmq的helloworld
来源:互联网 发布:行业数据库 编辑:程序博客网 时间:2024/06/06 07:07
1、在linux机器上安装rabbitmq,并启动服务:
2、下载MQ 客户端jar包:amqp-client-3.5.4.jar
3、通过rabbitmq输出”Hello World!”
其中P代表生产者、C表示消费者、中间红色部分代表消息队列
4、生产者客户端的发送消息程序如下:
package com.mq;import java.io.IOException; import java.util.concurrent.TimeoutException;import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class Send { private final static String QUEUE_NAME = "hello"; public static void main(String[] args) throws IOException { try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.11.32"); Connection connection; connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World!"; for(int i=0;i<10;i++){ channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行给过如下:
[x] Sent 'Hello World!' [x] Sent 'Hello World!' [x] Sent 'Hello World!' [x] Sent 'Hello World!' [x] Sent 'Hello World!' [x] Sent 'Hello World!'
5、消费者客户端接收消息程序如下:
package com.mq;import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.QueueingConsumer; public class Reqv { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.11.32"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } } }
运行结果如下:
[*] Waiting for messages. To exit press CTRL+C [x] Received 'Hello World!' [x] Received 'Hello World!' [x] Received 'Hello World!' [x] Received 'Hello World!' [x] Received 'Hello World!' [x] Received 'Hello World!' [x] Received 'Hello World!' [x] Received 'Hello World!'
如果消费者出现“[x] Received ‘Hello World!’”说明已接收到此消息信息。
0 0
- Rabbitmq的helloworld
- rabbitMQ HelloWorld
- RabbitMQ HelloWorld
- RabbitMQ(二)CentOS6.7 下的 HelloWorld
- RabbitMQ(一) Windows下的 HelloWorld
- Windows环境的RabbitMQ之HelloWorld
- RabbitMQ 之HelloWorld
- RabbitMQ 入门 Helloworld
- RabbitMQ 入门 Helloworld
- 初学RabbitMQ之HelloWorld
- RabbitMQ 入门 Helloworld
- RabbitMQ 入门 Helloworld
- RabbitMQ 入门 Helloworld
- RabbitMQ 入门 Helloworld
- RabbitMQ 入门 Helloworld
- RabbitMQ 入门 Helloworld
- RabbitMQ 入门 Helloworld
- RabbitMQ 入门 Helloworld
- document.write和 innerHTML的区别,待完善
- 自定义类做成员变量
- UIViewContentModeScaleAspectFill不凑效的解决办法
- mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)
- mysql全文索引中文问题的解决
- Rabbitmq的helloworld
- libopencv_gpu.so.2.4: undefined reference to 问题
- Redis Sentinel环境下的Key过期事件消息订阅
- zkc学长的福利
- SQL 按匹配条件的多少查询出匹配度最高的配置数据
- Android中View绘制流程以及invalidate()等相关方法分析
- 第十三篇:一球盘面以下,用亚盘解剖欧赔
- Redis在win7上的可视化应用
- 113. Path Sum II