1. What is RABBITMQ
MQ (Message Queue): A queue of messages that is designed by the server to store a large number of messages and provides a way for the client to operate the queue: the production queue (adding data to the queue), the consumption queue (fetching data from the queue). RABBITMQ is a typical application based on Message Queuing. RABBITMQ In addition to the normal production and consumption functions, there are some advanced features: Fair distribution, polling distribution, routing mode, wildcard mode, publish subscription, queue persistence.
2, Java implementation of RABBITMQ connection
2.1. RABBITMQ Client Jar Package
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.0.2</version>
</dependency>
2.2. Java Connection RABBITMQ Tool class
Copy Code
public class Connectionutil
{
private static Logger Logger = Logger.getlogger (Connectionutil.class);
public static Connection getConnection(){ try { Connection connection = null; //定义一个连接工厂 ConnectionFactory factory = new ConnectionFactory(); //设置服务端地址(域名地址/ip) factory.setHost("127.0.0.1"); //设置服务器端口号 factory.setPort(5672); //设置虚拟主机(相当于数据库中的库) factory.setVirtualHost("/"); //设置用户名 factory.setUsername("admin"); //设置密码 factory.setPassword("888888"); connection = factory.newConnection(); return connection; } catch (Exception e) { return null; }}
}
Copy Code
2.3. Simple producer-consumer model
Work map taken from the production and consumption model of the official website (RabbitMQ)
P: Producer of messages
C: Consumer of the message
Red: Queue
The producer sends the message to the queue and the consumer gets the message from the queue.
2.4. Producer (Send)
Copy Code
public class Send
{
Queue name
private static final String queue_name = "Test_simple_queue";
public static void main(String[] args){ try { //获取连接 Connection connection = ConnectionUtil.getConnection(); //从连接中获取一个通道 Channel channel = connection.createChannel(); //声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "This is simple queue"; //发送消息 channel.basicPublish("", QUEUE_NAME, null, message.getBytes("utf-8")); System.out.println("[send]:" + message); channel.close(); connection.close(); } catch (IOException | TimeoutException e) { e.printStackTrace(); }}
}
Operation Result:
[Send]:this is simple queue
Copy Code
2.5. Consumer (Receive)
Copy Code
public class Receive
{
Queue name
private static final String queue_name = "Test_simple_queue";
public static void main(String[] args){ try { //获取连接 Connection connection = ConnectionUtil.getConnection(); //从连接中获取一个通道 Channel 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 message = new String(body, "utf-8"); System.out.println("[Receive]:" + message); } }; //监听队列 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (IOException | ShutdownSignalException | ConsumerCancelledException e) { e.printStackTrace(); }}
}
Operation Result:
[Receive]:this is simple queue
Copy Code
Summary :
The simple producer-consumer model enables producers to produce data in the queue, and the consumer can listen to the queue continuously and fetch data from the queue.
RABBITMQ Study First: Connect with Java RABBITMQ