|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.rocketmq.client.apis.consumer; |
| 19 | + |
| 20 | +import java.io.Closeable; |
| 21 | +import java.io.IOException; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Optional; |
| 26 | +import org.apache.rocketmq.client.apis.ClientException; |
| 27 | +import org.apache.rocketmq.client.apis.message.MessageQueue; |
| 28 | +import org.apache.rocketmq.client.apis.message.MessageView; |
| 29 | + |
| 30 | +public interface PullConsumer extends Closeable { |
| 31 | + /** |
| 32 | + * Get the consumer group of the consumer. |
| 33 | + */ |
| 34 | + String getConsumerGroup(); |
| 35 | + |
| 36 | + /** |
| 37 | + * @param topic the topic that needs to be monitored. |
| 38 | + * @param listener the callback to detect the message queue changes. |
| 39 | + */ |
| 40 | + void registerMessageQueueChangeListenerByTopic(String topic, TopicMessageQueueChangeListener listener); |
| 41 | + |
| 42 | + /** |
| 43 | + * Fetch message queues of the topic. |
| 44 | + */ |
| 45 | + Collection<MessageQueue> fetchMessageQueues(String topic) throws ClientException; |
| 46 | + |
| 47 | + /** |
| 48 | + * Manually assign a list of message queues to this consumer. |
| 49 | + * |
| 50 | + * <p>This interface does not allow for incremental assignment and will replace the previous assignment (if |
| 51 | + * previous assignment existed). |
| 52 | + * |
| 53 | + * @param messageQueues the list of message queues that are to be assigned to this consumer. |
| 54 | + */ |
| 55 | + void assign(Collection<MessageQueue> messageQueues); |
| 56 | + |
| 57 | + /** |
| 58 | + * Fetch messages from assigned message queues specified by {@link #assign(Collection)}. |
| 59 | + * |
| 60 | + * @param timeout the maximum time to block. |
| 61 | + * @return list of fetched messages. |
| 62 | + */ |
| 63 | + List<MessageView> poll(Duration timeout); |
| 64 | + |
| 65 | + /** |
| 66 | + * Overrides the fetch offsets that the consumer will use on the next poll. If this method is invoked for the same |
| 67 | + * message queue more than once, the latest offset will be used on the next {@link #poll(Duration)}. |
| 68 | + * |
| 69 | + * @param messageQueue the message queue to override the fetch offset. |
| 70 | + * @param offset message offset. |
| 71 | + */ |
| 72 | + void seek(MessageQueue messageQueue, long offset); |
| 73 | + |
| 74 | + /** |
| 75 | + * Suspending message pulling from the message queues. |
| 76 | + * |
| 77 | + * @param messageQueues message queues that need to be suspended. |
| 78 | + */ |
| 79 | + void pause(Collection<MessageQueue> messageQueues); |
| 80 | + |
| 81 | + /** |
| 82 | + * Resuming message pulling from the message queues. |
| 83 | + * |
| 84 | + * @param messageQueues message queues that need to be resumed. |
| 85 | + */ |
| 86 | + void resume(Collection<MessageQueue> messageQueues); |
| 87 | + |
| 88 | + /** |
| 89 | + * Look up the offsets for the given message queue by timestamp. The returned offset for each message queue is the |
| 90 | + * earliest offset whose timestamp is greater than or equal to the given timestamp in the corresponding message |
| 91 | + * queue. |
| 92 | + * |
| 93 | + * @param messageQueue message queue that needs to be looked up. |
| 94 | + * @param timestamp the timestamp for which to search. |
| 95 | + * @return the offset of the message queue, or {@link Optional#empty()} if there is no message. |
| 96 | + */ |
| 97 | + Optional<Long> offsetForTimestamp(MessageQueue messageQueue, Long timestamp); |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the latest committed offset for the given message queue. |
| 101 | + * |
| 102 | + * @return the latest committed offset, or {@link Optional#empty()} if there was no prior commit. |
| 103 | + */ |
| 104 | + Optional<Long> committed(MessageQueue messageQueue); |
| 105 | + |
| 106 | + /** |
| 107 | + * Commit offset manually. |
| 108 | + */ |
| 109 | + void commit() throws ClientException; |
| 110 | + |
| 111 | + /** |
| 112 | + * Overrides the fetch offsets with the beginning offset that the consumer will use on the next poll. If this |
| 113 | + * method is invoked for the same message queue more than once, the latest offset will be used on the next |
| 114 | + * {@link #poll(Duration)}. |
| 115 | + * |
| 116 | + * @param messageQueue the message queue to seek. |
| 117 | + */ |
| 118 | + void seekToBegin(MessageQueue messageQueue) throws ClientException; |
| 119 | + |
| 120 | + /** |
| 121 | + * Overrides the fetch offsets with the end offset that the consumer will use on the next poll. If this method is |
| 122 | + * invoked for the same message queue more than once, the latest offset will be used on the next |
| 123 | + * {@link #poll(Duration)}. |
| 124 | + * |
| 125 | + * @param messageQueue the message queue to seek. |
| 126 | + */ |
| 127 | + void seekToEnd(MessageQueue messageQueue) throws ClientException; |
| 128 | + |
| 129 | + /** |
| 130 | + * Close the pull consumer and release all related resources. |
| 131 | + */ |
| 132 | + @Override |
| 133 | + void close() throws IOException; |
| 134 | +} |
0 commit comments