一.导入包
implementation ‘com.rabbitmq:amqp-client:4.2.0’
二.项目使用
1.设置工程连接
factory = new ConnectionFactory();
factory.setHost(hostname);
factory.setPort(port);
factory.setUsername(MQConfig.username);
factory.setVirtualHost("/");
factory.setPassword(MQConfig.password);
factory.setConnectionTimeout(10000); //连接时间设置为10秒
factory.setAutomaticRecoveryEnabled(true); //恢复连接,通道
factory.setTopologyRecoveryEnabled(true); //恢复通道中 转换器,队列,绑定关系等
factory.setNetworkRecoveryInterval(10000); //恢复连接间隔,默认5秒
factory.setRequestedHeartbeat(10);
2.监听消息与发送消息
定义线程池
ExecutorService executor = Executors.newFixedThreadPool(2);
监听队列消息
executor.execute(new Runnable() {
@Override
public void run() {
if (instance != null) {
receiveMessageByQueue(queueName);
}
}
});
public void receiveMessageByQueue(final String queueName) {
try {
if (connection == null || !connection.isOpen()) {
connection = factory.newConnection();
}
if (!channelMap.containsKey(FLAG_RECEIVE + queueName)) {
final Channel channel = connection.createChannel();
channel.queueDeclare(queueName, true, false, false, null);
channel.basicQos(100);
channel.basicConsume(queueName, false, 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");
if (mqMessageReceiver != null) {
mqMessageReceiver.onMQMessage(queueName, message, channel, envelope);
}
}
});
channelMap.put(FLAG_RECEIVE + queueName, channel);
}
} catch (IOException | TimeoutException | AlreadyClosedException e) {
SystemClock.sleep(5000);
receiveMessageByQueue(queueName);
}
}
发送消息
executor.execute(new Runnable() {
@Override
public void run() {
try {
if (connection == null || !connection.isOpen()) {
connection = factory.newConnection();
}
Channel channel;
if (channelMap.containsKey(FLAG_SEND + queueName)) {
channel = channelMap.get(FLAG_SEND + queueName);
} else {
channel = connection.createChannel();
channel.queueDeclare(queueName, true, false, false, null);
channelMap.put(FLAG_SEND + queueName, channel);
}
channel.confirmSelect();
channel.basicPublish("", queueName, null, message.getBytes());
boolean success = channel.waitForConfirms();
if (success) {
if (listener != null) {
listener.sendMessage(true);
}
} else {
if (listener != null) {
listener.sendMessage(false);
}
}
} catch (IOException | InterruptedException | TimeoutException e) {
e.printStackTrace();
if (listener != null) {
listener.sendMessage(false);
}
}
}
});
关闭资源
public void close() {
instance = null;
if(executor!=null){
executor.shutdown();
}
if(factory!=null){
factory=null;
}
if (channelMap != null && channelMap.size() > 0) {
for (Channel channel : channelMap.values()) {
if (channel != null && channel.isOpen()) {
try {
Connection connection = channel.getConnection();
if(channel!=null&&channel.isOpen()){
channel.close();
}
if(connection!=null&&connection.isOpen()){
connection.close();
}
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}catch (Exception e1){
e1.printStackTrace();
}
}
}
channelMap.clear();
}
if (connection != null && connection.isOpen()) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}