示例#1
0
    @Override
    public void innerRun() {
      try (CDC ignored = _lock.getCdc().restore()) {
        CellMessageAnswerable callback = _lock.getCallback();

        CellMessage answer;
        Object obj;
        try {
          answer = _message.decode();
          obj = answer.getMessageObject();
        } catch (SerializationException e) {
          LOGGER.warn(e.getMessage());
          obj = e;
          answer = null;
        }

        EventLogger.sendEnd(_lock.getMessage());
        if (obj instanceof Exception) {
          callback.exceptionArrived(_lock.getMessage(), (Exception) obj);
        } else {
          callback.answerArrived(_lock.getMessage(), answer);
        }
        LOGGER.trace("addToEventQueue : callback done for : {}", _message);
      }
    }
示例#2
0
  public void sendMessage(
      CellMessage msg, boolean local, boolean remote, CellMessageAnswerable callback, long timeout)
      throws SerializationException {
    if (!msg.isStreamMode()) {
      msg.touch();
    }

    msg.setTtl(timeout);

    EventLogger.sendBegin(this, msg, "callback");
    UOID uoid = msg.getUOID();
    boolean success = false;
    try {
      CellLock lock = new CellLock(msg, callback, timeout);
      synchronized (_waitHash) {
        _waitHash.put(uoid, lock);
      }

      __cellGlue.sendMessage(this, msg, local, remote);
      success = true;
    } catch (NoRouteToCellException e) {
      if (callback != null) {
        callback.exceptionArrived(msg, e);
      }
    } finally {
      if (!success) {
        synchronized (_waitHash) {
          _waitHash.remove(uoid);
        }
        EventLogger.sendEnd(msg);
      }
    }
  }
示例#3
0
  public CellMessage sendAndWait(CellMessage msg, boolean local, boolean remote, long timeout)
      throws SerializationException, NoRouteToCellException, InterruptedException {
    if (!msg.isStreamMode()) {
      msg.touch();
    }

    msg.setTtl(timeout);

    EventLogger.sendBegin(this, msg, "blocking");
    UOID uoid = msg.getUOID();
    try {
      CellLock lock = new CellLock();
      synchronized (_waitHash) {
        _waitHash.put(uoid, lock);
      }
      LOGGER.trace("sendAndWait : adding to hash : {}", uoid);

      __cellGlue.sendMessage(this, msg, local, remote);

      //
      // because of a linux native thread problem with
      // wait(n > 0), we have to use a interruptedFlag
      // and the time messurement.
      //
      synchronized (lock) {
        long start = System.currentTimeMillis();
        while (lock.getObject() == null && timeout > 0) {
          lock.wait(timeout);
          timeout -= (System.currentTimeMillis() - start);
        }
      }
      CellMessage answer = (CellMessage) lock.getObject();
      if (answer == null) {
        return null;
      }
      answer = answer.decode();

      Object obj = answer.getMessageObject();
      if (obj instanceof NoRouteToCellException) {
        throw (NoRouteToCellException) obj;
      } else if (obj instanceof SerializationException) {
        throw (SerializationException) obj;
      }
      return answer;
    } finally {
      synchronized (_waitHash) {
        _waitHash.remove(uoid);
      }
      EventLogger.sendEnd(msg);
    }
  }
示例#4
0
  public void sendMessage(CellMessage msg, boolean locally, boolean remotely)
      throws SerializationException, NoRouteToCellException {

    if (!msg.isStreamMode()) {
      msg.touch();
    }

    EventLogger.sendBegin(this, msg, "async");
    try {
      __cellGlue.sendMessage(this, msg, locally, remotely);
    } finally {
      EventLogger.sendEnd(msg);
    }
  }
示例#5
0
  public int updateWaitQueue() {
    Collection<CellLock> expired = new ArrayList<>();
    long now = System.currentTimeMillis();
    int size;

    synchronized (_waitHash) {
      Iterator<CellLock> i = _waitHash.values().iterator();
      while (i.hasNext()) {
        CellLock lock = i.next();
        if (lock != null && !lock.isSync() && lock.getTimeout() < now) {
          expired.add(lock);
          i.remove();
        }
      }
      size = _waitHash.size();
    }

    //
    // _waitHash can't be used here. Otherwise
    // we will end up in a deadlock (NO LOCKS WHILE CALLING CALLBACKS)
    //
    for (CellLock lock : expired) {
      try {
        CellMessage envelope = lock.getMessage();
        EventLogger.sendEnd(envelope);
        lock.getCallback().answerTimedOut(envelope);
      } catch (RuntimeException e) {
        /* Don't let a problem in the callback prevent us from
         * expiring all messages.
         */
        Thread t = Thread.currentThread();
        t.getUncaughtExceptionHandler().uncaughtException(t, e);
      }
    }

    return size;
  }