话不多说,直接上代码了
**
* 创建一个生产者
*/
public class Producer {
public static void main(String[] args) throws IOException, TimeoutException {
//创建于服务器的物理连接
/*
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
//选择一个厂库地址
connectionFactory.setVirtualHost("/test");
*/
//tcp 物理连接
Connection connection = RabbitUtils.getConnection(); //使用工具类惊醒连接
//创建一个通信通道
Channel channel = connection.createChannel();
//在通道上创建一个队列,声明并创建一个队列,如果队列已经存在则使用这个队列
//第一个参数为队列名称id
//第二个参数为是否持久化,false对应不持久化数据,MQ停掉数据就会丢失
//第三个参数:队列是否私有化,false则代表所有消费者都可以访问,true代表之后第一次拥有他的消费者才能一直使用,其他消费者不可使用
//第四个参数:是否自动删除,false代表连接停掉后不自动删除掉这个队列
//第五个参数:其他额外的参数,null
channel.queueDeclare("helloworld",false,false,false,null);
//发送信息
String message = "helloworld- wxt";
//四个参数
//1:exchange 交换机,暂时用不到,在后面的进行发布订阅时才会用到
//2:队列名称
//3:额外的设置属性
//4: 需要传递的消息字节数
channel.basicPublish("","helloworld",null,message.getBytes());
//关闭通道
channel.close();
//关闭连接
connection.close();
System.out.println("消息发送成功");
}
}
/**
* 创建一个消费者
*/
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
//常见连接
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setVirtualHost("/test"); //设置数据仓库
Connection connection = connectionFactory.newConnection();
//声明信道
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("helloworld",false,false,false,null);
//创建一个消息消费者
channel.basicConsume("helloworld",false,new Reciver(channel));
//此处不用关闭
}
}
/**
* 消费者的实现类
*/
public class Reciver extends DefaultConsumer {
private Channel channel;
/**
* 重写构造函数
* @param channel
*/
public Reciver(Channel channel) {
super(channel);
this.channel = channel;
}
/**
* 对消息进行处理
* @param consumerTag
* @param envelope
* @param properties
* @param body
* @throws IOException
*/
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//super.handleDelivery(consumerTag, envelope, properties, body);
//手动创建
String retMessage = new String(body);
//输出信息
System.out.println("接收到的信息:"+retMessage);
//签收信息,确认消息
//envelope.getDeliveryTag() 获取这个消息的TagId
//false 之确认签收当前的消息,设置为true的时候则代表签收改消费者所有未签收的消息。
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
public class RabbitUtils {
//创建于服务器的物理连接
private static ConnectionFactory connectionFactory = new ConnectionFactory();
static {
//初始化参数
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setVirtualHost("/test"); //选择一个厂库地址
}
public static Connection getConnection(){
try {
return connectionFactory.newConnection();
} catch (Exception e) {
throw new RuntimeException(e+"rabbitMq 创建连接失败");
}
}
}
代码就是上面的,rabbitmq 环境搭建好以后就可以写代码使用了。
本文详细介绍了如何使用RabbitMQ消息队列进行消息生产和消费。通过代码示例,展示了如何创建生产者和消费者,以及如何在两者之间传递消息。深入探讨了队列声明、消息持久化和消息确认等关键概念。
920

被折叠的 条评论
为什么被折叠?



