资源简介

spring集成rabbitMq(基于direct、topic和fanout模式),包括main方法,5种情景,一天总结运行

资源截图

代码片段和文件信息

package com.exchange.direct;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

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 ReceiveLogsDirect1 {
// 交换器名称
private static final String EXCHANGE_NAME = “direct_logs“;
// 路由关键字
private static final String[] routingKeys = new String[] { “info“
“warning“ };

public static void main(String[] args) throws IOException TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(“localhost“);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明交换器
channel.exchangeDeclare(EXCHANGE_NAME “direct“);
// 获取匿名队列名称
String queueName = channel.queueDeclare().getQueue();

// 根据路由关键字进行绑定
for (String routingKey : routingKeys) {
channel.queueBind(queueName EXCHANGE_NAME routingKey);
System.out.println(“ReceiveLogsDirect1 exchange:“ + EXCHANGE_NAME
+ ““ + “ queue:“ + queueName + “ BindRoutingKey:“
+ routingKey);
}
System.out.println(“ReceiveLogsDirect1  Waiting for messages“);
Consumer consumer = 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(“ReceiveLogsDirect1 Received ‘“
+ envelope.getRoutingKey() + “‘:‘“ + message + “‘“);
}
};
channel.basicConsume(queueName true consumer);
}
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-11-14 21:20  testRabbitMQ\
     文件        1069  2017-11-14 22:06  testRabbitMQ\.classpath
     文件         772  2017-11-15 08:51  testRabbitMQ\.project
     目录           0  2017-11-14 21:20  testRabbitMQ\.settings\
     文件         119  2017-11-15 08:51  testRabbitMQ\.settings\org.eclipse.core.resources.prefs
     文件         243  2017-11-14 21:20  testRabbitMQ\.settings\org.eclipse.jdt.core.prefs
     文件          90  2017-11-14 21:20  testRabbitMQ\.settings\org.eclipse.m2e.core.prefs
     目录           0  2017-11-15 11:23  testRabbitMQ\config\
     目录           0  2017-11-14 15:42  testRabbitMQ\config\direct\
     文件         991  2017-11-15 11:05  testRabbitMQ\config\direct\common.xml
     文件        1667  2017-11-15 11:18  testRabbitMQ\config\direct\customer.xml
     文件        1712  2017-11-15 11:13  testRabbitMQ\config\direct\producer.xml
     目录           0  2017-11-15 11:23  testRabbitMQ\config\error\
     文件        1503  2017-11-14 15:35  testRabbitMQ\config\error\common.xml
     文件        1212  2017-11-14 15:20  testRabbitMQ\config\error\customer.xml
     文件         889  2017-11-15 09:40  testRabbitMQ\config\error\producer.xml
     目录           0  2017-11-14 16:13  testRabbitMQ\config\fanout\
     文件         991  2017-11-15 09:20  testRabbitMQ\config\fanout\common.xml
     文件        1564  2017-11-15 09:31  testRabbitMQ\config\fanout\customer.xml
     文件        1134  2017-11-15 09:47  testRabbitMQ\config\fanout\producer.xml
     目录           0  2017-11-14 16:32  testRabbitMQ\config\topic\
     文件        1151  2017-11-15 09:07  testRabbitMQ\config\topic\common.xml
     文件        2284  2017-11-15 09:07  testRabbitMQ\config\topic\customer.xml
     文件        2184  2017-11-15 09:08  testRabbitMQ\config\topic\producer.xml
     文件        3056  2017-11-14 15:38  testRabbitMQ\pom.xml
     目录           0  2017-11-14 09:17  testRabbitMQ\src\
     目录           0  2017-11-14 09:17  testRabbitMQ\src\main\
     目录           0  2017-11-14 13:37  testRabbitMQ\src\main\java\
     目录           0  2017-11-14 17:24  testRabbitMQ\src\main\java\com\
     目录           0  2017-11-14 15:59  testRabbitMQ\src\main\java\com\exchange\
     目录           0  2017-11-14 14:12  testRabbitMQ\src\main\java\com\exchange\direct\
............此处省略117个文件信息

评论

共有 条评论