Spring云生产者异常-java.lang.IllegalStateException:生产者关闭后无法执行操作

Spring云生产者异常-java.lang.IllegalStateException:生产者关闭后无法执行操作,spring,apache-kafka,cloud,producer,Spring,Apache Kafka,Cloud,Producer,Spring云生产者引发异常-java.lang.IllegalStateException:生产者关闭后无法执行操作 嗨 我们给出了一个基于spring云的微服务应用程序。在该应用程序中,我们在向Kafka生成消息时出错。我们正在使用下面的Spring云版本 Spring云版本 格林威治 以下是卡夫卡制作人使用的接口定义 String INPUT = "jobmanager-in"; String OUTPUT_C = "collection-out"; String OUTPUT_P

Spring云生产者引发异常-java.lang.IllegalStateException:生产者关闭后无法执行操作

嗨 我们给出了一个基于spring云的微服务应用程序。在该应用程序中,我们在向Kafka生成消息时出错。我们正在使用下面的Spring云版本

Spring云版本

格林威治

以下是卡夫卡制作人使用的接口定义

    String INPUT = "jobmanager-in";
String OUTPUT_C = "collection-out";
String OUTPUT_P = "parser-out";
String OUTPUT_CMP = "compare-out";
String OUTPUT_R = "report-out";
String OUTPUT_N = "notification-out";

@Input(INPUT)
SubscribableChannel inboundJobManager();

@Output(OUTPUT_C)
    MessageChannel outboundCollections();

@Output(OUTPUT_P)
MessageChannel outboundParse();

@Output(OUTPUT_CMP)
MessageChannel outboundCompare();

@Output(OUTPUT_R)
MessageChannel outboundReport();

@Output(OUTPUT_N)
MessageChannel outboundNotification();
生产者代码是

public void sendCollectionTask(final MessageT message) {

        logger.info("Sending Collection Task :: " + message.toString());
        MessageChannel messageChannel = collectionStream.outboundCollections();
        boolean l_bool = messageChannel.send(MessageBuilder.withPayload(message)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON).build());
        logger.info("After sending message for Collection Task send status :: " + message.getTask().getId() + " : "
                + l_bool);
    }
当我们启动应用程序时,生产者会正确地生成消息,但一段时间后,我们会收到错误

2019-09-12 02:07:45,215 ERROR com.ericsson.tmo.cm.ccm.jobmanager.util.JobOrchestrator [scheduling-1] Error Trace ::
org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder$ProducerConfigurationMessageHandler@11328ab9]; nested exception is java.lang.IllegalStateException: Cannot perform operation after producer has been closed
        at org.springframework.integration.support.utils.IntegrationUtils.wrapInHandlingExceptionIfNecessary(IntegrationUtils.java:189)
        at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:179)
如果我们重新启动应用程序,问题就解决了。 谢谢你的帮助

我们的Spring云配置是

cloud:
    stream:
      kafka:
        binder:
          brokers: kafka
          autoCreateTopic: false
          configuration:
            auto.offset.reset: latest
            max.request.size: 16777216
            buffer.memory: 67108864
      bindings:
        collection-out:
          destination: collection
          contentType: application/json
          group: collection
          producer:
            partitionCount: 5
            autoAddPartitions: true

异常消息很清楚

生产者关闭后无法执行操作

您的生产者可能会被一个线程关闭,而生产者正在另一个线程上调用
send()
,或者生产者网络线程仍在发送消息


由于您使用的是Spring,我假设
KafkaProducer
对象是为您创建的,并通过依赖项注入注入

您需要做的就是找出谁使用了相同的
KafkaProducer
对象,以及调用
close()
的位置

可能的原因

  • 有时,同一个
    KafkaProducer
    可能被同一应用程序的多个其他组件使用(如果您没有在多个生产者之间进行划分),其中一个可能已经关闭了它

    更清楚的是,您可能正在使用同一个producerbean实例为同一实例中的不同组件生成消息,并且在某个地方它是关闭的

    通常,我们有不同的生产者为不同的主题制作,因为对于不同类型的数据,我们有不同的生产者配置和主题

  • 当我添加了一个关闭钩子时,我遇到了一个类似的异常,在这个钩子中,我调用了
    producer.close()
    ,而另一个线程正在尝试生成