0

I am trying to migrate some of my code from XML into java dsl style (pre-java8).

This is the java config i created, i am not able to figure out how to set the poller. The examples only talk about global poller, but i need to set the poller within the adapter in my case.

  @Bean
  public MessageHandler kafkaMessageHandler() {
    KafkaProducerMessageHandler<String, String> handler =
        new KafkaProducerMessageHandler<>(kafkaTemplate());
    handler.setMessageKeyExpression(new LiteralExpression("kafka-integration"));
    handler.setTopicExpression(new LiteralExpression("headers.kafka_topic"));
    return handler;
  }

  @Bean
  public KafkaTemplate<String, String> kafkaTemplate() {
    return new KafkaTemplate<>(new DefaultKafkaProducerFactory<String, String>(producerConfigs()));
  }


  @Bean
  public Map<String, Object> producerConfigs() {
    Map<String, Object> properties = new HashMap<>();
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
    // introduce a delay on the send to allow more messages to accumulate
    properties.put(ProducerConfig.LINGER_MS_CONFIG, 1);
    return properties;
  }

The XML equivalent i have is the following :

<int-kafka:outbound-channel-adapter
    id="kafkaOutboundChannelAdapter"
    kafka-producer-context-ref="kafkaProducerContext"
    channel="kafkaChannel" >
  <int:poller fixed-rate="1000" max-messages-per-poll="10000}"/>
</int-kafka:outbound-channel-adapter>
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Vidhya
  • 57
  • 8

1 Answers1

1

See the documentation...

@Bean
@ServiceActivator(inputChannel = "kafkaChannel" poller = @Poller(fixedDelay = "1000", ...)
public MessageHandler kafkaMessageHandler() {
    ...
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179