-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
Each Reactor
you create needs a Dispatcher
to execute tasks. By default, with no configuration, you’ll get a synchronous Dispatcher. This works fine for testing but is probably not what you want for a real application.
Since it’s not desirable to create too many threads in an asynchronous application and something has to keep track of those few Dispatchers that are divvyed out to the components that need them, you need to instaniate an Environment
which will create those Dispatchers based on either the default configuration (provided in a properties file in the Reactor JAR file) or by providing your own configuration.
There’s no magic to it. You simply "new" up an instance of Environment
and, when creating Reactors, Streams, and Promises, pass a reference to this Environment into the Specs (essentially a "builder" helper class). The Environment instance is where the reactor.
system properties live and it’s also the place where the small number of Dispatchers that are intended to be used by any component in your application that needs one reside.
You can, of course, create Dispatchers directly in your code. There may be times—like embedding in other threading models—where that’s desirable. But in general, you should refrain from directly instantiating your own Dispatchers and instead use those configured to be created by the Environment
.
Three of the most foundational components in Reactor’s reactor-core
module are the Selector
, the Consumer
, and the Event
. A Consumer
can be assigned to a Reactor
by using a Selector
, which is a simple abstraction to provide flexibility when finding the Consumer`s to invoke for an `Event
. A range of default selectors are available. From plain `String`s to regular expressions to Spring MVC-style URL templates.
There are different kinds of Selector`s for doing different kinds of matching. The simplest form is just to match one object with another. For example, a `Selector
created from a String
"parse" will match another Selector
whose wrapped object is also a String
"parse" (in this case it’s just like a String.equals(String)
.
But a Selector
can also match another Selector
based on Class.isAssignableFrom(Class<?>)
, regular expressions, URL templates, or the like. There are helper methods on the Selectors
abstract class to make creating these `Selector`s very easy in user code.
Here’s is an example of wiring a Consumer
to a Selector
on a Reactor
:
// This helper method is like jQuery’s. // It just creates a Selector instance so you don’t have to "new" one up. import static reactor.event.selector.Selectors.$;
Environment env = new Environment();
// This factory call creates a Reactor. Reactor reactor = Reactors.reactor() .env(env) // our current Environment .dispatcher(Environment.EVENT_LOOP) // use one of the BlockingQueueDispatchers .get(); // get the object when finished configuring
// Register a consumer to handle events sent with a key that matches "parse" reactor.on($("parse"), new Consumer<Event<String>>() { @Override public void accept(Event<String> ev) { System.out.println("Received event with data: " + ev.getData()); } });
// Send an event to this Reactor and trigger all actions that match the given key reactor.notify("parse", Event.wrap("data"));
In Java 8, the event wiring would become extremely succinct:
// Use a POJO as an event handler class Service { public <T> void handleEvent(Event<T> ev) { // handle the event data } }
@Inject Service service;
// Use a method reference to create a Consumer<Event<T>> reactor.on($("parse"), service::handleEvent);
// Notify consumers of the 'parse' topic that data is ready // by passing a Supplier<Event<T>> in the form of a lambda reactor.notify("parse", () -> { slurpNextEvent() });
Home | Usage Guide | Tuples | Streams | Promises | API Javadoc