rabbitmq的简单使用
package com.example.rabbitmqjava.rabbitmq.simple;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* 生产者
*/
public class Producer {
public static void main(String[] args) {
//所有的中间件技术都是基于tcp、ip协议的基础之上后见新型规范,rabbitmq基于amqp
//1.创建连接工程
ConnectionFactory connect = new ConnectionFactory();
connect.setHost("192.168.2.102");
connect.setPort(5672);
connect.setUsername("admin");
connect.setPassword("admin");
connect.setVirtualHost("/");
//2.创建连接connection
Channel channel=null;
Connection con=null;
try {
con = connect.newConnection("消费者");
//3.通过连接获取连接通道
channel = con.createChannel();
//4.通过创建交换机,声明队列,绑定关系,路由key,发送消息,和接收消息
String queueName="queue1";
//6.接收队列中的消息
channel.basicConsume(queueName, true, new DeliverCallback() {
@Override
public void handle(String s, Delivery message) throws IOException {
System.out.println("收到的消息是" + new String(message.getBody(), "UTF-8"));
}
}, new CancelCallback() {
@Override
public void handle(String s) throws IOException {
System.out.println("接收失败了");
}
});
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}finally {
//7. 关闭通道
if(channel!=null&&channel.isOpen()){
try{
channel.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
//8.关闭连接
if(con!=null&&con.isOpen()){
try{
con.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}
}
}
---------------------------------------------------------------------------
国之殇,未敢忘!
南京大屠杀!
731部队!
(有关书籍《恶魔的饱食》)以及核污染水排海等一系列全无人性的操作,购买他们的食品和为它们提供帮助只会更加变本加厉的害你,呼吁大家不要购买日本相关产品
昭昭前事,惕惕后人
吾辈当自强,方使国不受他人之侮!
---------------------------------------------------------------------------
作者:三号小玩家
出处:https://www.cnblogs.com/q1359720840/
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 版权信息