All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vlingo.actors.plugin.mailbox.agronampscarrayqueue.ManyToOneConcurrentArrayQueueMailbox Maven / Gradle / Ivy

Go to download

Type safe Actor Model toolkit for reactive concurrency and resiliency using Java and other JVM languages.

There is a newer version: 1.7.5
Show newest version
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.

package io.vlingo.actors.plugin.mailbox.agronampscarrayqueue;

import org.agrona.concurrent.ManyToOneConcurrentArrayQueue;

import io.vlingo.actors.Dispatcher;
import io.vlingo.actors.Mailbox;
import io.vlingo.actors.Message;

public class ManyToOneConcurrentArrayQueueMailbox implements Mailbox {
  private final Dispatcher dispatcher;
  private final boolean notifyOnSend;
  private final ManyToOneConcurrentArrayQueue queue;
  private final int totalSendRetries;

  @Override
  public void close() {
    dispatcher.close();
    queue.clear();
  }

  @Override
  public boolean isClosed() {
    return dispatcher.isClosed();
  }

  @Override
  public boolean isDelivering() {
    throw new UnsupportedOperationException("ManyToOneConcurrentArrayQueueMailbox does not support this operation.");
  }

  @Override
  public int concurrencyCapacity() {
    return 1;
  }

  @Override
  public void run() {
    throw new UnsupportedOperationException("ManyToOneConcurrentArrayQueueMailbox does not support this operation.");
  }

  @Override
  public void resume(final String name) {
    // TODO: Consider supporting Stowage here
    System.out.println("WARNING: ManyToOneConcurrentArrayQueueMailbox does not support resume(): " + name);
  }

  @Override
  public void send(final Message message) {
    for (int tries = 0; tries < totalSendRetries; ++tries) {
      if (queue.offer(message)) {
        if (notifyOnSend) {
          dispatcher.execute(this);
        }
        return;
      }
      while (pendingMessages() >= queue.capacity()) ;
    }
    throw new IllegalStateException("Count not enqueue message due to busy mailbox.");
  }

  @Override
  public void suspendExceptFor(final String name, final Class... overrides) {
    // TODO: Consider supporting Stowage here
    if (!name.equals(Mailbox.Stopping)) {
      System.out.println("WARNING: ManyToOneConcurrentArrayQueueMailbox does not support suspendExceptFor(): " + name + " overrides: " + overrides);
    }
  }

  @Override
  public boolean isSuspended() {
    return false;
  }

  @Override
  public final Message receive() {
    return queue.poll();
  }

  /* @see io.vlingo.actors.Mailbox#pendingMessages() */
  @Override
  public int pendingMessages() {
    return queue.size();
  }

  protected ManyToOneConcurrentArrayQueueMailbox(final Dispatcher dispatcher, final int mailboxSize, final int totalSendRetries, final boolean notifyOnSend) {
    this.dispatcher = dispatcher;
    this.queue = new ManyToOneConcurrentArrayQueue<>(mailboxSize);
    this.totalSendRetries = totalSendRetries;
    this.notifyOnSend = notifyOnSend;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy