我有一个Java服务,该服务从Oracle Advanced Queue获取消息。我可以创建连接并收听并获取消息。我可以看到您可以停止并开始侦听消息,因此我已经为此实现了控件。但是,我希望能够报告侦听器的当前状态。我可以看到它是否存在,但是如何知道它是停止还是启动?
我有一个类似的容器类(Listener
是我自己的类(同时实现MessageListener
和ExceptionListener
),实际上对消息有作用)
public class QueueContainer {
private static final String QUEUE_NAME = "foo";
private final Connection dbConnection;
private final QueueConnection queueConnection;
private final QueueSession queueSession;
private final Queue queue;
private final MessageConsumer consumer;
private final Listener listener;
public QueueContainer(final Connection dbConnection ) {
try {
this.dbConnection = dbConnection;
queueConnection = AQjmsQueueConnectionFactory.createQueueConnection(dbConnection);
queueSession = queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
queue = ((AQjmsSession) queueSession).getQueue(context.getEnvironment(), QUEUE_NAME);
consumer = queueSession.createConsumer(queue);
listener = new Listener(QUEUE_NAME);
consumer.setMessageListener(listener);
queueConnection.setExceptionListener(listener);
} catch (JMSException | SQLException e) {
throw new RunTimeException("Queue Exception", e);
}
}
public void startListening() {
try {
queueConnection.start();
} catch (JMSException e) {
throw new RunTimeException("Failed to start listening to queue", e);
}
}
public void stopListening() {
try {
queueConnection.stop();
} catch (JMSException e) {
throw new RunTimeException("Failed to stop listening to queue", e);
}
}
public void close() {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {
throw new RunTimeException("Failed to stop listening to queue", e);
}
}
}
public boolean isRunning() {
try {
// This doesn't work - I can't distinguish between started and stopped
return queueConnection.getClientID() != null;
} catch (JMSException e) {
LOGGER.warn("Failed to get queue client ID", e);
return false;
}
}
我看不到要插入isRunning
的内容以区分已停止的侦听器和开始的侦听器
答案 0 :(得分:0)
没有JMS API调用来确定是否启动javax.jms.Connection
。
要清楚,队列本身不是已启动或已停止的实体。 连接已启动或停止。
您可能 可以从Oracle Advanced Queue实现对象获取此信息,但是我对该实现不熟悉,所以我不能说。显然,任何使用实现对象而不是标准API的解决方案都是不可移植的。
答案 1 :(得分:0)
JMS API假定您了解自己的所作所为。那么,为什么不添加一个布尔标志并跟踪呢?
private volatile boolean isListening = false;
...
public void startListening() {
try {
queueConnection.start();
isListening = true;
} catch (JMSException e) {
throw new RunTimeException("Failed to start listening to queue", e);
}
}
public void stopListening() {
try {
queueConnection.stop();
isListening = false;
} catch (JMSException e) {
throw new RunTimeException("Failed to stop listening to queue", e);
}
}
public void close() {
if (queueConnection != null) {
try {
queueConnection.close();
isListening = false;
} catch (JMSException e) {
throw new RunTimeException("Failed to stop listening to queue", e);
}
}
}
public boolean isRunning() {
return isListening;
}