Message Listeners

当你使用 message listener container 时,你必须提供一个侦听器来接收数据。当前有八个受支持的邮件侦听器接口。如下清单所示这些接口:

When you use a message listener container, you must provide a listener to receive data. There are currently eight supported interfaces for message listeners. The following listing shows these interfaces:

public interface MessageListener<K, V> { 1

    void onMessage(ConsumerRecord<K, V> data);

}

public interface AcknowledgingMessageListener<K, V> { 2

    void onMessage(ConsumerRecord<K, V> data, Acknowledgment acknowledgment);

}

public interface ConsumerAwareMessageListener<K, V> extends MessageListener<K, V> { 3

    void onMessage(ConsumerRecord<K, V> data, Consumer<?, ?> consumer);

}

public interface AcknowledgingConsumerAwareMessageListener<K, V> extends MessageListener<K, V> { 4

    void onMessage(ConsumerRecord<K, V> data, Acknowledgment acknowledgment, Consumer<?, ?> consumer);

}

public interface BatchMessageListener<K, V> { 5

    void onMessage(List<ConsumerRecord<K, V>> data);

}

public interface BatchAcknowledgingMessageListener<K, V> { 6

    void onMessage(List<ConsumerRecord<K, V>> data, Acknowledgment acknowledgment);

}

public interface BatchConsumerAwareMessageListener<K, V> extends BatchMessageListener<K, V> { 7

    void onMessage(List<ConsumerRecord<K, V>> data, Consumer<?, ?> consumer);

}

public interface BatchAcknowledgingConsumerAwareMessageListener<K, V> extends BatchMessageListener<K, V> { 8

    void onMessage(List<ConsumerRecord<K, V>> data, Acknowledgment acknowledgment, Consumer<?, ?> consumer);

}
1 在使用自动提交或某一个容器管理的 commit methods 时,使用此接口处理从 Kafka 消费者 poll() 操作接收的各个 ConsumerRecord 实例。
2 Use this interface for processing individual ConsumerRecord instances received from the Kafka consumer poll() operation when using auto-commit or one of the container-managed commit methods.
3 使用此接口处理在使用某一个手动 commit methods 时从 Kafka 消费者 poll() 操作接收的各个 ConsumerRecord 实例。
4 Use this interface for processing individual ConsumerRecord instances received from the Kafka consumer poll() operation when using one of the manual commit methods.
5 在使用自动提交或某一个容器管理的 commit methods 时,使用此接口处理从 Kafka 消费者 poll() 操作接收的各个 ConsumerRecord 实例。访问 Consumer 对象。
6 Use this interface for processing individual ConsumerRecord instances received from the Kafka consumer poll() operation when using auto-commit or one of the container-managed commit methods. Access to the Consumer object is provided.
7 使用此接口处理在使用某一个手动 commit methods 时从 Kafka 消费者 poll() 操作接收的各个 ConsumerRecord 实例。访问 Consumer 对象。
8 Use this interface for processing individual ConsumerRecord instances received from the Kafka consumer poll() operation when using one of the manual commit methods. Access to the Consumer object is provided.
9 在使用自动提交或某一个容器管理的 commit methods 时,使用此接口处理从 Kafka 消费者 poll() 操作接收的所有 ConsumerRecord 实例。由于侦听器提供完整批处理,因此在使用此接口时不受支持 AckMode.RECORD
10 Use this interface for processing all ConsumerRecord instances received from the Kafka consumer poll() operation when using auto-commit or one of the container-managed commit methods. AckMode.RECORD is not supported when you use this interface, since the listener is given the complete batch.
11 在使用某一个手动 commit methods 时,使用此接口处理从 Kafka 消费者 poll() 操作接收的所有 ConsumerRecord 实例。
12 Use this interface for processing all ConsumerRecord instances received from the Kafka consumer poll() operation when using one of the manual commit methods.
13 在使用自动提交或某一个容器管理的 commit methods 时,使用此接口处理从 Kafka 消费者 poll() 操作接收的所有 ConsumerRecord 实例。由于侦听器提供完整批处理,因此在使用此接口时不受支持 AckMode.RECORD。访问 Consumer 对象。
14 Use this interface for processing all ConsumerRecord instances received from the Kafka consumer poll() operation when using auto-commit or one of the container-managed commit methods. AckMode.RECORD is not supported when you use this interface, since the listener is given the complete batch. Access to the Consumer object is provided.
15 使用此接口处理在使用某一个手动 commit methods 时从 Kafka 消费者 poll() 操作接收的所有 ConsumerRecord 实例。访问 Consumer 对象。
16 Use this interface for processing all ConsumerRecord instances received from the Kafka consumer poll() operation when using one of the manual commit methods. Access to the Consumer object is provided.

Consumer 对象不是线程安全的。您必须仅在其调用侦听器的线程中调用其方法。

The Consumer object is not thread-safe. You must only invoke its methods on the thread that calls the listener.

您不应在侦听器中执行任何影响消费者位置或已提交偏移量的 Consumer<?, ?> 方法;该容器需要管理此类信息。

You should not execute any Consumer<?, ?> methods that affect the consumer’s positions or committed offsets in your listener; the container needs to manage such information.