0

Currently I have Spring Boot application which is something like this.

@Component
@EnableBinding(Source::class)
class EmailMessageProducer(private val source: Source) {

    suspend fun send(textMessage: TextMessage) {

        source.output().send(
            MessageBuilder.withPayload(textMessage).setHeader("service", "test").build()
        )
    }
}

I would like to use Spring Cloud Function here using reactive pattern.

Furthermore, is my current solution non blocking? I am asking this because this is my first time using Kotlin coroutine in this context.

Java solution works for me as well since I am just trying to understand the concept here.

nicholasnet
  • 2,117
  • 2
  • 24
  • 46

1 Answers1

0

What you're looking for is a reactive Supplier (e.g., Supplier<Flux>).

In your case it would look something like this:

@SpringBootApplication
public class SomeApplication {

    @Bean
    public Supplier<Flux<Message<String>>> messageProducer() {
        return () -> Flux.just(MessageBuilder.withPayload(textMessage).setHeader("service", "test").build());
    }
}

Provide spring.cloud.function.definition=messageProducer property and this is pretty much it.

Obviously the above example produced a finite stream with a single item, but feel free to modify the returned flux. In fact we discuss this in more details here.

Oleg Zhurakousky
  • 5,820
  • 16
  • 17
  • How do I pass `textMessage` here? – nicholasnet Jan 13 '20 at 20:40
  • Pass? Pass to where? This is a Supplier - a producer of Messages. . . – Oleg Zhurakousky Jan 13 '20 at 20:49
  • I see what you mean but in my case I am producing message based on parameter given as you see in my example above. – nicholasnet Jan 13 '20 at 20:53
  • You can utilise `EmitterProcessor` for that. Please reference [this section](https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/3.0.1.RELEASE/reference/html/spring-cloud-stream.html#_foreign_event_driven_sources) for more details. – Oleg Zhurakousky Jan 14 '20 at 03:45
  • I did this `private val processor: EmitterProcessor = EmitterProcessor.create()` `@Bean fun supplier(): Supplier> { return Supplier { processor } }` and later `processor.onNext(textMessageBuilder.build())` however I do not see any message in RabbitMQ do I need to add anything in application.yml – nicholasnet Jan 14 '20 at 19:05
  • I don't know what does `and later` means in the context of your comment. The example I pointed out is complete and functioning. What you are showing is a fragment of something. Consider pushing your project (bare minimum, just enoughg to repropduce the issue) to GitHub so one of us can take a look. – Oleg Zhurakousky Jan 15 '20 at 05:19