rabbitmq helloword 例子
来源:互联网 发布:网络攻击日语 编辑:程序博客网 时间:2024/06/06 17:38
- package com.abin.test;
- import java.io.IOException;
- 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 {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- String message = "Hello World!";
- channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
- System.out.println(" [x] Sent '" + message + "'");
- channel.close();
- connection.close();
- }
- }
运行结果如下:
- [x] Sent 'Hello World!'
消费者客户端接收消息程序如下:
- package com.abin.test;
- 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("localhost");
- 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!'”说明已接收到此消息信息。
0 0
- rabbitmq helloword 例子
- RabbitMQ之Helloword
- RabbitMQ学习之helloword(java)
- RabbitMQ学习(一)之helloword(java)
- 第2篇 RabbitMQ简介实现HelloWord
- RabbitMQ(一)——HelloWord
- 一个axis2 webservice helloword例子
- 一、第一个Struts2例子(Helloword)
- Spring+RabbitMq简单例子
- RabbitMQ搭建与例子
- rabbitmq java 例子
- RabbitMQ简单例子
- HelloWord
- HelloWord
- helloword
- Helloword
- HelloWord!
- helloword
- poj1155
- SAP 上线切换
- WinForm ListView虚拟模式加载数据 提高加载速度
- Android安全--运行时验证签名
- 李大霄的黑五类
- rabbitmq helloword 例子
- Android消息队列模型——Thread,Handler,Looper,Massage Queue
- 天天刷水题~~~(此题题意有点难理解)
- xml及json解析
- 学写thinkPHP Mac10.10
- 【JavaWeb】(8)监听器
- git忽略已被跟踪的文件
- Android开发四大组件---Service详解
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)