RabbitMQ第一个案例:hello world
来源:互联网 发布:华通云数据与马云关系 编辑:程序博客网 时间:2024/06/06 05:44
前提:把rabbitMQ安装完成
Producer:
package yy.rabbitstudy;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) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = null; Channel channel = null; try { //获取连接 connection = factory.newConnection(); //创建通道 channel = connection.createChannel(); //打开队列位置 channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World RabbitMQ"; //把数据发布到队列 channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println("[x] Sent" + message + ""); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
Consumer:
package yy.rabbitstudy;import java.io.IOException;import java.util.concurrent.TimeoutException;import com.rabbitmq.client.AMQP;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.Consumer;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.Envelope;public class Recv { private final static String QUEUE_NAME="hello"; public static void main(String[] args) throws IOException, TimeoutException { 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"); //把通道交给消费者 Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(QUEUE_NAME, true, consumer); }}
0 0
- RabbitMQ第一个案例:hello world
- RabbitMQ (一)第一个hello world
- springboot开发 第一个案例之hello,world!
- 第一个驱动程序---Hello world
- 第一个“Hello World”程序
- 第一个atlas "Hello world"
- 第一个dojo hello world
- 第一个dojo hello world
- 第一个dojo hello world
- 第一个应用程序Hello World
- 第一个程序:"Hello World"
- 第一个程序:Hello World
- 第一个汇编程序hello world
- 第一个程序:Hello World
- Hello World 第一个程序
- redis 第一个hello world
- vue 第一个hello world
- 第一个程序“Hello World !”
- Andorid源码下载
- 使用Artifactory搭建本地maven仓库
- 彩笔笔记2016/12/5——改写equals
- 【玩转SQLite系列】(七)打造轻量级ORM工具类SQLiteDbUtil操作数据库
- 浅谈 Scala 中下划线的用途
- RabbitMQ第一个案例:hello world
- Appium环境配置-1
- Fragment的基本应用
- js判断是否是Json对象
- springMVC接收表单传来的file,并上传
- tab栏切换案例
- Servlet详解(二):request和response对象
- git 生成ssh key及使用过程
- linux内核优化