rabbitmqspring-cloud-streamspring-cloud-function

Post separate messages in RabbitMQ for each element in a list returned by the producer function


I trying out spring-cloud-funtions, with RabbitMQ integration. So my producer fetches a list of elements with an IO operation (lets say a database call). Instead of the list being posted as one single message, I am trying to make make it post a bunch of separate messages to my exchange. This way my next processor (which consumes from the list) can pick up elements and process them separately. My publisher would look something like this:

    @Bean
    Supplier<List<Foo>> publisher() {
        return str -> fooDAO.findAll();
    }

And I would like my queue to be like:

    @Bean
    Consumer<Foo> consumer() {
        return foo -> barDAO.save(foo.getBar());
    }

Solution

  • Use the StreamBridge to send each message.

    streamBridge.send("toStream-out-0", someData);
    

    See https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#_sending_arbitrary_data_to_an_output_e_g_foreign_event_driven_sources