如何判断 JMS 队列是启动还是停止?

How to tell if a JMS queue is started or stopped?

我有一个 Java 服务,它从 Oracle Advanced Queue 获取消息。我可以创建连接并收听和接收消息。我可以看到您可以停止和开始收听消息,因此我已经为此实施了控制。但是,我希望能够报告监听器的当前状态。我可以看到它是否在那里,但我怎么知道它是停止还是开始?

我有一个容器class(Listener是我自己的class(实现了MessageListenerExceptionListener),它实际上是消息)

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 中放入什么来区分已停止和已启动的侦听器

没有 JMS API 调用来确定是否启动了 javax.jms.Connection

需要说明的是,队列本身不是启动或停止的实体。 连接 已启动或停止。

可能能够从 Oracle Advanced Queue 实现对象中获取此信息,但我不熟悉该实现,所以我不能说。显然,任何使用实现对象而不是标准 API 的解决方案都不可移植。

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