RabbitMq发送接收消息方式
RabbitMq不使用交换机
发送方
public static void main(String[] args) {
String messageData = "test message, hello!";
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
/**
* 参数1:队列名
* 参数2:持久化
* 参数3:是否排外,排外只允许一个消费者监听
* 参数4:是否自动删除,队列中没有消息并且没有消费者连接时会自动删除
* 参数4:为队列设置属性,通常为null
*/
channel.queueDeclare("myqueue",true,false,false,null);
/**
* 参数1:交换机名
* 参数2:队列名或者routingkey,指定了交换机这里就是routingkey
* 参数3:设置消息属性,通常为null
* 参数4:消息数据
*/
channel.basicPublish("","myqueue",null,messageData.getBytes());
System.out.println("消息发送成功");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
if(channel!=null){
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
接收方
public static void main(String[] args) {
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare("myqueue",true,false,false,null);
/**
* 参数1 监听队列名
* 参数2 自动确认
* 参数3 接受者标签
* 参数4 消息接收回调方法
*/
channel.basicConsume("myqueue",true,"",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");
System.out.println(message);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
}
}
RabbitMq-direct
发送方
public static void main(String[] args) {
String messageData = "direct message, hello!";
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
/**
* 队列
* 参数1:队列名
* 参数2:持久化
* 参数3:是否排外,排外只允许一个消费者监听
* 参数4:是否自动删除,队列中没有消息并且没有消费者连接时会自动删除
* 参数4:为队列设置属性,通常为null
*/
channel.queueDeclare("myqueue001",true,false,false,null);
/**
* 声明一个交换机
* 参数1交换机名称
* 参数2交换机类型
* 参数3是否持久化
*/
channel.exchangeDeclare("exchange001","direct",true);
/**
* 将队列绑定到交换机
* 参数1队列名称
* 参数2交换机名称
* 参数3routingkey(Bindingkey)
*/
channel.queueBind("myqueue001","exchange001","routingkey001");
/**
* 参数1:交换机名
* 参数2:routingkey
* 参数3:设置消息属性,通常为null
* 参数4:消息数据
*/
channel.basicPublish("exchange001","routingkey001",null,messageData.getBytes());
System.out.println("消息发送成功");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
if(channel!=null){
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
接收方
public static void main(String[] args) {
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare("myqueue001",true,false,false,null);
channel.exchangeDeclare("exchange001","direct",true);
channel.queueBind("myqueue001","exchange001","routingkey001");
/**
* 参数1 监听队列名
* 参数2 自动确认
* 参数3 接受者标签
* 参数4 消息接收回调方法
*/
channel.basicConsume("myqueue001",true,"",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");
System.out.println(message);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
}
}
RabbitMq-fanout
发送方
public static void main(String[] args) {
String messageData = "fanout message, hello!";
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare("exchange002","fanout",true);
/**
* 参数1:交换机名
* 参数2:routingkey
* 参数3:设置消息属性,通常为null
* 参数4:消息数据
*/
channel.basicPublish("exchange002","",null,messageData.getBytes());
System.out.println("消息发送成功");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
if(channel!=null){
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
接收方01
public static void main(String[] args) {
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
//创建随机队列
String queue = channel.queueDeclare().getQueue();
channel.exchangeDeclare("exchange002","fanout",true);
channel.queueBind(queue,"exchange002","");
/**
* 参数1 监听队列名
* 参数2 自动确认
* 参数3 接受者标签
* 参数4 消息接收回调方法
*/
channel.basicConsume(queue,true,"",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");
System.out.println(“消费者01”+message);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
}
}
接收方02
public static void main(String[] args) {
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
//创建随机队列
String queue = channel.queueDeclare().getQueue();
channel.exchangeDeclare("exchange002","fanout",true);
channel.queueBind(queue,"exchange002","");
/**
* 参数1 监听队列名
* 参数2 自动确认
* 参数3 接受者标签
* 参数4 消息接收回调方法
*/
channel.basicConsume(queue,true,"",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");
System.out.println(“消费者02”+message);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
}
}
RabbitMq-topic
消费者001
public static void main(String[] args) {
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare("topicmyqueue001",true,false,false,null);
channel.exchangeDeclare("topicexchange001","topic",true);
channel.queueBind("topicmyqueue001","topicexchange001","aa");
/**
* 参数1 监听队列名
* 参数2 自动确认
* 参数3 接受者标签
* 参数4 消息接收回调方法
*/
channel.basicConsume("topicmyqueue001",true,"",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");
System.out.println("消费者001"+message);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
}
}
消费者002
public static void main(String[] args) {
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare("topicmyqueue002",true,false,false,null);
channel.exchangeDeclare("topicexchange001","topic",true);
channel.queueBind("topicmyqueue002","topicexchange001","aa.*");
/**
* 参数1 监听队列名
* 参数2 自动确认
* 参数3 接受者标签
* 参数4 消息接收回调方法
*/
channel.basicConsume("topicmyqueue002",true,"",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");
System.out.println("消费者002"+message);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
}
}
消费者003
public static void main(String[] args) {
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare("topicmyqueue003",true,false,false,null);
channel.exchangeDeclare("topicexchange001","topic",true);
channel.queueBind("topicmyqueue003","topicexchange001","aa.#");
/**
* 参数1 监听队列名
* 参数2 自动确认
* 参数3 接受者标签
* 参数4 消息接收回调方法
*/
channel.basicConsume("topicmyqueue003",true,"",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");
System.out.println("消费者003"+message);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
}
}
发送者
public static void main(String[] args) {
String messageData = "topic message, hello!";
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("47.110.157.82");
factory.setPort(5672);
factory.setUsername("liu");
factory.setPassword("123456");
Connection connection=null;
Channel channel=null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare("topicexchange001","topic",true);
/**
* 参数1:交换机名
* 参数2:routingkey
* 参数3:设置消息属性,通常为null
* 参数4:消息数据
*/
//消费者001,消费者003可以接收到消息
// channel.basicPublish("topicexchange001","aa",null,messageData.getBytes());
//消费者002,消费者003可以接收到消息
// channel.basicPublish("topicexchange001","aa.bb",null,messageData.getBytes());
//消费者003可以接收到消息
channel.basicPublish("topicexchange001","aa.bb.cc",null,messageData.getBytes());
System.out.println("消息发送成功");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
if(channel!=null){
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}