目录
一、producer
1,引入包
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.9.0</version>
</dependency>
2,创建main
package com.wayne;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Producer_hello_world {
public static void main(String[] args) throws IOException, TimeoutException {
//1,创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//2,设置参数(host,port,virtual host,username,guest)
factory.setHost("10.211.55.7");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
//3,创建连接
Connection connection = factory.newConnection();
//4,创建channel
Channel channel = connection.createChannel();
//5,创建队列Queue
//String queue--队列名称,
// boolean durable--是否持久化,
// boolean exclusive--是否独占(只有一个消费者)当connection关闭时,是否删除队列, boolean autoDelete--是否允许自动删除(没有消费者时),
// Map<String, Object> arguments-- 参数
channel.queueDeclare("hello_world", true, false, false, null);
//6,发送消息
//String exchange--交换机名称,简单模式下,使用默认值 ""空字符串
// String routingKey--路由名称,当使用默认的 ""(空字符串)的exchange时,和队列名称保持一致。
// BasicProperties props--属性,
// byte[] body--实际发送消息内容
channel.basicPublish("","hello_world",null,"hello rabbitmq".getBytes());
//7,关闭连接,释放资源
channel.close();
connection.close();
}
}
3,测试
运行之后,打开控制界面
二、consumer
1,代码
package com.wayne;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer_hello_world {
public static void main(String[] args) throws IOException, TimeoutException {
//1,创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//2,设置参数(host,port,virtual host,username,guest)
factory.setHost("10.211.55.7");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
//3,创建连接
Connection connection = factory.newConnection();
//4,创建channel
Channel channel = connection.createChannel();
//5,创建队列Queue
//String queue--队列名称,
// boolean durable--是否持久化,
// boolean exclusive--是否独占(只有一个消费者)当connection关闭时,是否删除队列, boolean autoDelete--是否允许自动删除(没有消费者时),
// Map<String, Object> arguments-- 参数
channel.queueDeclare("hello_world", true, false, false, null);
//6,创建消费者
//String queue--队列名称, boolean autoAck--是否发送自动确认信息, Consumer callback--回调对象)
channel.basicConsume("hello_world",true,new DefaultConsumer(channel){
//糊掉对象中的方法
//String consumerTag, -- 消费者标签
// Envelope envelope, -- 一些信息 例如交换机、路由信息
// AMQP.BasicProperties properties, 配置信息
// byte[] body --实际的消息体
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("consumerTag:"+consumerTag);
System.out.println("Exchange:"+envelope.getExchange());
System.out.println("routerKey:"+envelope.getRoutingKey());
System.out.println("properties:"+properties);
System.out.println("body:"+new String(body));
}
});
}
}
2,测试
控制台: