Пример #1
0
  /** Init calls the @OnInit methods. */
  private void init(StubAmp stub) {
    // BuildMessageAmp buildMessage = new BuildMessageAmp(this);

    OnInitMessage onInitMsg = new OnInitMessage(this);

    _queue.offer(onInitMsg);
    _queue.wake();
  }
Пример #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);
    }
  }
Пример #3
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);
  }
Пример #4
0
  @Override
  public final void offerAndWake(MessageAmp message, long callerTimeout) {
    if (isClosed()) { // _lifecycle.isAfterStopping()) {
      message.fail(
          new ServiceExceptionClosed(
              L.l("Closed {0}, stub {1} for message {2}", serviceRef(), _stubMain, message)));

      return;
    }

    QueueDeliver<MessageAmp> queue = _queue;

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

    if (!queue.offer(message, timeout, TimeUnit.MILLISECONDS)) {
      // ThreadDump.create().dumpThreads();
      _fullHandler.onQueueFull(serviceRef(), queue.size(), timeout, TimeUnit.MILLISECONDS, message);
    }

    queue.wake();
  }
Пример #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;
  }
Пример #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;
  }
Пример #7
0
  public InboxQueue(
      ServicesAmp manager,
      QueueDeliverBuilderImpl<MessageAmp> queueBuilder,
      QueueServiceFactoryInbox serviceQueueFactory,
      ServiceConfig config) {
    super(manager);

    _isFiner = log.isLoggable(Level.FINER);

    _inboxMessage = new InboxMessage(this);

    StubAmp stubMain = serviceQueueFactory.stubMain();

    String name = stubMain.name();

    if (name != null) {
    } else if (config.address() != null) {
      name = config.address();
    } else if (config.name() != null) {
      name = config.name();
    } else {
      TypeRef typeRef = TypeRef.of(stubMain.api().getType());
      name = "anon:" + typeRef.rawClass().getSimpleName();
    }

    _anonAddress = name;

    if (config.isPublic()) {
      _serviceRef = new ServiceRefPublic(stubMain, this);
    } else {
      _serviceRef = new ServiceRefLocal(stubMain, this);
    }

    // queueBuilder.setOutboxContext(new OutboxContextAmpImpl(this));
    queueBuilder.setOutboxContext(this);

    _queue = serviceQueueFactory.build(queueBuilder, this);

    _worker = worker(_queue);

    _stubMain = stubMain;

    _isLifecycleAware = stubMain.isLifecycleAware() || !_queue.isSingleWorker();

    long timeout = config.queueTimeout();

    if (timeout < 0) {
      timeout = 60 * 1000L;
    }

    _sendTimeout = timeout;

    QueueFullHandler handler = config.queueFullHandler();

    if (handler == null) {
      handler = manager.queueFullHandler();
    }

    _fullHandler = handler;

    // start(actor);
  }
Пример #8
0
 public void wakeAllAndWait() {
   _queue.wakeAllAndWait();
 }
Пример #9
0
 void wakeAll() {
   _queue.wakeAll();
 }
Пример #10
0
  @Override
  public final long getSize() {
    int size = _queue.size();

    return size;
  }
Пример #11
0
 @SuppressWarnings("unchecked")
 private WorkerDeliver<MessageAmp> worker(QueueDeliver<MessageAmp> queue) {
   return (WorkerDeliver<MessageAmp>) queue.worker();
 }