コード例 #1
0
ファイル: JmsService.java プロジェクト: juanavelez/qbit
  /**
   * Get the current connection or create one using the connectionSupplier.
   *
   * @return JMS Connection
   */
  private Connection getConnection() {

    if (!connectionOption.isPresent()) {
      Connection connection = connectionSupplier.get();

      if (connection instanceof ActiveMQConnection) {
        ((ActiveMQConnection) connection)
            .addTransportListener(
                new TransportListener() {
                  @Override
                  public void onCommand(Object command) {}

                  @Override
                  public void onException(IOException error) {}

                  @Override
                  public void transportInterupted() {
                    connected.set(false);
                  }

                  @Override
                  public void transportResumed() {
                    connected.set(true);
                  }
                });
      }

      connected.set(true);

      if (startConnection) {
        try {
          connection.start();
        } catch (JMSException e) {
          throw new IllegalStateException("Unable to start JMS connection", e);
        }
      }
      connectionOption = Optional.of(connection);
    }
    return connectionOption.get();
  }