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

quickfix.mina.QueueTrackers Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
package quickfix.mina;

import org.apache.mina.core.session.IoSession;
import quickfix.Responder;
import quickfix.Session;

import java.util.Collection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import static java.lang.String.format;

/**
 * Queue trackers factory methods
 */
final class QueueTrackers {
    private static final String LOWER_WATERMARK_FMT = "inbound queue size < lower watermark (%d), socket reads resumed";
    private static final String UPPER_WATERMARK_FMT = "inbound queue size > upper watermark (%d), socket reads suspended";

    /**
     * Watermarks-based queue tracker
     */
    static  WatermarkTracker newMultiSessionWatermarkTracker(
            BlockingQueue queue,
            long lowerWatermark, long upperWatermark,
            Function classifier) {
        return WatermarkTracker.newMulti(queue, lowerWatermark, upperWatermark, classifier,
                qfSession -> resumeReads(qfSession, (int)lowerWatermark),
                qfSession -> suspendReads(qfSession, (int)upperWatermark));
    }

    /**
     * Default no-op queue tracker
     */
    static  QueueTracker newDefaultQueueTracker(BlockingQueue queue) {
        return new QueueTracker() {
            @Override
            public void put(E e) throws InterruptedException {
                queue.put(e);
            }

            @Override
            public E poll(long timeout, TimeUnit unit) throws InterruptedException {
                return queue.poll(timeout, unit);
            }

            @Override
            public int drainTo(Collection collection) {
                return queue.drainTo(collection);
            }
        };
    }

    private static IoSession lookupIoSession(Session qfSession) {
        final Responder responder = qfSession.getResponder();

        if (responder instanceof IoSessionResponder) {
            return ((IoSessionResponder)responder).getIoSession();
        } else {
            return null;
        }
    }

    private static void resumeReads(Session qfSession, int queueLowerWatermark) {
        final IoSession ioSession = lookupIoSession(qfSession);
        if (ioSession != null && ioSession.isReadSuspended()) {
            ioSession.resumeRead();
            qfSession.getLog().onEvent(format(LOWER_WATERMARK_FMT, queueLowerWatermark));
        }
    }

    private static void suspendReads(Session qfSession, int queueUpperWatermark) {
        final IoSession ioSession = lookupIoSession(qfSession);
        if (ioSession != null && !ioSession.isReadSuspended()) {
            ioSession.suspendRead();
            qfSession.getLog().onEvent(format(UPPER_WATERMARK_FMT, queueUpperWatermark));
        }
    }

    static  WatermarkTracker newSingleSessionWatermarkTracker(
            BlockingQueue queue,
            long lowerWatermark, long upperWatermark,
            Session qfSession) {
        return WatermarkTracker.newMono(queue, lowerWatermark, upperWatermark,
                () -> resumeReads(qfSession, (int)lowerWatermark),
                () -> suspendReads(qfSession, (int)upperWatermark));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy