Skip to content

Commit 2ec0e32

Browse files
garyrussellartembilan
authored andcommitted
Container factory doc polishing
* Polishing - PR Comments
1 parent 1fa700a commit 2ec0e32

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

spring-kafka/src/main/java/org/springframework/kafka/config/ConcurrentKafkaListenerContainerFactory.java

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
* This factory is primarily for building containers for {@code KafkaListener} annotated
3333
* methods but can also be used to create any container.
3434
*
35+
* Only containers for {@code KafkaListener} annotated methods are added to the
36+
* {@code KafkaListenerEndpointRegistry}.
37+
*
3538
* @param <K> the key type.
3639
* @param <V> the value type.
3740
*

spring-kafka/src/main/java/org/springframework/kafka/config/KafkaListenerContainerFactory.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ public interface KafkaListenerContainerFactory<C extends MessageListenerContaine
3636

3737
/**
3838
* Create a {@link MessageListenerContainer} for the given {@link KafkaListenerEndpoint}.
39+
* Containers created using this method are added to the listener endpoint registry.
3940
* @param endpoint the endpoint to configure
4041
* @return the created container
4142
*/
4243
C createListenerContainer(KafkaListenerEndpoint endpoint);
4344

4445
/**
4546
* Create and configure a container without a listener; used to create containers that
46-
* are not used for KafkaListener annotations.
47+
* are not used for KafkaListener annotations. Containers created using this method
48+
* are not added to the listener endpoint registry.
4749
* @param topicPartitions the topicPartitions.
4850
* @return the container.
4951
* @since 2.2
@@ -52,7 +54,8 @@ public interface KafkaListenerContainerFactory<C extends MessageListenerContaine
5254

5355
/**
5456
* Create and configure a container without a listener; used to create containers that
55-
* are not used for KafkaListener annotations.
57+
* are not used for KafkaListener annotations. Containers created using this method
58+
* are not added to the listener endpoint registry.
5659
* @param topics the topics.
5760
* @return the container.
5861
* @since 2.2
@@ -61,7 +64,8 @@ public interface KafkaListenerContainerFactory<C extends MessageListenerContaine
6164

6265
/**
6366
* Create and configure a container without a listener; used to create containers that
64-
* are not used for KafkaListener annotations.
67+
* are not used for KafkaListener annotations. Containers created using this method
68+
* are not added to the listener endpoint registry.
6569
* @param topicPattern the topicPattern.
6670
* @return the container.
6771
* @since 2.2

src/reference/asciidoc/kafka.adoc

+17-1
Original file line numberDiff line numberDiff line change
@@ -1381,9 +1381,25 @@ To arbitrarily seek at runtime, use the callback reference from the `registerSee
13811381
As discussed in <<kafka-listener-annotation>> a `ConcurrentKafkaListenerContainerFactory` is used to create containers for annotated methods.
13821382

13831383
Starting with _version 2.2_, the same factory can be used to create any `ConcurrentMessageListenerContainer`.
1384-
This might be useful if you want to create several containers with similar properties, or you wish to use some exernally configured factory, such as the one provided by Spring Boot auto configuration.
1384+
This might be useful if you want to create several containers with similar properties, or you wish to use some externally configured factory, such as the one provided by Spring Boot auto configuration.
13851385
Once the container is created, you can further modify its properties, many of which are set by using `container.getContainerProperties()`.
13861386

1387+
[source, java]
1388+
----
1389+
@Bean
1390+
public ConcurrentMessageListenerContainer<String, String>(
1391+
ConcurrentKafkaListenerContainerFactory<String, String> factory) {
1392+
1393+
ConcurrentMessageListenerContainer<String, String> container =
1394+
factory.createContainer("topic1", "topci2");
1395+
container.setMessageListener(m -> { ... } );
1396+
return container;
1397+
}
1398+
----
1399+
1400+
IMPORTANT: Containers created this way are not added to the endpoint registry.
1401+
They should be created as `@Bean` s so that they will be registered with the application context.
1402+
13871403
[[pause-resume]]
13881404
==== Pausing/Resuming Listener Containers
13891405

src/reference/asciidoc/si-kafka.adoc

+23
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,26 @@ In most cases, this will be an `ErrorMessageSendingRecoverer` which will send th
277277
When building `ErrorMessage` (for use in the `error-channel` or `recovery-callback`), you can customize the error message using the `error-message-strategy` property.
278278
By default, a `RawRecordHeaderErrorMessageStrategy` is used; providing access to the converted message as well as the raw `ConsumerRecord`.
279279

280+
Starting with _Spring for Apache Kafka version 2.2_ (_Spring Integration Kafka 3.1_), the container factory used for `@KafkaListener` annotations can also be used to create `ConcurrentMessageListenerContainer` s for other purposes.
281+
See <<container-factory>> for an example.
282+
283+
With the Java DSL, the container does not have to be configured as a `@Bean` because the DSL will register the container as a bean.
284+
285+
[source, java]
286+
----
287+
@Bean
288+
public IntegrationFlow topic2ListenerFromKafkaFlow() {
289+
return IntegrationFlows
290+
.from(Kafka.messageDrivenChannelAdapter(kafkaListenerContainerFactory().createContainer(TEST_TOPIC2),
291+
KafkaMessageDrivenChannelAdapter.ListenerMode.record)
292+
.id("topic2Adapter"))
293+
...
294+
get();
295+
}
296+
----
297+
298+
Notice that, in this case, the adapter is given an `id` ("topic2Adapter"); the container will be registered in the application context with the name `topic2Adapter.container`.
299+
If the adapter does not have an `id` property, the container's bean name will be the container's fully qualified class name + `#n` where `n` is incremented for each container.
280300

281301
[[si-outbound-gateway]]
282302
==== Outbound Gateway
@@ -396,6 +416,9 @@ public IntegrationFlow serverGateway() {
396416

397417
XML configuration is not currently available for this component.
398418

419+
Starting with _Spring for Apache Kafka version 2.2_ (_Spring Integration Kafka 3.1_), the container factory used for `@KafkaListener` annotations can also be used to create `ConcurrentMessageListenerContainer` s for other purposes.
420+
See <<container-factory>> and <<si-inbound>> for examples.
421+
399422
[[message-conversion]]
400423
==== Message Conversion
401424

0 commit comments

Comments
 (0)