Spring Integration Kafka - 发送基本字符串

时间:2015-09-03 06:26:54

标签: spring-integration apache-kafka

我正在尝试使用Spring Integration Kafka v1.2.1发送基本的字符串有效负载,但它失败并出现以下异常:

2015-09-03 11:50:39.729 ERROR 14418 --- [task-executor-3] [                                    ] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.integration.kafka.outbound.KafkaProducerMessageHandler#0]; nested exception is org.apache.kafka.common.errors.SerializationException: Can't convert value of class [B to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:84)
at org.springframework.integration.endpoint.PollingConsumer.handleMessage(PollingConsumer.java:74)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:219)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:55)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:149)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:146)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:298)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.kafka.common.errors.SerializationException: Can't convert value of class [B to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer

我的配置XML如下所示:

             

<task:executor id="schedule-request-task-executor" pool-size="5" keep-alive="120" queue-capacity="125"/>

<bean id="kafkaStringSerializer" class="org.apache.kafka.common.serialization.StringSerializer"/>

<int-kafka:producer-context id="schedule-request-producer-context">
    <int-kafka:producer-configurations>
        <int-kafka:producer-configuration topic="schedule.requests"
                                          key-serializer="kafkaStringSerializer"
                                          value-serializer="kafkaStringSerializer"
                                          broker-list="${kafka.brokers}"/>
    </int-kafka:producer-configurations>
</int-kafka:producer-context>

<int-kafka:outbound-channel-adapter
        kafka-producer-context-ref="schedule-request-producer-context"
        channel="schedule-request-channel">
    <int:poller receive-timeout="0"
                fixed-delay="100" time-unit="MILLISECONDS"
                task-executor="schedule-request-task-executor"/>
</int-kafka:outbound-channel-adapter>

我发送的邮件中包含以下代码:

Message message = MessageBuilder.withPayload("PAYLOAD")
                .setHeader("messageKey", "KEY")
                .setHeader("topic", "schedule.requests")
                .build();
scheduleRequestChannel.send(message);

我查看了https://github.com/spring-projects/spring-integration-extensions/blob/master/samples/处的样本,但这些似乎已经过时了。

1 个答案:

答案 0 :(得分:2)

在调试SI和Kafka类之后,我发现这是因为Spring Integration将String转换为byte [],除非在Producer Configuration中指定了key-class-typevalue-class-type

如果有人有兴趣,请点击此处更新的配置。

<int-kafka:producer-context id="schedule-request-producer-context">
        <int-kafka:producer-configurations>
            <int-kafka:producer-configuration topic="schedule.requests"
                                              key-class-type="java.lang.String"
                                              key-serializer="kafkaStringSerializer"
                                              value-class-type="java.lang.String"
                                              value-serializer="kafkaStringSerializer"
                                              broker-list="${kafka.brokers}"/>