11

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:

  1. Responsive
  2. Resilient
  3. Scalable
  4. Event-Driven

enter image description here

The Image Reference: medium.com

My problem is about Reactive Programming, I understood that its goal is Asynchronous Programming by Observable/Subscriber model. enter image description here

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.

1
  • In asynchronous models, message queue is a middleware which facilitates publishing messages and then subscribing to these messages. Similarly a queue will be a component of the ecosystem which achieves reactive programming.
    – S.K.
    Commented Aug 16, 2018 at 7:26

2 Answers 2

18

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.