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

 
23
Kudos
 
23
Kudos

Now read this

Over-engineer your own file-backup solution

Until yesterday all my digital life was backed up on a physical drive using Time Machine. Occasionally I would feel guilty about that TimeMachine reminder that I haven’t backed-up in a while and would rummage through my desk for the... Continue →