Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* The MIT License (MIT)
*
* Copyright (c) 2019 nobark (tools4j), Marco Terzer, Anton Anufriev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.tools4j.nobark.queue;
import sun.misc.Contended;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
/**
* A conflation queue implementation that merges old and new value if a value is enqueued and another one already exists
* in the queue with the same conflation key. The backing queue is supplied to the constructor and it determines
* whether single or multiple producers and consumers are supported.
*
* Each producer should acquire its own {@link #appender() appender} from the producing thread, and similarly every
* consumer should call {@link #poller()} from the consuming thread. Note that appender listener and poller listener
* must be thread safe if multiple producers or consumers are used, e.g. use
* {@link AppenderListener#threadLocalSupplier(Supplier)} and {@link PollerListener#threadLocalSupplier(Supplier)} to
* create separate listener instances per producer/consumer thread.
*
* @param the type of the conflation key
* @param the type of elements in the queue
*/
public class MergeConflationQueue implements ExchangeConflationQueue {
private final Queue>> queue;
private final Map>> entryMap;
private final Merger super K, V> merger;
private final ThreadLocal> appender = ThreadLocal.withInitial(MergeQueueAppender::new);
private final ThreadLocal> poller = ThreadLocal.withInitial(MergeQueuePoller::new);
private final Supplier extends AppenderListener super K, ? super V>> appenderListenerSupplier;
private final Supplier extends PollerListener super K, ? super V>> pollerListenerSupplier;
private MergeConflationQueue(final Queue>> queue,
final Map>> entryMap,
final Merger super K, V> merger,
final Supplier extends AppenderListener super K, ? super V>> appenderListenerSupplier,
final Supplier extends PollerListener super K, ? super V>> pollerListenerSupplier) {
this.queue = Objects.requireNonNull(queue);
this.entryMap = Objects.requireNonNull(entryMap);
this.merger = Objects.requireNonNull(merger);
this.appenderListenerSupplier = Objects.requireNonNull(appenderListenerSupplier);
this.pollerListenerSupplier = Objects.requireNonNull(pollerListenerSupplier);
}
/**
* Constructor with queue factory and merger. A concurrent hash map is used to manage entries per conflation
* key.
*
* @param queueFactory the factory to create the backing queue
* @param merger the merge strategy to use if conflation occurs
*/
public MergeConflationQueue(final Supplier extends Queue