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

com.caucho.v5.amp.deliver.WorkerDeliverDisruptor Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * Copyright (c) 1998-2015 Caucho Technology -- all rights reserved
 *
 * This file is part of Baratine(TM)(TM)
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Baratine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Baratine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Baratine; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package com.caucho.v5.amp.deliver;

import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.caucho.v5.amp.spi.ShutdownModeAmp;


/**
 * Interface for the transaction log.
 */
public final class WorkerDeliverDisruptor // extends MessageOutbox>
  extends WorkerDeliverBase
{
  private static final Logger log
    = Logger.getLogger(WorkerDeliverDisruptor.class.getName());
  
  private final QueueRing _queue;
  private final Deliver _deliver;
  
  private final int _headCounter;
  private final int _tailCounter;
  private final boolean _isTail;
  
  private WorkerDeliver _headWorker;
  private final WorkerDeliver _tailWorker;
  
  public WorkerDeliverDisruptor(Deliver deliver,
                                Object outboxContext,
                                Executor executor,
                                ClassLoader loader,
                                QueueRing queue,
                                int headCounterIndex, 
                                int tailCounterIndex,
                                boolean isTail,
                                WorkerDeliver tailWorker)
  {
    super(deliver, outboxContext, executor, loader);

    _queue = queue;
    _deliver = deliver;
    _headCounter = headCounterIndex;
    _tailCounter = tailCounterIndex;
    _tailWorker = tailWorker;
    _isTail = isTail;
  }
  
  public void setHeadWorker(WorkerDeliver headWorker)
  {
    _headWorker = headWorker;
    
    if (_tailWorker instanceof WorkerDeliverDisruptor) {
      WorkerDeliverDisruptor tailDisruptor
        = (WorkerDeliverDisruptor) _tailWorker;
      
      tailDisruptor.setHeadWorker(headWorker);
    }
  }

  @Override
  public void runImpl(Outbox outbox, M msg)
  {
    
    final Deliver deliver = _deliver;
    final QueueRing queue = _queue;
    final WorkerDeliver tailWorker = _tailWorker;
    final boolean isTail = _isTail;
    
    if (msg != null) {
      if (queue.offer(msg, 0, TimeUnit.SECONDS)) {
        msg = null;
      }
    }
    
    try {
      deliver.beforeBatch();

      try {
        queue.deliver(deliver,
                      outbox,
                      _headCounter,
                      _tailCounter,
                      tailWorker, isTail);
      } finally {
        deliver.afterBatch();
      }
    } catch (Throwable e) {
      log.log(Level.FINER, e.toString(), e);
    }
    
    if (msg != null) {
      if (! queue.offer(msg, 10, TimeUnit.SECONDS)) {
        log.warning("Dropped message: " + msg);
      }
    }
    
    if (isTail && queue.size() > 0 && _headWorker != null) {
      // with queue resizing, the head might need to be woken
      _headWorker.wake();
    }
  }

  /*
  @Override
  public void onActive()
  {
    _deliver.onActive();
    
    if (! _isTail) {
      _tailWorker.onActive();
    }
  }

  @Override
  public void onInit()
  {
    _deliver.onInit();
    
    if (! _isTail) {
      _tailWorker.onInit();
    }
  }
  */

  @Override
  public void shutdown(ShutdownModeAmp mode)
  {
    // cloud/05h3
    // _deliver.shutdown(mode);
    
    if (! _isTail) {
      _tailWorker.shutdown(mode);
    }
  }
  
  @Override
  public String toString()
  {
    return getClass().getSimpleName() + "[" + _deliver + "," + getState() + "]";
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy