Пример #1
0
  /**
   * @param packet the packet to intercept
   * @return the name of the interceptor that returned <code>false</code> or <code>null</code> if no
   *     interceptors returned <code>false</code>.
   */
  public static String invokeInterceptors(
      final Packet packet,
      final List<Interceptor> interceptors,
      final RemotingConnection connection) {
    if (interceptors != null) {
      for (final Interceptor interceptor : interceptors) {
        try {
          boolean callNext = interceptor.intercept(packet, connection);

          if (HornetQClientLogger.LOGGER.isDebugEnabled()) {
            // use a StringBuilder for speed since this may be executed a lot
            StringBuilder msg = new StringBuilder();
            msg.append("Invocation of interceptor ")
                .append(interceptor.getClass().getName())
                .append(" on ")
                .append(packet)
                .append(" returned ")
                .append(callNext);
            HornetQClientLogger.LOGGER.debug(msg.toString());
          }

          if (!callNext) {
            return interceptor.getClass().getName();
          }
        } catch (final Throwable e) {
          HornetQClientLogger.LOGGER.errorCallingInterceptor(e, interceptor);
        }
      }
    }

    return null;
  }
Пример #2
0
  /**
   * Due to networking issues or server issues the server may take longer to answer than expected..
   * the client may timeout the call throwing an exception and the client could eventually retry
   * another call, but the server could then answer a previous command issuing a
   * class-cast-exception. The expectedPacket will be used to filter out undesirable packets that
   * would belong to previous calls.
   */
  public Packet sendBlocking(final Packet packet, byte expectedPacket) throws HornetQException {
    String interceptionResult = invokeInterceptors(packet, interceptors, connection);

    if (interceptionResult != null) {
      // if we don't throw an exception here the client might not unblock
      throw HornetQClientMessageBundle.BUNDLE.interceptorRejectedPacket(interceptionResult);
    }

    if (closed) {
      throw HornetQClientMessageBundle.BUNDLE.connectionDestroyed();
    }

    if (connection.getBlockingCallTimeout() == -1) {
      throw new IllegalStateException(
          "Cannot do a blocking call timeout on a server side connection");
    }

    // Synchronized since can't be called concurrently by more than one thread and this can occur
    // E.g. blocking acknowledge() from inside a message handler at some time as other operation on
    // main thread
    synchronized (sendBlockingLock) {
      packet.setChannelID(id);

      final HornetQBuffer buffer = packet.encode(connection);

      lock.lock();

      try {
        if (failingOver) {
          try {
            if (connection.getBlockingCallFailoverTimeout() < 0) {
              while (failingOver) {
                failoverCondition.await();
              }
            } else {
              if (!failoverCondition.await(
                  connection.getBlockingCallFailoverTimeout(), TimeUnit.MILLISECONDS)) {
                HornetQClientLogger.LOGGER.debug("timed-out waiting for failover condition");
              }
            }
          } catch (InterruptedException e) {
            throw new HornetQInterruptedException(e);
          }
        }

        response = null;

        if (resendCache != null && packet.isRequiresConfirmations()) {
          resendCache.add(packet);
        }

        connection.getTransportConnection().write(buffer, false, false);

        long toWait = connection.getBlockingCallTimeout();

        long start = System.currentTimeMillis();

        while (!closed
            && (response == null
                || (response.getType() != PacketImpl.EXCEPTION
                    && response.getType() != expectedPacket))
            && toWait > 0) {
          try {
            sendCondition.await(toWait, TimeUnit.MILLISECONDS);
          } catch (InterruptedException e) {
            throw new HornetQInterruptedException(e);
          }

          if (response != null
              && response.getType() != PacketImpl.EXCEPTION
              && response.getType() != expectedPacket) {
            HornetQClientLogger.LOGGER.packetOutOfOrder(response, new Exception("trace"));
          }

          if (closed) {
            break;
          }

          final long now = System.currentTimeMillis();

          toWait -= now - start;

          start = now;
        }

        if (response == null) {
          throw HornetQClientMessageBundle.BUNDLE.timedOutSendingPacket(packet.getType());
        }

        if (response.getType() == PacketImpl.EXCEPTION) {
          final HornetQExceptionMessage mem = (HornetQExceptionMessage) response;

          HornetQException e = mem.getException();

          e.fillInStackTrace();

          throw e;
        }
      } finally {
        lock.unlock();
      }

      return response;
    }
  }