Java实现RabbitMQ客户端和消费者端的简单实例

1、定义

RabbitMQ是流行的开源消息队列系统,用erlang语言开发。RabbitMQ是AMQP(高级消息队列协议)的标准实现。几个概念说明:

(1)Broker:简单来说就是消息队列服务器实体。

(2)Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。

(3)Queue:消息队列载体,每个消息都会被投入到一个或多个队列。

(4)Binding:绑定,它的作用就是把exchange和queue按照路由规则绑定起来。

(5)Routing Key:路由关键字,exchange根据这个关键字进行消息投递。

(6)vhost:虚拟主机,一个broker里可以开设多个vhost,用作不同用户的权限分离。

(7)producer:消息生产者,就是投递消息的程序。

(8)consumer:消息消费者,就是接受消息的程序。

(9)channel:消息通道,在客户端的每个连接里,可建立多个channel,每个channel代表一个会话任务。

2、简单实例

(1)目录结构

Java实现RabbitMQ客户端和消费者端的简单实例_第1张图片

(2)pom.xml


  4.0.0

  cn.hwd
  rabbitmq
  0.0.1-SNAPSHOT
  jar

  rabbitmq
  http://maven.apache.org

  
    UTF-8
  

  
    
		com.rabbitmq
		amqp-client
		3.6.5
 	
  	
  		junit
  		junit
  		4.8.1
  	
  

(3)RabbitmqUtil.java

package cn.hwd.rabbitmq.util;

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

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class RabbitmqUtil {

	private ConnectionFactory factory = null;
	private Connection connection = null;
	private Channel channel = null;
	private String host = "localhost";
	private String username = "guest";
	private String password = "guest";
	private Integer port = 5672;
	private String queueName = "MyQueue";
	private String exchangeName = "amq.direct";
	
	public void send(String message) {
		try {
			// 创建工厂
			factory = new ConnectionFactory();
			// 设置
			factory.setHost(host);
			factory.setUsername(username);
		    factory.setPassword(password);
		    factory.setPort(port);
		    // 创建连接
			connection = factory.newConnection();
			// 创建通道
			channel = connection.createChannel();
			// 声明队列
			channel.queueDeclare(queueName, false, false, false, null);
			// 发送消息到队列之中
			channel.basicPublish(exchangeName, queueName, null, message.getBytes("UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭通道
				channel.close();
				// 关闭连接
				connection.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TimeoutException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void receive() {
		try {
			factory = new ConnectionFactory();
			factory.setHost(host);
			factory.setUsername(username);
		    factory.setPassword(password);
		    factory.setPort(port);
			connection = factory.newConnection();
			channel = connection.createChannel();
			channel.queueDeclare(queueName, false, false, false, null);
			// 创建队列消费者
			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("receive:" + message);
	            }
			};
			// 消息确认机制
			channel.basicConsume(queueName, true, consumer);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
	}
	
}

(4)RabbitmqMain.java

package cn.hwd.rabbitmq.main;

import cn.hwd.rabbitmq.util.RabbitmqUtil;

public class RabbitmqMain {

	public static void main(String[] args) {
		RabbitmqUtil rabbitmqUtil = new RabbitmqUtil();
		rabbitmqUtil.send("Hello RabbitMQ");
		rabbitmqUtil.receive();
	}

}

3、运行结果

receive:Hello RabbitMQ

你可能感兴趣的:(RabbitMQ)