0
怎麼可能使用ActiveMQ的持久連接/會話入隊的消息不同的隊列?ActiveMQ的生產者多隊列中的一個會議
我做了什麼:
public class ActiveMQProducer {
private static final Logger LOGGER = Logger.getLogger(ActiveMQProducer.class);
private Connection connection;
private MessageProducer producer;
private Session session;
String activeMQConnection;
public ActiveMQProducer() throws ConfigurationException, JMSException {
activeMQConnection = ActiveMQPropertyManagerFactory.getInstance().getString("active.mq.url");
}
public void setupActiveMQ(String queueName) throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(activeMQConnection);
factory.setRejectedTaskHandler(new ThreadPoolExecutor.CallerRunsPolicy());
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(queueName);
producer = session.createProducer(queue);
}
public void getConnection(String queueName) throws JMSException {
if (connection == null || session == null) {
Object object = new Object();
synchronized (object) {
setupActiveMQ(queueName);
}
}
}
public <T extends Serializable> T sendToActiveMQ(String queueName, T t) throws JMSException {
getConnection(queueName);
ObjectMessage message = session.createObjectMessage(t);
producer.send(message);
return null;
}
public void sendMessageToActiveMQ(String queueName, String message) throws JMSException {
getConnection(queueName);
TextMessage toSend = session.createTextMessage(message);
producer.send(toSend);
}
}
我以這一點,併發送消息到不同的隊列實現最終的ActiveMQ用完的連接,因爲我從來沒有關閉連接或會話:
org.apache.activemq.transport.tcp.ExceededMaximumConnectionsException: Exceeded the maximum number of allowed client connections.
什麼是處理這個正確的方法是什麼?我有大約5隊列我有不同的消息發送到,我應該打開一個新的連接,入隊和關閉連接,反正是有保持會話/連接執着?
謝謝。
神奇的解決方案。謝謝。 – Asiriel