【问题标题】:How to tell if a JMS queue is started or stopped?如何判断 JMS 队列是启动还是停止?
【发布时间】:2019-03-26 10:21:20
【问题描述】:

我有一个从 Oracle 高级队列获取消息的 Java 服务。我可以创建连接并收听并接收消息。我可以看到您可以停止并开始收听消息,因此我为此实施了控制。但是,我希望能够报告侦听器的当前状态。我可以查看它是否存在,但我如何判断它是停止还是启动?

我有一个容器类(Listener 是我自己的类(实现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 中可以区分已停止和已启动的侦听器的内容

【问题讨论】:

    标签: java jms oracle-aq


    【解决方案1】:

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

    【讨论】:

    • 会的。曾希望它会报告 - 可能会发生一些事情来阻止它。但我会从异常处理程序重新连接并在那里处理它。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2013-01-19
      • 1970-01-01
      相关资源
      最近更新 更多