ubuntun下安装rabbitMq
参考链接 :
1.安装
https://blog.csdn.net/a295277302/article/details/71246941
https://blog.csdn.net/junzhen_chen/article/details/78459383
2.使用
https://blog.csdn.net/hzw19920329/article/details/53156015
https://blog.csdn.net/u014308482/article/details/53994401
简单使用
1.导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
2.消息生产者
import com.rabbitmq.client.*;
import java.util.HashMap;
import java.util.Map;
/**
* 消息的生产者
* 将想要发送的消息加入消息队列
* @author ZX
* 参考链接: https://blog.csdn.net/hzw19920329/article/details/53156015
https://blog.csdn.net/u014308482/article/details/53994401
*/
public class Producer {
public static void main(String[]args) throws Exception{
//1、创建链接对象
ConnectionFactory factory = new ConnectionFactory();
//rabbitmq所在ip,用户名密码
factory.setHost("192.168.0.17");
factory.setUsername("admin1");
factory.setPort(5672);
factory.setPassword("123456");
Connection connection = factory.newConnection();
//创建一个频道
Channel channel = connection.createChannel();
//声明一个dirent模式的交换机
/**channel.exchangeDeclare(String exchange,String type,boolean durable,boolean autodelete....)
参数1:exchange 交换机对象的名称
参数2 type
BuiltinExchangeType. DIRECT("direct"),定向发送消息
BuiltinExchangeType. FANOUT("fanout"),会向所有的queue广播所有收到的消息。如log系统可使用此模式
BuiltinExchangeType. TOPIC("topic"),
BuiltinExchangeType. HEADERS("headers");
参数3:durable:如果设置了true,本交换机在重启后也会生存(实际上不一定靠谱,设置为true就好)
参数n:。。。。。。。
*/
channel.exchangeDeclare("exchange_name", BuiltinExchangeType.DIRECT,true);
//声明一个非持久化自动删除的队列,如果该队列不在被使用就删除他 zhe
channel.queueDeclare("queue_name",false,false,true,null);
//将绑定到改交换机
channel.queueBind("queue_name","exchange_name","route_key");
//声明一个消息头部
AMQP.BasicProperties.Builder builder= new AMQP.BasicProperties.Builder();
Map<String,Object> header=new HashMap<String, Object>();
header.put("charset","utf-8");
builder.headers(header);
AMQP.BasicProperties basicProperties=builder.build();
//将消息发出去
channel.basicPublish("exchange_name","route_key",false,basicProperties,"消息的内容".getBytes());
for (int i=0;i<100;i++){
channel.basicPublish("exchange_name","route_key",false,basicProperties,("消息的内容"+i).getBytes());
}
}
}
3.消息消费者
import com.rabbitmq.client.*;
import com.rabbitmq.client.BuiltinExchangeType;
import java.io.IOException;
public class Consumer {
public static void main(String[]args) throws Exception{
//1、创建链接对象
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.0.17");
factory.setUsername("admin1");
factory.setPort(5672);
factory.setPassword("123456");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明一个dirent模式的交换机
/**channel.exchangeDeclare(String exchange,String type,boolean durable,boolean autodelete....)
参数1:exchange 交换机对象的名称
参数2 type
BuiltinExchangeType. DIRECT("direct"),
BuiltinExchangeType. FANOUT("fanout"),
BuiltinExchangeType. TOPIC("topic"),
BuiltinExchangeType. HEADERS("headers");
参数3:durable:如果设置了true,本交换机在重启后也会生存(实际上不一定靠谱,设置为true就好)
参数n:。。。。。。。
*/
channel.exchangeDeclare("exchange_name",BuiltinExchangeType.DIRECT,true);
//声明一个非持久化自动删除的队列,如果该队列不在被使用就删除他
channel.queueDeclare("queue_name",false,false,true,null);
//将绑定到改交换机
channel.queueBind("queue_name","exchange_name","route_key");
//创建消费者对象
com.rabbitmq.client.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);
}
}