Last active
March 8, 2020 21:29
-
-
Save danielkec/742b02393d0d965fecc3fe5513b6eee1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @ApplicationScoped | |
| public class MsgProcessingBean { | |
| private final EmittingPublisher<String> emittingPublisher = new EmittingPublisher<>(); | |
| private SseBroadcaster sseBroadcaster; | |
| @Outgoing("multiplyVariants") | |
| public Publisher<String> preparePublisher() { | |
| // Create new publisher for emitting to by this::process | |
| return ReactiveStreams | |
| .fromPublisher(emittingPublisher) | |
| .buildRs(); | |
| } | |
| @Incoming("multiplyVariants") | |
| @Outgoing("wrapSseEvent") | |
| public ProcessorBuilder<String, String> multiply() { | |
| // Multiply to 3 variants of same message | |
| return ReactiveStreams.<String>builder() | |
| .flatMap(o -> | |
| ReactiveStreams.of( | |
| // upper case variant | |
| o.toUpperCase(), | |
| // repeat twice variant | |
| o.repeat(2), | |
| // reverse chars 'tnairav' | |
| new StringBuilder(o).reverse().toString()) | |
| ); | |
| } | |
| @Incoming("wrapSseEvent") | |
| @Outgoing("broadcast") | |
| public OutboundSseEvent wrapSseEvent(String msg) { | |
| // Map every message to sse event | |
| return new OutboundEvent.Builder().data(msg).build(); | |
| } | |
| @Incoming("broadcast") | |
| public void broadcast(OutboundSseEvent sseEvent) { | |
| // Broadcast to all sse sinks | |
| this.sseBroadcaster.broadcast(sseEvent); | |
| } | |
| public void addSink(final SseEventSink eventSink, final Sse sse) { | |
| if (this.sseBroadcaster == null) { | |
| this.sseBroadcaster = sse.newBroadcaster(); | |
| } | |
| this.sseBroadcaster.register(eventSink); | |
| } | |
| public void process(final String msg) { | |
| emittingPublisher.emit(msg); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment