rabbitmq学习记录(二)基本队列

本文介绍了如何使用RabbitMQ实现简单的消息传递。通过创建生产者和消费者应用,演示了消息从发送到接收的全过程。首先,生产者将消息发送到RabbitMQ队列中;然后,消费者从队列中接收消息并进行处理。

基本队列:Producer直接发送信息到Queue中,Consumer接收Queue发送过来的信息

简而言之,一个生产者发送信息,一个消费者接收信息。

32274ae7a85095d26d45016762d5bc3f2ac.jpg

获取连接工具类:

package com.example.demo.utils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class ConnectionUtil {

	public static Connection getConnection() throws IOException, TimeoutException {
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("127.0.0.1");
		connectionFactory.setPort(5672);
//		connectionFactory.setVirtualHost("");
		connectionFactory.setUsername("guest");
		connectionFactory.setPassword("guest");
		return connectionFactory.newConnection();
	}
	
}

生产者:

package com.example.demo.queue.simple;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Producer {

	// 队列名称
	private static final String QUEUE_NAME="simple_queue";
	
	public static void main(String[] args) {
		Connection connection = null;
		Channel channel = null;
		try {
			// 获取连接
			connection = ConnectionUtil.getConnection();
			// 创建通道
			channel = connection.createChannel();
			// 声明队列
			channel.queueDeclare(QUEUE_NAME, false, false, false, null);
			// 生产者发送的信息
			String sendMsg = "msg from producer";
			// 发送信息
			channel.basicPublish("", QUEUE_NAME, null, sendMsg.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭通道
			try {
				channel.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TimeoutException e) {
				e.printStackTrace();
			}
			// 关闭连接
			try {
				connection.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
}

消费者:

package com.example.demo.queue.simple;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Consumer {

	// 队列名称
	private static final String QUEUE_NAME="simple_queue";
	
	public static void main(String[] args) {
		Connection connection = null;
		Channel channel = null;
		try {
			// 获取连接
			connection = ConnectionUtil.getConnection();
			// 创建通道
			channel = connection.createChannel();
			// 声明队列
			channel.queueDeclare(QUEUE_NAME, false, false, false, null);
			// 定义消费者
			DefaultConsumer consumer = new DefaultConsumer(channel) {

				@Override
				public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
					String msg = new String(body,"UTF-8");
					System.out.println("receive msg:"+msg);
				}
				
			};
			// 接收信息
			channel.basicConsume(QUEUE_NAME, true, consumer);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				channel.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TimeoutException e) {
				e.printStackTrace();
			}
			try {
				connection.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}

执行方法:右键->Run as->Java Application

执行顺序:Producer.main()->Consumer.main()

 

执行Producer的main方法之后,打开rabbitmq的管理界面,切换到Queues

e0d1912fc1ea9eb192e5983a7baa9bb4bce.jpg

图中圈出的队列,说明,生产者(Producer)已经成功的发送信息到rabbitmq的队列之中了。

接下来,我们执行消费者(Consumer)的main方法,如果想看到消费者收到信息,可以把消费者类中的通道,连接关闭代码先注释掉,即可看到收到的信息。

abecee8885e44cb780ad535c4a0abfe8766.jpg

 

转载于:https://my.oschina.net/u/3229807/blog/1860285

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值