Reputation: 745
First thing first, I am pretty new to the domain of asychronous processing. In my current project, we are using spring boot along with project reactor, specifically Eventbus, to do some asynchronous processing. Use of eventbus I guess would also make our system more scalable.
Till now, the use of EventBus has been pretty limited where we do some processing in an EventBus consumer which does not return something. The configiuration and example processor are as follows:
//Config File
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private EventBus eventBus;
@Autowired
private BatchProcessor batchProcessor;
@Override
public void run(String... arg0) throws Exception {
eventBus.on("batchProcessor", batchProcessor);
}
}
//Consumer
@Service
public class BatchProcesspr implements Consumer<Event<Request>> {
@Override
public void accept(Event<Request> event) {
// processing goes here
}
Till now, this was fine with the accept method having a void return type. But, now I have a scenario where I would like to return a response from the processor method, or if an error occurs while processing need to throw an appropriate exception and in either case, the response/exception needs to be returned to the point of invocation.
Can this be done using reactor? if yes, please provide an easy example for this. I have read about Promise but cannot find an example similar to my case.
Upvotes: 0
Views: 1130
Reputation: 11993
Did you try sendAndReceive? http://projectreactor.io/ext/docs/reference/#bus-request-reply
EventBus bus;
bus.receive($("job.sink"), (Event<String> ev) -> {
return ev.getData().toUpperCase();
});
bus.sendAndReceive(
"job.sink",
Event.wrap("Hello World!"),
s -> System.out.printf("Got %s on thread %s%n", s, Thread.currentThread())
);
You can easily register another consumer on the caller side that is notified when the service responds.
Upvotes: 0