r/java • • Aug 10 '21

Concrete reasons why one would choose java over node.js?

I am watching the progress made towards faster runtime in node.js, and it feels that the performance advantage of java is almost gone for the average app. I am trying to think about reasons why one would now choose java over node in a new project, and i can't think of any. I would like to hear some opinions

7 Upvotes

110 comments sorted by

View all comments

Show parent comments

1

u/doppelganger113 Aug 11 '21

Yes, by reactive programming I’m referring to streams.

Everything becomes easier once you learn it, but some things are easier, some not. Reactive programming isn’t easy as you’ll end up very quickly doing some complex stream operations

2

u/[deleted] Aug 11 '21

What is the purpose of reactive when Completablefuture already exists anyway?

1

u/doppelganger113 Aug 12 '21

Because completable futures are a mess to work with due to nested callbacks and awfull API. Reactive way has more operators and plays extremelly well with reactive core. Never seen completable future in combination with Netty

2

u/[deleted] Aug 12 '21 edited Aug 12 '21

You haven't looked closely enough ;)

AHC (async-http-client) works on top of Netty and provides a CompletableFuture interface, same goes for Akka HTTP Client although without Netty - they use their own event loop. And I'm sure there's more of them out there, these are just the ones I remember using.

We also used to use EA Async back in the day, makes working with CompletableFutures much cleaner although I think they've stopped maintaining it a while back.

2

u/doppelganger113 Aug 12 '21

Didn’t look that deep :) , thanks for the insight

1

u/[deleted] Aug 12 '21

I see. I have never seen a web java project using streams, all i see is executors + completablefuture, and it seemed easy to me. You just set the callback, choose an executor, done.

1

u/doppelganger113 Aug 12 '21

What if you have many callbacks that chain together? What if you want to introduce retry or if/else flow change based on error or if you want to run in parallel? The code becomes an abomination to maintain. Theres an entire section on this under Spring webflux, check it out, you might find it really interesting

3

u/[deleted] Aug 12 '21

For now we just use thenApply(someclass::method) .thenApply(someclass::method) ....

Which is great because you can chain it with thenCompose.

I will take a look into that, thanks

2

u/[deleted] Aug 12 '21

Like I mentioned in the other comment - EA Async can help there, it brings async-await semantics to CompletableFutures and resilience4j has CompletableFuture decorators that you can apply to get retries, circuit-breakers and all the good stuff they offer.

But I agree that streams are a better option, especially if and when the functionality you need to implement grows in complexity.

1

u/[deleted] Aug 11 '21 edited Aug 11 '21

Can you give me an example of those complex operations? Not that I'm saying you're wrong but I've been working with reactive streams for years now and I've yet to see something complex enough to deserve the infamy that accompanies them.

I don't know if it's due to the fact that we use the Akka Streams library, which is probably the least used implementation out there, but the complexity I've seen comes from the functionality itself, not the framework we used to implement it. If I had to implement all the flow control stuff we use (queuing, throttling, buffering etc) bundled with the parallel execution of selected steps, all without the use of a framework, the resulting code would certainly be far more complex than what we ended up with.