Sunday, 10 July 2016

JMS

(The main objective of this article is to identify the places where JMS is needed)

JMS(Java messaging service) is a widely used technology. It has two different mechanism called Queue and Topic(publish-subscribe). A queue may have multiple listeners. But when u push a message, it will be picked up by only one listener whereas in topic, message will be received by all the subscribers who subscribed to the topic.

So now the question is where does it need?

Let's consider a case, you have a centralised database which has to be updated by the multiple applications. But you don't want other application to access this database. So here, you can have a queue and all the applications will just push their data to this queue. You will take care of dequeue them, process them & store them in the DB.

In another case, you need to send a mail and call a web service, before Storing the user data into your application database, when user submit a request.

Here, there's no guarantee that the email service and the web service will always be up and running. So the entire flow will fail or we have to skip the mailing part and/or calling web service, if they are down. Even though both the servers are up, there'll be some delay in storing data and sending the response back to the user with respect to the time taken by the external services.

To avoid such issues, we can have a queue where we push the messages and go ahead with the storing data in DB. A separate JMS listener component will take care of sending emails and calling/updating the web service.

You may think that we can store the same data which we are pushing to the queue, in DB and later, we can fetch them from DB and process them.Yes, you can also use DB. If you are ok to expose your database to other components or if you want to store those data permanently or if the size of the message is large, you can go for DB instead of queues.

If your application needs to send a message to multiple applications/components, you may go for the topic. As we told before, if a message comes into the topic , it will be received by more than one subscribers which is actually an another application or component. Queue and Topic both can be used as a middleware which use to connect components/applications written in various languages.

No comments:

Post a Comment