我在使用 JMS 重新连接策略时遇到问题。我不确定我是否做得正确(很可能不是)。无论如何,我正在使用 WebLogic,这是一个消费者客户端。这是我获得连接的方法,并尝试添加自动重新连接。问题是它没有重新连接,并且没有任何异常记录什么。我通过启动 weblogic 来重新创建这种情况,启动获得初始连接的消费者客户端,我突然关闭 weblogic,然后我重新启动 weblogic,此时该消费者正在侦听的队列中的任何消息都保留在队列中而不被确认因为他们没有被接收。
public void setReceiver(MessageListener listener) {
try {
Properties parm = new Properties();
parm.setProperty("java.naming.factory.initial",
"weblogic.jndi.WLInitialContextFactory");
parm.setProperty("java.naming.provider.url", URL);
parm.setProperty("java.naming.security.principal", username);
parm.setProperty("java.naming.security.credentials", password);
ctx = new InitialContext(parm);
final QueueConnectionFactory connectionFactory = (QueueConnectionFactory) ctx
.lookup(conFactoryName);
connection = connectionFactory.createQueueConnection();
// TODO: 8/6/2012 Work on reconnection strategies for Consumer.
((WLConnection) connection)
.setReconnectPolicy(JMSConstants.RECONNECT_POLICY_ALL);
((WLConnection) connection).setReconnectBlockingMillis(30000L);
((WLConnection) connection).setTotalReconnectPeriodMillis(-1L);
session = connection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
// receiver = session.createReceiver(queue);
// receiver.setMessageListener(listener);
consumer = session.createConsumer(queue);
consumer.setMessageListener(listener);
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException arg0) {
// Assume Disconnected.
FileHandler fh = null;
try {
fh = new FileHandler("./logs/ExceptionListener", true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
Logger log2 = Logger.getLogger("ExceptionListener");
log2.addHandler(fh);
boolean connected = false;
do {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
log.warning(e.toString());
}
try {
connection = connectionFactory.createQueueConnection();
connection.setExceptionListener(this);
connection.start();
connected = true;
} catch (JMSException e) {
log.severe(e.toString());
}
}
} while (!connected);
}
});
connection.start();
} catch (JMSException je) {
log.severe(je.getMessage());
} catch (Exception e) {
log.severe(e.getMessage());
}
}