Using Hazelcast and RxJava to build agnostic clients
I am currently refactoring a suite of services that are all Hazelcast cache instances and part of a cache cluster. One of my first steps was to look at client-service communication endpoints and see whether I can’t decouple clients and services some more to allow for a more flexible Client-Service framework built on top of Hazelcast Queues and Topics but more of that in next week’s post. For now lets look at the current clients and services. They all assume that communication happens synchronously. That lead to the first refactoring on the road to true Hazelcast client-service messaging bliss.
Clients should be agnostic of service implementation
Clients should not care whether the service call is made asynchronously or synchronously. Maybe we use synchronous REST calls in one place but asynchronous cache queue-based communication somewhere. As the framework developer I don’t want to burden another developer with the additional complexity of figuring out how the call is being made. RxJava is a library developed by Netflix that will wrap a client’s return type in an Observable to which the caller can attach a callback. If you would rather wait for the data you can turn the Observable into a BlockingObservable which waits until data is available.
Observable<SomeDTO> ret = yourClient.makeSomeCall(String parameter); SomeDTO thanksForWaiting = ret.first();
I’m a big fan of this pattern. Whenever I can hide complexity I’m happy.
Check out https://github.com/Netflix/RxJava