public abstract class BufferingSubscriber extends Object implements Subscriber
Subscriber
that provides a base subscriber that buffers a fixed number of messages read.
See maxUncommittedMessages
.
It provides implementations of Subscriber.commit(String)
, Subscriber.fail(String)
and
Subscriber.receive()
that honor the fixed number of messages to read.
This class is intended to be used if your PubSub implementation does not care about (or cannot be) using commit and
fail to reprocess messages from the PubSub and prefers to manage it in code.Modifier and Type | Field and Description |
---|---|
protected int |
maxUncommittedMessages
The maximum number of PubSubMessages we can have unacked at any time.
|
protected List<PubSubMessage> |
receivedMessages
A List of messages read.
|
protected Map<String,PubSubMessage> |
uncommittedMessages
A Map of messages that have not been committed so far.
|
Constructor and Description |
---|
BufferingSubscriber(int maxUncommittedMessages)
Creates an instance of this class with the given max for committed messages.
|
Modifier and Type | Method and Description |
---|---|
void |
commit(String id)
Commits allow clients to implement at least once, at most once or exactly once semantics when processing messages.
|
void |
fail(String id)
Marks the processing of the
PubSubMessage with the given id as failed. |
protected abstract List<PubSubMessage> |
getMessages()
Implement this method to read and return a
List of PubSubMessage from your actual PubSub source. |
protected boolean |
haveMessages()
Returns true if we already have messages to emit or if
getMessages() returns non-null
and non-empty List of messages. |
PubSubMessage |
receive()
Gets a new
PubSubMessage from the assigned partition/partitions (Here a partition is a unit of
parallelism in the Pub/Sub queue, See PubSub ). |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
close
protected final int maxUncommittedMessages
protected List<PubSubMessage> receivedMessages
receive()
emits from the head.protected Map<String,PubSubMessage> uncommittedMessages
public BufferingSubscriber(int maxUncommittedMessages)
maxUncommittedMessages
- The maximum number of messages that this Subscriber will buffer.public PubSubMessage receive() throws PubSubException
Subscriber
PubSubMessage
from the assigned partition/partitions (Here a partition is a unit of
parallelism in the Pub/Sub queue, See PubSub
).receive
in interface Subscriber
PubSubMessage
.PubSubException
- when a receive fails.public void commit(String id)
maxUncommittedMessages
uncommited messages, further calls to receive()
will return
nulls till some messages are committed.commit
in interface Subscriber
id
- The ID of the message to be marked as committed.public void fail(String id)
PubSubMessage
with the given id as failed.
Marks a message denoted by the id and sequence to have failed. This message is added for emission and will be
emitted on the next receive()
.fail
in interface Subscriber
id
- The ID of the PubSubMessage to mark as a processing failure.protected boolean haveMessages() throws PubSubException
getMessages()
returns non-null
and non-empty List
of messages. Otherwise, returns false.PubSubException
- if there was an issue reading the messages.protected abstract List<PubSubMessage> getMessages() throws PubSubException
List
of PubSubMessage
from your actual PubSub source.List
of PubSubMessage
if any were read. A null or an empty list if not.PubSubException
- if there was an issue reading the messages.Copyright © 2021. All rights reserved.