pom.xml
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.0.2</version>
</dependency>
directExchange 生产者
directExchange Product生产者的类
package com.qbsea.rabbitmq.product.directexchange;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class ProducerApp {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = null;
Channel channel = null;
try{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("sunlei");
factory.setPassword("sunlei");
factory.setVirtualHost("/vhost_sunlei");
//创建与RabbitMQ服务器的TCP连接
connection = factory.newConnection();
channel = connection.createChannel();
/**
*创建名为directExchange的Direct Exchange
*Exchange类型 direct , fanout, topic, topic
*/
channel.exchangeDeclare("directExchange", "direct");
/**
* 参数1 firstQueue 队列的名字
* 参数2 true durable表示建立的消息队列是否是持久化true为持久化 false为非持久化 durable单词的意思是耐用的持久的
* 参数3 false exclusive表示是否只适用于TCP连接,false表示不是只适用于TCP exclusive独有的排外的专一的
* 参数4 false autoDelete表示队列不再被使用时,rabbit会自动删除该队列
* 参数5 arguments 定义了队列一些参数信息
*/
channel.queueDeclare("directQueue", true, false, false, null);
/**
* 参数1 queue 在这里queue=directQueue 消息队列的名称
* 参数2 exchange 在这里exchange的名字=directExchange Exchange的名称
* 参数3 routingKey 在这里routingKey=directMessage 是指消息队列与Exchange之间绑定的routingKey
* 即只有routingKey=directMessage 的消息会被转发到消息队列的directQueue,其它消息不会被转
*/
channel.queueBind("directQueue", "directExchange", "directMessage");
String message = "First Direct Message";
//非持久化消息 props=null;
/**
* 参数1 exchange 在这里 exchange=directExchange
* 参数2 routingKey 在这里routingKey=directMessage
* 参数3 props 持久化
* 参数4 body 消息体
*/
channel.basicPublish("directExchange", "directMessage", null, message.getBytes());
System.out.println("Send Direct Message is:'" + message + "'");
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(channel != null){
channel.close();
}
if(connection != null){
connection.close();
}
}
}
}
消费者的类
package com.qbsea.modules.rabbitmqconsumer;
import java.io.IOException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class DirectExchangeConsumer {
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("admin");
factory.setVirtualHost("/vhost_sunlei");
connection = factory.newConnection();
channel = connection.createChannel();
Consumer consumer = new DefaultConsumer(channel) {
/**
* 参数1 consumerTag 接收到消息时的消费者Tag 如果没指定默认随机生成
* 参数2 envelope 消息打包信息 包含四个属性_deliveryTag 消息发送编号,这条消息发送的第几条消息
* _redeliver重传标志 执行失败后是否重发 fasle为不重发
* _exchange消息发送到的Exchange名称 当exchange名称为空使用的是Default Exchange
* _routingKey发送的路由Key这里设置的“firstQueue”
* 参数3 properties
* 参数4 body 消息体
* */
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" Consumer have received '" + message + "'");
}
};
/**
* String queue, boolean autoAck, Consumer callback
* 参数1 queue 绑定的队列的名称
* 参数2 autoAck 自动确认参数,true表示接收到消息后会自动发送确认消息 然后消息队列会把这台消息删除
* 参数3 callback consumer对象
* */
channel.basicConsume("directQueue", true, consumer);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}