1、Ubuntu安装RabbitMQ与RabbitMQ简单队列使用
#!/bin/sh ## If sudo is not available on the system, ## uncomment the line below to install it # apt-get install -y sudo sudo apt-get update -y ## Install prerequisites sudo apt-get install curl gnupg -y ## Install RabbitMQ signing key curl -fsSL https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc | sudo apt-key add - ## Install apt HTTPS transport sudo apt-get install apt-transport-https ## Add Bintray repositories that provision latest RabbitMQ and Erlang 21.x releases sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list <<EOF ## Installs the latest Erlang 22.x release. ## Change component to "erlang-21.x" to install the latest 21.x version. ## "bionic" as distribution name should work for any later Ubuntu or Debian release. ## See the release to distribution mapping table in RabbitMQ doc guides to learn more. deb https://dl.bintray.com/rabbitmq-erlang/debian bionic erlang deb https://dl.bintray.com/rabbitmq/debian bionic main EOF ## Update package indices sudo apt-get update -y ## Install rabbitmq-server and its dependencies sudo apt-get install rabbitmq-server -y --fix-missing
2、RabbitMQ简单队列使用
package com.maaya.rabbitmq.util;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
// 创建连接的工具类
public class ConnectionUtil {
public static Connection getConnection() {
ConnectionFactory factory = new ConnectionFactory();
// RabbitMQ的地址
factory.setHost("XXXXXXX");
// 端口
factory.setPort(5672);
// VirtualHost
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
try {
return factory.newConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
return null;
}
}
package com.maaya.rabbitmq.simple;
import com.maaya.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
// 发送端/生产者
public class SendMessage {
public static final String QUEUE_NAME = "SIMPLE_QUEUE";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String msg = "Hello Message";
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
System.out.println("发送消息:" + msg);
channel.close();
connection.close();
}
}
package com.maaya.rabbitmq.simple;
import com.maaya.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;
import java.io.IOException;
// 接收者/消费者
public class ReceiveMessage {
public static final String QUEUE_NAME = "SIMPLE_QUEUE";
public static void main(String[] args) throws IOException {
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, AMQP.BasicProperties properties, byte[] body) throws IOException {
super.handleDelivery(consumerTag, envelope, properties, body);
String msg = new String(body, "UTF-8");
System.out.println("接收消息:" + msg);
}
};
channel.basicConsume(QUEUE_NAME,true,consumer);
}
}
2019.11.22