我有一个Spring 3应用程序,它通过非RabbitMQ接收器接收消息,处理它们并通过RabbitMQ转发。每次发送消息时,都会构建一个新的RabbitMQ连接。这似乎有点浪费。我只是想知道这是否真的有必要,或者是否有原因导致连接无法在Singleton中保存并且只构建一次(对于多个发送)。这是发送方法:
private void send(String routingKey, String message) throws Exception {
String exchange = applicationConfiguration.getAMQPExchange();
String ipAddress = applicationConfiguration.getAMQPHost();
String exchangeType = applicationConfiguration.getAMQPExchangeType();
String password = applicationConfiguration.getAMQPUser();
String user = applicationConfiguration.getAMQPPassword();
String virtualHost = applicationConfiguration.getAMQPVirtualHost();
String port = applicationConfiguration.getAMQPPort();
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername(user);
factory.setPassword(password);
factory.setVirtualHost(virtualHost);
factory.setPort(Integer.parseInt(port));
factory.setHost(ipAddress);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(exchange, exchangeType);
channel.basicPublish(exchange, routingKey, null, message.getBytes());
log.debug(" [AMQP] Sent message with key {} : {}",routingKey, message);
connection.close();
}
或可能的单身人士:
public class MyConnection {
private static MyConnection singleton = new MyConnection();
private static Connection connection;
private MyConnection() {
ConnectionFactory factory = new ConnectionFactory();
String exchange = applicationConfiguration.getAMQPExchange();
String ipAddress = applicationConfiguration.getAMQPHost();
String exchangeType = applicationConfiguration.getAMQPExchangeType();
String password = applicationConfiguration.getAMQPUser();
String user = applicationConfiguration.getAMQPPassword();
String virtualHost = applicationConfiguration.getAMQPVirtualHost();
String port = applicationConfiguration.getAMQPPort();
try {
factory.setUsername(user);
factory.setPassword(password);
factory.setVirtualHost(virtualHost);
factory.setPort(Integer.parseInt(port));
factory.setHost(ipAddress);
connection = factory.newConnection();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getInstance( ) {
return connection;
}
}
答案 0 :(得分:3)
连接可以是单例,您可以共享它以进行多次发送。 通道应该是一个用于线程。
你的代码可能是:
private void send(String routingKey, String message) throws Exception {
Connection connection = MyConnection().getInstance();
Channel channel = connection.createChannel();
channel.exchangeDeclare(exchange, exchangeType);
channel.basicPublish(exchange, routingKey, null, message.getBytes());
log.debug(" [AMQP] Sent message with key {} : {}",routingKey, message);
channel.close();
}
您可以决定为每个发布创建和销毁一个频道,或者为您的线程创建它,并始终重用相同的频道。
修改** 强> 为了在这里创建一个sigleton: http://javarevisited.blogspot.it/2012/12/how-to-create-thread-safe-singleton-in-java-example.html
public class MySingletonConnection{
private static final MySingletonConnection INSTANCE = new MySingletonConnection();
private Connection myConnection;
private Singleton(){
// here you can init your connection parameter
}
public static MySingletonConnection getInstance(){
return INSTANCE;
}
public Connection getConnection( ) {
return connection;
}
}
这是创建单身人士的一种方式
private void send(String routingKey, String message) throws Exception {
Connection connection = MySingletonConnection().getInstance().getConnection();