Java操作RabbitMQ简单队列

1、创建工具类

1package com.kobe.rabbitmq; 2 3import com.rabbitmq.client.Connection; 4import com.rabbitmq.client.ConnectionFactory; 5 6import java.io.IOException; 7import java.util.concurrent.TimeoutException; 8 9public class ConnectionUtils { 10 11 public static Connection getConnection() throws TimeoutException,IOException { 12 13 ConnectionFactory factory = new ConnectionFactory(); 14 15 factory.setHost("127.0.0.1"); 16 17 factory.setPort(5672); 18 19 factory.setVirtualHost("/vhost_kobe"); 20 21 factory.setUsername("kobe"); 22 23 factory.setPassword("123"); 24 25 return factory.newConnection(); 26 } 27 28}

2、创建生产者

1package com.kobe.rabbitmq; 2 3import com.rabbitmq.client.Channel; 4import com.rabbitmq.client.Connection; 5 6public class SendSms { 7 private static final String QUEUE_NAME = "simple_queue"; 8 9 public static void main(String[] args) { 10 Connection connection = null; 11 Channel channel = null; 12 try { 13 connection = ConnectionUtils.getConnection(); 14 channel = connection.createChannel(); 15 channel.queueDeclare(QUEUE_NAME,false,false,false,null); 16 String msg = "hello rabbitmq : " + System.currentTimeMillis(); 17 channel.basicPublish("",QUEUE_NAME,null,msg.getBytes()); 18 System.out.println("send msg to rabbitmq:" + msg ); 19 } catch (Exception e ) { 20 e.printStackTrace(); 21 } finally { 22 try { 23 channel.close(); 24 connection.close(); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 } 28 } 29 } 30}

3、创建消费者

1package com.kobe.rabbitmq; 2 3import com.rabbitmq.client.*; 4 5import java.io.IOException; 6 7public class ReceiveSms { 8 9 private static final String QUEUE_NAME = "simple_queue"; 10 11 public static void main(String[] args) { 12 Connection connection = null; 13 Channel channel = null; 14 try { 15 connection = ConnectionUtils.getConnection(); 16 channel = connection.createChannel(); 17 channel.queueDeclare(QUEUE_NAME,false,false,false,null); 18 DefaultConsumer consumer = new DefaultConsumer(channel){ 19 //一旦有消息进入队列就会触发 20 @Override 21 public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { 22 String msg = new String (body,"utf-8"); 23 System.out.println("receive msg :" + msg); 24 } 25 }; 26 //监听队列 27 channel.basicConsume(QUEUE_NAME,true,consumer); 28 29 } catch (Exception e ) { 30 e.printStackTrace(); 31 } 32 } 33 34}

4、运行生产者,往队列里存数据

输出结果:send msg to rabbitmq:hello rabbitmq : 1534087498613

5、查看RabbitMQ Management

可以看得到数据已经存入队列

6、运行消费者进行消息监听

输出结果:receive msg :hello rabbitmq : 1534087498613

7、再次运行生产者

输出结果:send msg to rabbitmq:hello rabbitmq : 1534087638186

消费者监听到之后打印出 receive msg :hello rabbitmq : 1534087638186

8、查看RabbitMQ Management 

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )