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

io.vlingo.actors.plugin.mailbox.concurrentqueue.ConcurrentQueueMailbox 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-2018 Vaughn Vernon. 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.concurrentqueue;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;

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

public class ConcurrentQueueMailbox implements Mailbox, Runnable {
  private AtomicBoolean delivering;
  private final Dispatcher dispatcher;
  private final Queue queue;
  private final byte throttlingCount;

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

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

  @Override
  public void send(final Message message) {
    queue.add(message);
    if (!isDelivering()) {
      dispatcher.execute(this);
    }
  }

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

  @Override
  public boolean isDelivering() {
    return delivering.get();
  }

  @Override
  public boolean delivering(final boolean flag) {
    return delivering.compareAndSet(!flag, flag);
  }

  @Override
  public void run() {
    final int total = (int) throttlingCount;
    for (int count = 0; count < total; ++count) {
      final Message message = receive();
      if (message != null) {
        message.deliver();
      } else {
        break;
      }
    }
    delivering(false);
    if (!queue.isEmpty()) {
      dispatcher.execute(this);
    }
  }
  
  /* @see io.vlingo.actors.Mailbox#pendingMessages() */
  @Override
  public int pendingMessages() {
    return queue.size();
  }
  
  protected ConcurrentQueueMailbox(final Dispatcher dispatcher, final int throttlingCount) {
    this.dispatcher = dispatcher;
    this.delivering = new AtomicBoolean(false);
    this.queue = new ConcurrentLinkedQueue();
    this.throttlingCount = (byte) throttlingCount;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy