All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reactor.groovy.config.StreamRouter Maven / Gradle / Ivy

There is a newer version: 2.0.8.RELEASE
Show newest version
package reactor.groovy.config;


import org.reactivestreams.Processor;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.bus.Event;
import reactor.bus.filter.Filter;
import reactor.bus.registry.Registration;
import reactor.bus.registry.Registry;
import reactor.bus.routing.ConsumerFilteringRouter;
import reactor.fn.Consumer;

import java.util.List;

/**
 * @author Stephane Maldini
 */
public class StreamRouter extends ConsumerFilteringRouter {

	private final Registry, Event>> processorRegistry;

	public StreamRouter(Filter filter,
	                    Registry, Event>> processorRegistry) {
		super(filter);
		this.processorRegistry = processorRegistry;
	}

	@Override
	@SuppressWarnings("unchecked")
	public > void route(final Object key, final E event,
	                      final List>>> consumers,
	                      final Consumer completionConsumer,
	                      final Consumer errorConsumer) {

		Processor, Event> processor;
		for (Registration, Event>> registration : processorRegistry.select(key)){
			processor = registration.getObject();
			processor.onNext(event);
			processor.subscribe(new Subscriber>() {
				@Override
				public void onSubscribe(Subscription subscription) {
					subscription.request(Integer.MAX_VALUE);
				}

				@Override
				public void onNext(Event hydratedEvent) {
					StreamRouter.super.route(hydratedEvent.getKey(), (E) hydratedEvent, consumers, completionConsumer,
							errorConsumer);
				}

				@Override
				public void onComplete() {

				}

				@Override
				public void onError(Throwable cause) {
					 event.consumeError(cause);
				}
			});
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy