-
simple模式:最簡單模式
- 消息生產者
public class Producter {
private final static String queue_name="simple";
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 RabbitMQ";
channel.basicPublish("",queue_name,null,msg.getBytes());
channel.close();
connection.close();
}
}
- 消息接收者
public class Consumers {
private final static String queue_name = "simple";
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);
Consumer consumer = 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.basicConsume(queue_name, true,consumer);
}
}
-
fanout模式:生產者發送給交換機,交換機發送給所有的隊列,在發送給相應的消費者
- 生產者
public class Producter {
private final static String exchange_name="fanout_exchange";
public static void main(String[] args)throws IOException, TimeoutException {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(exchange_name,"fanout");
String msg = "Hello RabbitMQ";
channel.basicPublish(exchange_name,"",null,msg.getBytes());
channel.close();
connection.close();
}
}
- 消費者1
public class Consumers1 {
private final static String queue_name = "fanout1";
private final static String exchange_name="fanout_exchange";
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);
channel.exchangeDeclare(exchange_name,"fanout");
channel.queueBind(queue_name,exchange_name,"");
Consumer consumer = 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.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(queue_name, false,consumer);
}
}
- 消費者2
public class Consumers2 {
private final static String queue_name = "fanout2";
private final static String exchange_name="fanout_exchange";
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);
channel.exchangeDeclare(exchange_name,"fanout");
channel.queueBind(queue_name,exchange_name,"");
Consumer consumer = 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.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(queue_name, false,consumer);
}
}
-
direct模式:只有路由鍵完全匹配纔會被路由到相應的隊列
- 生產者
public class Producter {
private final static String exchange_name="direct_exchange";
public static void main(String[] args)throws IOException, TimeoutException {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(exchange_name,"direct");
String msg = "Hello RabbitMQ";
channel.basicPublish(exchange_name,"info",null,msg.getBytes());
channel.basicPublish(exchange_name,"warning",null,msg.getBytes());
channel.basicPublish(exchange_name,"error",null,msg.getBytes());
channel.close();
connection.close();
}
}
- 消費者1
public class Consumers1 {
private final static String queue_name = "direct1";
private final static String exchange_name="direct_exchange";
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);
channel.exchangeDeclare(exchange_name,"direct");
channel.queueBind(queue_name,exchange_name,"error");
Consumer consumer = 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.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(queue_name, false,consumer);
}
}
- 消費者2
public class Consumers2 {
private final static String queue_name = "direct2";
private final static String exchange_name="direct_exchange";
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);
channel.exchangeDeclare(exchange_name,"direct");
channel.queueBind(queue_name,exchange_name,"info");
channel.queueBind(queue_name,exchange_name,"warning");
Consumer consumer = 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.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(queue_name, false,consumer);
}
}
-
topic模式:路由鍵相應匹配纔會被路由到相應的隊列
-
-
生產者
public class Producter {
private final static String exchange_name="topic_exchange";
public static void main(String[] args)throws IOException, TimeoutException {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(exchange_name,"topic");
String msg = "Hello RabbitMQ";
channel.basicPublish(exchange_name,"info.info",null,msg.getBytes());
channel.basicPublish(exchange_name,"warning.info",null,msg.getBytes());
channel.basicPublish(exchange_name,"info.error",null,msg.getBytes());
channel.close();
connection.close();
}
}
- 消費者1
public class Consumers1 {
private final static String queue_name = "topic1";
private final static String exchange_name="topic_exchange";
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);
channel.exchangeDeclare(exchange_name,"topic");
channel.queueBind(queue_name,exchange_name,"info.#");
Consumer consumer = 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.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(queue_name, false,consumer);
}
}
- 消費者2
public class Consumers2 {
private final static String queue_name = "topic2";
private final static String exchange_name="topic_exchange";
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);
channel.exchangeDeclare(exchange_name,"topic");
channel.queueBind(queue_name,exchange_name,"#.info");
Consumer consumer = 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.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(queue_name, false,consumer);
}
}