001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.ecm.platform.importer.mqueues.mqueues;
020
021import java.io.Externalizable;
022import java.time.Duration;
023import java.util.Collection;
024
025/**
026 * Sequential reader for a partition or multiple partitions.
027 *
028 * A tailer is not thread safe and should not be shared by multiple threads.
029 *
030 */
031public interface MQTailer<M extends Externalizable> extends AutoCloseable {
032
033    /**
034     * Returns the consumer group.
035     *
036     */
037    String group();
038
039    /**
040     * Returns the list of MQueue name/partition tuples currently assigned to this tailer.
041     * Assignments can change only if the tailer has been created using {@link MQManager#subscribe}.
042     */
043    Collection<MQPartition> assignments();
044
045    /**
046     * Read a message from assigned partitions within the timeout.
047     *
048     * @return null if there is no message in the queue after the timeout.
049     * @throws MQRebalanceException if a partition rebalancing happen during the read,
050     * this is possible only when using {@link MQManager#subscribe}.
051     */
052    MQRecord<M> read(Duration timeout) throws InterruptedException;
053
054    /**
055     * Commit current positions for all partitions (last message offset returned by read).
056     */
057    void commit();
058
059    /**
060     * Commit current position for the partition.
061     *
062     * @return the committed offset, can return null if there was no previous read done on this partition.
063     */
064    MQOffset commit(MQPartition partition);
065
066    /**
067     * Set the current positions to the end of all partitions.
068     */
069    void toEnd();
070
071    /**
072     * Set the current position to the fist message of all partitions.
073     */
074    void toStart();
075
076    /**
077     * Set the current positions to previously committed positions.
078     */
079    void toLastCommitted();
080
081    /**
082     * Returns {@code true} if the tailer has been closed.
083     */
084    boolean closed();
085}