gpt4 book ai didi

java - 每次发送消息是否需要重建RabbitMQ连接

转载 作者:行者123 更新时间:2023-11-29 03:18:47 25 4
gpt4 key购买 nike

我有一个 Spring 3 应用程序,它通过非 RabbitMQ 接收器接收消息,处理它们并通过 RabbitMQ 转发。每次发送消息时,都会建立一个新的 RabbitMQ 连接。这似乎有点浪费。我只是想知道这是否真的有必要,或者是否存在连接不能保持在单例中并且只能构建一次(用于多次发送)的原因。这是发送方法:

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;
}
}

最佳答案

连接可以是单例的,您可以共享它进行多次发送。该 channel 应该是一个线程。

你的代码可以是:

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();
}

您可以决定为每个发布创建和销毁一个 channel ,或者为您的线程创建一个 channel 并始终重复使用相同的 channel 。

编辑**为了创建一个 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();

关于java - 每次发送消息是否需要重建RabbitMQ连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24910502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com