Class RMQSource<OUT>
- java.lang.Object
-
- org.apache.flink.api.common.functions.AbstractRichFunction
-
- org.apache.flink.streaming.api.functions.source.RichSourceFunction<Type>
-
- Type Parameters:
OUT- The type of the data read from RabbitMQ.
- All Implemented Interfaces:
Serializable,org.apache.flink.api.common.functions.Function,org.apache.flink.api.common.functions.RichFunction,org.apache.flink.api.common.state.CheckpointListener,org.apache.flink.api.java.typeutils.ResultTypeQueryable<OUT>,org.apache.flink.streaming.api.checkpoint.CheckpointedFunction,org.apache.flink.streaming.api.functions.source.SourceFunction<OUT>
public class RMQSource<OUT> extends org.apache.flink.streaming.api.functions.source.MultipleIdsMessageAcknowledgingSourceBase<OUT,String,Long> implements org.apache.flink.api.java.typeutils.ResultTypeQueryable<OUT>
RabbitMQ source (consumer) which reads from a queue and acknowledges messages on checkpoints. When checkpointing is enabled, it guarantees exactly-once processing semantics.RabbitMQ requires messages to be acknowledged. On failures, RabbitMQ will re-resend all messages which have not been acknowledged previously. When a failure occurs directly after a completed checkpoint, all messages part of this checkpoint might be processed again because they couldn't be acknowledged before failure. This case is handled by the
MessageAcknowledgingSourceBasebase class which deduplicates the messages using the correlation id.RabbitMQ's Delivery Tags do NOT represent unique ids / offsets. That's why the source uses the Correlation ID in the message properties to check for duplicate messages. Note that the correlation id has to be set at the producer. If the correlation id is not set, messages may be produced more than once in corner cases.
This source can be operated in three different modes:
1) Exactly-once (when checkpointed) with RabbitMQ transactions and messages with unique correlation IDs. 2) At-least-once (when checkpointed) with RabbitMQ transactions but no deduplication mechanism (correlation id is not set). 3) No strong delivery guarantees (without checkpointing) with RabbitMQ auto-commit mode.
Users may overwrite the setupConnectionFactory() method to pass their setup their own ConnectionFactory in case the constructor parameters are not sufficient.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.flink.streaming.api.functions.source.SourceFunction
org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T extends Object>
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanautoAckprotected com.rabbitmq.client.Channelchannelprotected com.rabbitmq.client.Connectionconnectionprotected org.apache.flink.streaming.connectors.rabbitmq.QueueingConsumerconsumerprotected RMQDeserializationSchema<OUT>deliveryDeserializerprotected StringqueueName
-
Constructor Summary
Constructors Constructor Description RMQSource(RMQConnectionConfig rmqConnectionConfig, String queueName, boolean usesCorrelationId, org.apache.flink.api.common.serialization.DeserializationSchema<OUT> deserializationSchema)Creates a new RabbitMQ source.RMQSource(RMQConnectionConfig rmqConnectionConfig, String queueName, boolean usesCorrelationId, RMQDeserializationSchema<OUT> deliveryDeserializer)Creates a new RabbitMQ source.RMQSource(RMQConnectionConfig rmqConnectionConfig, String queueName, org.apache.flink.api.common.serialization.DeserializationSchema<OUT> deserializationSchema)Creates a new RabbitMQ source with at-least-once message processing guarantee when checkpointing is enabled.RMQSource(RMQConnectionConfig rmqConnectionConfig, String queueName, RMQDeserializationSchema<OUT> deliveryDeserializer)Creates a new RabbitMQ source with at-least-once message processing guarantee when checkpointing is enabled.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidacknowledgeSessionIDs(List<Long> sessionIds)voidcancel()voidclose()org.apache.flink.api.common.typeinfo.TypeInformation<OUT>getProducedType()voidopen(org.apache.flink.configuration.Configuration config)voidrun(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<OUT> ctx)protected com.rabbitmq.client.ConnectionsetupConnection()Initializes the connection to RMQ using the default connection factory fromsetupConnectionFactory().protected com.rabbitmq.client.ConnectionFactorysetupConnectionFactory()Initializes the connection to RMQ with a default connection factory.protected voidsetupQueue()Sets up the queue.-
Methods inherited from class org.apache.flink.streaming.api.functions.source.MultipleIdsMessageAcknowledgingSourceBase
acknowledgeIDs, snapshotState
-
Methods inherited from class org.apache.flink.streaming.api.functions.source.MessageAcknowledgingSourceBase
addId, initializeState, notifyCheckpointAborted, notifyCheckpointComplete
-
-
-
-
Field Detail
-
queueName
protected final String queueName
-
deliveryDeserializer
protected RMQDeserializationSchema<OUT> deliveryDeserializer
-
connection
protected transient com.rabbitmq.client.Connection connection
-
channel
protected transient com.rabbitmq.client.Channel channel
-
consumer
protected transient org.apache.flink.streaming.connectors.rabbitmq.QueueingConsumer consumer
-
autoAck
protected transient boolean autoAck
-
-
Constructor Detail
-
RMQSource
public RMQSource(RMQConnectionConfig rmqConnectionConfig, String queueName, org.apache.flink.api.common.serialization.DeserializationSchema<OUT> deserializationSchema)
Creates a new RabbitMQ source with at-least-once message processing guarantee when checkpointing is enabled. No strong delivery guarantees when checkpointing is disabled.For exactly-once, please use the constructor
RMQSource(RMQConnectionConfig, String, boolean, DeserializationSchema).- Parameters:
rmqConnectionConfig- The RabbiMQ connection configurationRMQConnectionConfig.queueName- The queue to receive messages from.deserializationSchema- ADeserializationSchemafor turning the bytes received into Java objects.
-
RMQSource
public RMQSource(RMQConnectionConfig rmqConnectionConfig, String queueName, boolean usesCorrelationId, org.apache.flink.api.common.serialization.DeserializationSchema<OUT> deserializationSchema)
Creates a new RabbitMQ source. For exactly-once, you must set the correlation ids of messages at the producer. The correlation id must be unique. Otherwise the behavior of the source is undefined. If in doubt, set usesCorrelationId to false. When correlation ids are not used, this source has at-least-once processing semantics when checkpointing is enabled.- Parameters:
rmqConnectionConfig- The RabbiMQ connection configurationRMQConnectionConfig.queueName- The queue to receive messages from.usesCorrelationId- Whether the messages received are supplied with a unique id to deduplicate messages (in case of failed acknowledgments). Only used when checkpointing is enabled.deserializationSchema- ADeserializationSchemafor turning the bytes received into Java objects.
-
RMQSource
public RMQSource(RMQConnectionConfig rmqConnectionConfig, String queueName, RMQDeserializationSchema<OUT> deliveryDeserializer)
Creates a new RabbitMQ source with at-least-once message processing guarantee when checkpointing is enabled. No strong delivery guarantees when checkpointing is disabled.For exactly-once, please use the constructor
RMQSource(RMQConnectionConfig, String, boolean, RMQDeserializationSchema).It also uses the provided
RMQDeserializationSchemato parse both the correlationID and the message.- Parameters:
rmqConnectionConfig- The RabbiMQ connection configurationRMQConnectionConfig.queueName- The queue to receive messages from.deliveryDeserializer- ARMQDeserializationSchemafor parsing the RMQDelivery.
-
RMQSource
public RMQSource(RMQConnectionConfig rmqConnectionConfig, String queueName, boolean usesCorrelationId, RMQDeserializationSchema<OUT> deliveryDeserializer)
Creates a new RabbitMQ source. For exactly-once, you must set the correlation ids of messages at the producer. The correlation id must be unique. Otherwise the behavior of the source is undefined. If in doubt, set usesCorrelationId to false. When correlation ids are not used, this source has at-least-once processing semantics when checkpointing is enabled.It also uses the provided
RMQDeserializationSchemato parse both the correlationID and the message.- Parameters:
rmqConnectionConfig- The RabbiMQ connection configurationRMQConnectionConfig.queueName- The queue to receive messages from.usesCorrelationId- Whether the messages received are supplied with a unique id to deduplicate messages (in case of failed acknowledgments). Only used when checkpointing is enabled.deliveryDeserializer- ARMQDeserializationSchemafor parsing the RMQDelivery.
-
-
Method Detail
-
setupConnectionFactory
protected com.rabbitmq.client.ConnectionFactory setupConnectionFactory() throws ExceptionInitializes the connection to RMQ with a default connection factory. The user may override this method to setup and configure their ownConnectionFactory.- Throws:
Exception
-
setupConnection
@VisibleForTesting protected com.rabbitmq.client.Connection setupConnection() throws ExceptionInitializes the connection to RMQ using the default connection factory fromsetupConnectionFactory(). The user may override this method to setup and configure their ownConnection.- Throws:
Exception
-
setupQueue
@VisibleForTesting protected void setupQueue() throws IOExceptionSets up the queue. The default implementation just declares the queue. The user may override this method to have a custom setup for the queue (i.e. binding the queue to an exchange or defining custom queue parameters)- Throws:
IOException
-
open
public void open(org.apache.flink.configuration.Configuration config) throws Exception
-
close
public void close() throws Exception
-
run
public void run(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<OUT> ctx) throws Exception
-
cancel
public void cancel()
- Specified by:
cancelin interfaceorg.apache.flink.streaming.api.functions.source.SourceFunction<OUT>
-
-