Java 使用Spring云流源将方法结果发送到流

Java 使用Spring云流源将方法结果发送到流,java,spring-boot,spring-cloud-stream,Java,Spring Boot,Spring Cloud Stream,我试图在Spring引导应用程序中创建一个Spring云流源Bean,它只将方法的结果发送到流(底层Kafka主题绑定到流) 我看到的大多数流示例都使用@InboundChannelAdapter注释,使用轮询器向流发送数据。但我不想用民意测验仪。我尝试将轮询器设置为空数组,但另一个问题是,当使用@InboundChannelAdapter时,您无法获得任何方法参数 我试图做的事情的总体概念是从入站流中读取。执行一些异步处理,然后将结果发布到出站流。因此,使用处理器似乎也不是一种选择。我正在使用

我试图在Spring引导应用程序中创建一个Spring云流源Bean,它只将方法的结果发送到流(底层Kafka主题绑定到流)

我看到的大多数流示例都使用
@InboundChannelAdapter
注释,使用轮询器向流发送数据。但我不想用民意测验仪。我尝试将轮询器设置为空数组,但另一个问题是,当使用@InboundChannelAdapter时,您无法获得任何方法参数

我试图做的事情的总体概念是从入站流中读取。执行一些异步处理,然后将结果发布到出站流。因此,使用处理器似乎也不是一种选择。我正在使用带有接收通道的
@StreamListener
来读取入站流,这很有效

这是我一直在尝试的一些代码,但根本不起作用。我希望它会这么简单,因为我的水槽是,但也许不是。希望有人给我指出一个不是处理器(即不需要监听入站频道)且不使用
@InboundChannelAdapter
的源代码示例,或者给我一些设计技巧,以不同的方式完成我需要做的事情。谢谢

@EnableBinding(Source.class)
public class JobForwarder {

   @ServiceActivator(outputChannel = Source.OUTPUT)
   @SendTo(Source.OUTPUT)
   public String forwardJob(String message) {
       log.info(String.format("Forwarding a job message [%s] to queue [%s]", message, Source.OUTPUT));
       return message;
   }
}

那么,如果您不想使用轮询器,是什么导致调用
forwardJob()
方法的呢

您不能仅仅调用该方法并期望结果进入输出通道

使用当前配置,您需要在包含入站消息的服务上使用
inputChannel
(以及向该通道发送消息的内容)。它不必绑定到运输工具上;它可以是一个简单的
MessageChannel
@Bean

或者,您可以使用
@Publisher
发布方法调用的结果(以及返回给调用方的结果)-


谢谢你的意见。我花了一段时间才回到这个问题上来。我确实试过阅读
@Publisher
的文档。它看起来正是我所需要的,但我只是无法得到正确的bean初始化,以使其正确连接

为了回答您的问题,在对输入进行异步处理后,将调用
forwardJob()
方法

最终,我只是直接使用
springkafka
库来实现,这更加明确,而且感觉更容易开始。我认为我们将坚持卡夫卡作为唯一的通道绑定,因此我认为我们将坚持使用该库

然而,我们最终让SpringCloudStream库非常简单地工作。这是一个没有轮询器的单一源代码

@Component
@EnableBinding(Source.class)
public class JobForwarder {

    private Source source;

    @Autowired
    public ScheduledJobForwarder(Source source) {
        this.source = source;
    }

    public void forwardScheduledJob(String message) {
        log.info(String.format("Forwarding a job message [%s] to queue [%s]", message, Source.OUTPUT));
        source.output().send(MessageBuilder.withPayload(message).build());
    }
}

您的原始要求可以通过以下步骤实现

  • 创建自定义绑定接口(也可以使用默认的
    @EnableBinding(Source.class)

  • 注入绑定通道

    @Component
    @EnableBinding(CustomSource.class)
    public class CustomOutputEventSource {
    
        @Autowired
        private CustomSource customSource;
    
        public void sendMessage(String message) {
            customSource.output().send(MessageBuilder.withPayload(message).build());
        }
    }
    
  • 测试一下

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class CustomOutputEventSourceTest {
    
        @Autowired
        CustomOutputEventSource output;
    
        @Test
        public void sendMessage() {
            output.sendMessage("Test message from JUnit test");
        }
    }
    
  • @Component
    @EnableBinding(CustomSource.class)
    public class CustomOutputEventSource {
    
        @Autowired
        private CustomSource customSource;
    
        public void sendMessage(String message) {
            customSource.output().send(MessageBuilder.withPayload(message).build());
        }
    }
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class CustomOutputEventSourceTest {
    
        @Autowired
        CustomOutputEventSource output;
    
        @Test
        public void sendMessage() {
            output.sendMessage("Test message from JUnit test");
        }
    }