RabbitMQ中的角色
Producer:Producing means nothing more than sending. A program that sends messages is a producer :
queue:A queue is the name for a post box which lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host’s memory & disk limits, it’s essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue:
Consumer:Consuming* has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages:
HelloWorld模型
这种模型只有一个生产者,一个消费者,一个队列。生产者向"hello"队列发送消息,消费者从该队列接收消息。
1.开发生产者
public void provider() throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
//设置连接rabbitmq主机地址
connectionFactory.setHost("192.168.1.18");
//设置连接虚拟机
connectionFactory.setVirtualHost("/ems");
//设置连接的用户
connectionFactory.setUsername("ems");
//设置连接的用户的密码
connectionFactory.setPassword("123");
//设置连接的端口号
connectionFactory.setPort(5672);
Connection connection = connectionFactory.newConnection();
//创建通道
Channel channel = connection.createChannel();
//参数一:队列名称,如果不存在则自动创建
//参数二:用来定义队列是否持久化
//参数三:是否是独占队列
//参数四:是否在消费完成后自动删除队列
//参数五:额外的附加参数
channel.queueDeclare("hello",false,false,false,null);
//参数一:交换机名称
//参数二:队列名称
//参数三:传递消息额外设置
//参数四:消息的具体内容
channel.basicPublish("","hello",null,"hello rabbitmq".getBytes());
channel.close();
connection.close();
}
2.开发消费者
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.1.18");
connectionFactory.setVirtualHost("/ems");
connectionFactory.setPort(5672);
connectionFactory.setUsername("ems");
connectionFactory.setPassword("123");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
//参数一: 队列名称
//参数二: 是否开启自动确认
//参数三: 消费者 DefaultConsumer建立使用,重写其中的方法
channel.basicConsume("hello", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("=====" + new String(body));
}
});
//channel.close();
//connection.close();
}
3.RabbitMQ的工具类封装
package util;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Created with IntelliJ IDEA
* Description:
* Author: sudi
* Date: 2020/12/28
* TIME: 18:40
*/
public class RabbitMQUtils {
private static ConnectionFactory connectionFactory;
static {
connectionFactory = new ConnectionFactory();
}
public static Connection getConnection(String Host,String VirtualHost,int Port,String Username,String Password) throws IOException, TimeoutException {
connectionFactory.setHost(Host);
connectionFactory.setVirtualHost(VirtualHost);
connectionFactory.setPort(Port);
connectionFactory.setUsername(Username);
connectionFactory.setPassword(Password);
return connectionFactory.newConnection();
}
public static void closeConnectionAndChannel(Channel channel, Connection connection) {
try {
if (channel!= null) channel.close();
if(connection!= null) connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}