Example #1
0
  /** init is called lazily when a service is first used. */
  @Override
  public void init() {
    if (_lifecycle.isAfterInit()) {
      return;
    }

    start();

    _lifecycle.waitForActive(10000);
  }
Example #2
0
  private void start(StubAmp stub) {
    if (!_lifecycle.toStarting()) {
      return;
    }

    // OutboxAmpBase buildContext = new OutboxAmpBase();

    // BuildMessageAmp buildMessage = new BuildMessageAmp(this);
    // MessageAmp oldMessage = ContextMessageAmp.getAndSet(buildMessage);

    // Outbox<MessageAmp> oldContext = OutboxThreadLocal.getCurrent();

    try {
      // OutboxThreadLocal.setCurrent(buildContext);

      MessageAmp onActiveMsg = new OnActiveMessage(this, isSingle());

      JournalAmp journal = stub.journal();

      // buildContext.setMessage(onActiveMsg);

      if (journal != null) {
        if (log.isLoggable(Level.FINER)) {
          log.finer(
              L.l(
                  "journal replay {0} ({1})",
                  serviceRef().address(), serviceRef().api().getType()));
        }

        onActiveMsg = new OnActiveReplayMessage(this, stub, isSingle());

        ActiveResult activeResult = new ActiveResult(onActiveMsg);

        ReplayMessage replayMsg = new ReplayMessage(this, _queue, activeResult);

        // buildContext.setMessage(replayMsg);

        _queue.offer(replayMsg);
        _queue.wake();
      } else {
        // _worker.onActive();
        // _queue.offer(onActiveMsg);
        _lifecycle.toActive();
        // _queue.wake();
      }
    } finally {
      // OutboxThreadLocal.setCurrent(oldContext);
      // ContextMessageAmp.set(oldMessage);
    }
  }
Example #3
0
  /** Start is called lazily when a service is first used. */
  @Override
  public void start() {
    if (_lifecycle.isActive()) {
      return;
    }

    synchronized (_lifecycle) {
      if (!_lifecycle.toInit()) {
        return;
      }

      init(_stubMain);
    }

    start(_stubMain);
  }
Example #4
0
  /** Closes the inbox */
  @Override
  public void shutdown(ShutdownModeAmp mode) {
    if (!_lifecycle.toStopping()) {
      return;
    }

    _lifecycle.toDestroy();

    OnShutdownMessage shutdownMessage = new OnShutdownMessage(this, mode, isSingle());

    _queue.offer(shutdownMessage);

    // _queue.close();
    /*
    for (Actor<?> actorProcessor : _queueActors) {
      actorProcessor.close();
    }
    */

    _queue.wakeAllAndWait();

    shutdownMessage.waitFor(1, TimeUnit.SECONDS);

    super.shutdown(mode);

    try (OutboxAmp outbox = OutboxAmp.currentOrCreate(manager())) {
      Object ctx = outbox.getAndSetContext(this);

      try {
        outbox.flush();

        if (!isSingle()) {
          _worker.shutdown(mode);
        }
      } finally {
        outbox.getAndSetContext(ctx);
      }
    }

    // XXX: _worker.shutdown(ShutdownModeAmp.IMMEDIATE);
  }
Example #5
0
  @Override
  public boolean offerResult(MessageAmp message) {
    if (_lifecycle.isAfterStopping()) {
      message.fail(
          new ServiceExceptionClosed(
              L.l("closed {0} for message {1} and inbox {2}", serviceRef(), message, this)));

      return true;
    }

    QueueDeliver<MessageAmp> queue = _queue;

    boolean value = queue.offer(message, 0, TimeUnit.MILLISECONDS);
    // boolean value = queue.offer(message, 10, TimeUnit.MILLISECONDS, 0);

    // XXX: offer shouldn't automatically wake
    // queue.wake();

    if (!value) {
      InboxQueueReplyOverflow overflowQueue;

      synchronized (this) {
        overflowQueue = _replyOverflowQueue;

        if (overflowQueue == null) {
          _replyOverflowQueue = overflowQueue = new InboxQueueReplyOverflow(queue);
        }
      }

      return overflowQueue.offer(message);
    }

    return value;

    // offerAndWake(message);

    // return true;
  }
Example #6
0
  @Override
  public boolean offer(MessageAmp message, long callerTimeout) {
    if (_lifecycle.isAfterStopping()) {
      message.fail(
          new ServiceExceptionClosed(L.l("closed {0} for message {1}", serviceRef(), message)));

      return true;
    }

    QueueDeliver<MessageAmp> queue = _queue;

    long timeout = Math.min(_sendTimeout, callerTimeout);

    boolean result = queue.offer(message, timeout, TimeUnit.MILLISECONDS);

    if (!result) {
      _fullHandler.onQueueFull(serviceRef(), queue.size(), timeout, TimeUnit.MILLISECONDS, message);

      return false;
    }

    return result;
  }
Example #7
0
 @Override
 public boolean isClosed() {
   return _lifecycle.isDestroyed();
 }