Java Reference
In-Depth Information
ture —you can still get all the events up until an exception occurs, but in both cases you
either end normally or end exceptionally.
Example 9-17. Notifying your Observable that an error has occurred
observer . onError ( new
new Exception ());
As with CompletableFuture , I've only given a flavor of how and where to use the Observ-
able API here. If you want more details, read the project's comprehensive documentation .
RxJava is also beginning to be integrated into the existing ecosystem of Java libraries. The
enterprise integration framework Apache Camel, for example, has added a module called
Camel RX that gives the ability to use RxJava with its framework. The Vert.x project has
also started a project to Rx-ify its API.
When and Where
Throughout this chapter, I've talked about how to use nonblocking and event-based systems.
Is that to say that everyone should just go out tomorrow and throw away their existing Java
EE or Spring enterprise web applications? The answer is most definitely no.
Even accounting for CompletableFuture and RxJava being relatively new, there is still an
additional level of complexity when using these idioms. They're simpler than using explicit
futures and callbacks everywhere, but for many problems the traditional blocking web ap-
plication development is just fine. If it ain't broke, don't fix it.
Of course, that's not to say that reading this chapter was a waste of your afternoon! Event-
driven, reactive applications are growing in popularity and are frequently a great way to
model the problems in your domain. The Reactive Manifesto advocates building more ap-
plications in this style, and if it's right for you, then you should. There are two scenarios in
particular in which you might want to think in terms of reacting to events rather than block-
ing.
The first is when your business domain is phrased in terms of events. A classic example here
is Twitter, a service for subscribing to streams of text messages. Your users are sending mes-
sages between one another, so by making your application event-driven, you are accurately
modeling the business domain. Another example might be an application that tries to plot the
price of shares. Each new price update can be modeled as an event.
The second obvious use case is a situation where your application needs to perform many I/O
operations simultaneously. In these situations, performing blocking I/O requires too many
Search WWH ::




Custom Search