Java Reference
In-Depth Information
tested components without incurring as many problems in integrating the components as we
would if they were communicating via shared mutable state. Of course, end-to-end tests are
still useful for making sure that your system does what your users expect of it!
Message passing-based systems also make it easier to isolate failure scenarios and write reli-
able code. If there is an error within a message handler, we have the choice of restarting its
local verticle without having to restart the entire JVM.
In Chapter 6 , we looked at how you can use lambda expressions in conjunction with the
streams library in order to build data parallel code. That lets us use parallelism in order to
process large amounts of data faster. Message passing and reactive programming, which
we'll look at later in this chapter, are at the other end of the spectrum. We're looking at con-
currency situations in which we want to have many more units of I/O work, such as connec-
ted chat clients, than we have threads running in parallel. In both cases, the solution is the
same: use lambda expressions to represent the behavior and build APIs that manage the con-
currency for you. Smarter libraries mean simpler application code.
The Pyramid of Doom
You've seen how we can use callbacks and events to produce nonblocking concurrent code,
but I haven't mentioned the elephant in the room. If you write code with lots of callbacks, it
becomes very hard to read, even with lambda expressions. Let's take a look at a more con-
crete example in order to understand this problem better.
While developing the chat server I wrote a series of tests that described the behavior of the
verticle from the point of view of the client. The code for this is listed in the messageFriend
test in Example 9-7 .
Example 9-7. A test of whether two friends in our chat server can talk to each other
@Test
public
public void
void messageFriend () {
withModule (() -> {
withConnection ( richard -> {
richard . dataHandler ( data -> {
assertEquals ( "bob>oh its you!" , data . toString ());
moduleTestComplete ();
});
richard . write ( "richard\n" );
withConnection ( bob -> {
bob . dataHandler ( data -> {
Search WWH ::




Custom Search