1、连接RabbitMQ
关于RabbitMQ的安装,提供一个链接:https://blog.csdn.net/yin767833376/article/details/81223491
创建maven工程,在pom文件中导入
<!-- https : //mvnrepository.com/artifact/com. rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.1</version>
</dependency>
编写一个工具类,用于获取conection
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class ConnectionUtils
{
private static final String USERNAME = "xxxx";
private static final String PASSWORD = "xxxx";
private static final String IP_ADDRESS = "xx.xxx.xx.xx";
private static final int PORT = 5672;
public static Connection getConnection() throws IOException, TimeoutException
{
// 定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 设置服务器地址
factory.setHost(IP_ADDRESS);
// 设置端口
factory.setPort(PORT);
// 设置用户名和密码
factory.setUsername(USERNAME);
factory.setPassword(PASSWORD);
// 创建一个连接
return factory.newConnection();
}
}
注意:Channel Connection中有isOpen方法用于检测其是否处于开启状态。一般不要使用。其要依赖于shutdownCause
2、简单的队列模型
RabbitMQ官网列出了几种常见的模型:https://www.rabbitmq.com/getstarted.html
接下来根据第一种简单模型来写一下:
消息队列就是生产者与消费者模型,本例中只有一个生产者和一个消费者,以及一个队列。
首先构建一个生产者
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class RabbitProducer
{
private static final String QUEUE_NAME = "test_simple_queue";
public static void main(String[] args) throws IOException, TimeoutException
{
Connection connection = ConnectionUtils.getConnection();
// 从连接中获取一个通道
Channel channel = connection.createChannel();
// 创建队列声明
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 发送消息
String message = "hello world";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println("-----send message----");
System.out.println("message");
// 释放资源
channel.close();
connection.close();
}
}
然后构建一个消费者
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class RabbitConsumer
{
private static final String QUEUE_NAME = "test_simple_queue";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException
{
Connection connection = ConnectionUtils.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 envelop, AMQP.BasicProperties properties, byte[] body) throws IOException
{
System.out.println("receive message is : " + new String(body));
}
};
channel.basicConsume(QUEUE_NAME, consumer);
}
}
然后先启动消费者,接着运行生产者。
3、方法详解
①、queueDeclare方法详解
queueDeclare表示声明一个队列,该队列用于存储生产者生产的消息,然后将其发给消费者。其重载方法有两个
com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclare() throws IOException; com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) throws IOException;
不带任何参数的方法默认会创建一个:有RabbitMQ命名的、排他的(同一个连接下才可以访问)、自动删除的、非持久化队列。
参数详解 queue:队列名 durable:是否持久化,为ture表示队列为持久化,可以存盘 exclusive:是否排他,为ture表示排他,同一个连接下的channel可以访问该队列 autoDelete:是否自动删除,前提是:至少有一个消费者连接到该队列,然后所有与这个队列连接的消费者都断 开连接,该队列自动删除。 arguments:一些其他的参数
生产者和消费者都可以声明队列。但如果消费者在同一个channel上订阅了其他队列就无法再声明队了,必须先取消订阅。
②、queueDeclarePassive方法
com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclarePassive(String queue) throws IOException;
该方法用于检测相应的队列是否存在,不存在是会抛出异常。
③、queueDelete方法
com.rabbitmq.client.AMQP.Queue.DeleteOk queueDelete(String queue) throws IOException; com.rabbitmq.client.AMQP.Queue.DeleteOk queueDelete(String queue, boolean isUnused, boolean ifEmpty) throws IOException;
ifUnused:如果为true表示该队列在没有被使用的情况下才会被删除,如果为false无论如何都要被删除。
ifEmpty:为true表示在队列为空的时候才会被删除。
④、queuePurge方法
PurgeOk queuePurge(String queue) throws IOException;
表示情况队列中的内容,不删除队列本身。
关于其他方法将在后面介绍