简体   繁体   English

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

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

I have a Java service which is getting messages from an Oracle Advanced Queue. 我有一个Java服务,它从Oracle Advanced Queue获取消息。 I can create the connection and listen and get messages OK. 我可以创建连接并监听并获取消息。 I can see that you can stop and start listening for messages, so I have implemented controls for that. 我可以看到你可以停止并开始收听消息,所以我已经实现了控制。 However, I would like to be able to report on the current status of the listener. 但是,我希望能够报告监听器的当前状态。 I can see if it's there, but how can I tell if it's stopped or started? 我可以看看它是否存在,但我怎么知道它是停止还是开始?

I have a container class along the lines of ( Listener is my own class (implementing both MessageListener and ExceptionListener ) which actually does something with the message) 我有一个容器类( 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;
    }
  }

I can't see what to put in isRunning that could distinguish between a stopped and started listener 我无法看到isRunning中的内容可以区分已停止和已启动的侦听器

There is no JMS API call to determine whether or not a javax.jms.Connection is started. 没有JMS API调用来确定是否启动了javax.jms.Connection

To be clear, the queue itself is not the entity that is started or stopped. 要清楚,队列本身不是启动或停止的实体。 The connection is started or stopped. 连接已启动或停止。

You may be able to get this information from the Oracle Advanced Queue implementation object, but I'm not familiar with that implementation so I can't say. 可以从Oracle Advanced Queue实现对象获取此信息,但我不熟悉该实现,因此我不能说。 Obviously any solution using an implementation object rather than the standard API will not be portable. 显然,使用实现对象而不是标准API的任何解决方案都不可移植。

The JMS API assumes you know yourself what you did. JMS API假设您了解自己所做的事情。 So why not add a boolean flag and keep track of this ? 那么为什么不添加一个布尔标志并跟踪它呢?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM