今天接到一个需求,就是在发送消息到rabbitmq消息中心的时候,需要根据设备类型,将消息发送到不同的消息队列,因此要创建不同的消息队列。
修改之前是把配置信息写在配置文中,项目启动时,获取配置文件中的配置信息,创建消息队列。
修改后的逻辑:是先查询数据库,根据数据库中的信息,创建所有的消息队列,并且将channel和对应的serverId作为键值对放入map中。在发送消息的时候,根据serverId从map中取出channel和queuename,将消息发送到消息中心。
1.NbiotRabbitCreator.java,工具类,接受参数,创建消息中心和消息队列
package com.xh.iot.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.stereotype.Component;
@Component
public class NbiotRabbitCreator {
public Channel creatRabbitChannel(String host, int port, String username, String password){
//创建连接和通道
Channel channel = null;
try{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setPort(port);
factory.setUsername(username);
factory.setPassword(password);
Connection connection = factory.newConnection();
channel = connection.createChannel();
}catch (Exception e){
e.printStackTrace();
}
return channel;
}
}
2.NbiotRabbitConfig.java,自动执行,从数据库获取信息,并且传递参数创建消息中心和队列。并将channel和queueName作为value,serverId作为键,存放在map中
package com.xh.iot.rabbitmq;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.xh.iot.repositories.model.Server;
import com.xh.iot.repositories.service.ServerService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class NbiotRabbitConfig implements ApplicationRunner {
public static Map<Integer,RabbitTemplate> rabbitTemplateMap = new HashMap<>();
public static Map<Integer,String> dataQueueNameMap = new HashMap<>();
public static Map<Integer,Channel> channelMap = new HashMap<>();
Integer serverId;
String host = "";
int port = 0;
String username = "";
String password = "";
String dataQueueName = "";
String commandQueueName = "";
String commandExchange = "";
String bindkey = "";
@Autowired
NbiotRabbitCreator nbiotRabbitUtil;
@Autowired
ServerService serverService;
@Override
public void run(ApplicationArguments args) throws Exception {
//获取所有的rabbitmq配置信息
List<Server> rabbitServerList = serverService.getServerList();
//根据查出的配置信息,创建队列并绑定
for(Server server : rabbitServerList){
serverId = server.getId();
host = server.getHost();
port = server.getPort();
username = server.getUsername();
password = server.getPassword();
dataQueueName = server.getDataqueuename();
commandQueueName = server.getCommandqueuename();
commandExchange = server.getCommandexchange();
bindkey = server.getBindkey();
//获得channel
Channel channel = nbiotRabbitUtil.creatRabbitChannel(host,port,username,password);
//声明队列
channel.queueDeclare(dataQueueName, true, false, false, null);
channel.queueDeclare(commandQueueName, true, false, false, null);
//声明路由(String exchange, String type, boolean durable, boolean autoDelete,Map<String, Object> arguments)
channel.exchangeDeclare(commandExchange, BuiltinExchangeType.DIRECT,true,false,null);
//绑定路由和队列(String queue, boolean durable, boolean exclusive, boolean autoDelete,Map<String, Object> arguments)
channel.queueBind(commandQueueName,commandExchange,bindkey);
//将channel和queueName作为value放在对应的map中,key为serverId
channelMap.put(serverId,channel);
dataQueueNameMap.put(serverId,dataQueueName);
}
}
}
3.在使用的时候,如下:会根据消息获得对应的serverId,再根据serverId从map中获取对应的channel和queueName来发送消息
package com.xh.iot.rabbitmq;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.utils.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by Srma on 7/13/2018.
*/
@Component
public class NbiotDataSender {
public void sendDataToQueueByQueueName(String data,Integer serverId) throws Exception {
Channel channel = NbiotRabbitConfig.channelMap.get(serverId);
String queueName = NbiotRabbitConfig.dataQueueNameMap.get(serverId);
if(channel!=null && queueName!=null){
channel.basicPublish("", queueName, null,SerializationUtils.serialize(data));
}
}
}
4.在处理消息之前,需要将接收到的消息反序列化
@Component
public class MqttDataReceiver {
@Autowired
private NbiotDataHandler nbiotDataHandler;
@RabbitListener(queues = "base_nbiot_data")
@RabbitHandler
public void process(byte[] data){
System.out.println("Receiver : " + data);
try{
String result = (String) SerializationUtils.deserialize(data);
nbiotDataHandler.handleDataAsync(result);
}catch (Exception e){
e.printStackTrace();
}
}
}