I'm busy with Reactive concept these days. I have understood two separate concepts as Reactive System and Reactive Programming. In addition, I know Reactive System is a larger concept which contains four properties:
The Image Reference: medium.com
My problem is about Reactive Programming, I understood that its goal is Asynchronous Programming by Observable/Subscriber model.
The Image Reference: https://hub.packtpub.com/introduction-reactive-programming/
Now I'm confused about the difference between Reactive Programming and Message Queue. I have had some experience in Message Oriented Middleware and related standards such as JMS and I think Reactive Programming is the same using messaging queue in listener mode not blocking mode.
I wanted to be clear in Reactive Programming real concept.
Reactive programming describes a design paradigm that relies on asynchronous programming logic to handle real-time updates to otherwise static content. It provides an efficient means -- the use of automated data streams -- to handle data updates to content whenever a user makes an inquiry.
Asynchronous programming just means that you're writing code that will not be executed immediately, as in imperative programming, but at "some point in the future". Reactive programming is characterized by the execution of the asynchronous code being triggered by the arrival of data to execute on.
So what is Reactive programming? It is a declarative, event-driven programming paradigm concerned with data streams and the propagation of change. It is the availability of new information that drives the logic forward rather than having control flow driven by a thread-of-execution.
Reactive programming is a design approach that uses asynchronous programming logic to handle real-time adjustments to typically static information. It provides an efficient mechanism — the use of automated data streams — for handling content modifications in response to user inquiries.
Reactive Programming is a new name for old concepts. It means prefer event-driven computing over request-reply computing. Or 'push' versus 'pull'. So instead of writing code that waits for something to arrive, you define callbacks that are executed when something happens. The Observer pattern is a good example of reactive programming and so is the Hollywood Principle ("Don't call us, we'll call you").
In JMS terms, you are doing reactive programming if you defined a MessageListener on your consumer: MessageConsumer.setMessageListener(MessageListener listener)
Then your API or other code can decide what to process in the meantime and if something arrives, from a queue or topic, that callback is invoked.
The alternative, using MessageConsumer.receive(long timeout)
, is not reactive programming. You are blocking your current thread until the next message arrives.
Thinking in an event-driven or reactive way takes a bit of a leap sometimes, but it is worth the effort.
When looking at the system itself, I would argue that an infrastructure built with topics is a reactive system. An infrastructure built with queues is not.
In synchronous programming, blocking queues
are widely used. If consumer does not manage to process messages in timely manner, blocking queue
suspends producer thread in order to avoid excessive consumption of memory for messages.
In synchronous world, thread blocking must be avoided, so blocking queues
are not acceptable. Instead, reactive streams
are used, which inform producer how many messages it is allowed to send.
Technically, a reactive stream
is a usual stream of messages plus backward stream of permissions
. Permissions
are like undistinguishable messages, which need not to be stored, but only counted. In synchronous world, permissions
are stored (counted) by java.lang.Semaphore
and java.lang.CountdownLatch
. Unfortunately, most asynchronous libraries do not provide developer with separate constructs to handle permissions
. The only exception I know is my own library df4j
(can be found at Github).
Except for (implicit) notion of asynchronous semaphore, I could not find any other sense in all that reactive programming
hype.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With