Sunday, March 19, 2017

Communicate Within Wars Inside Same Container

Hi All,

There are certain scenarios when you want to communicate within the wars inside the same container without using the network. For an example, you need to avoid any network delay that would cause either by using web-services,RMI or HTTP.

So what I'm going to show you is one working solution to achieve the above problem. What we can do is introduce a common library that is a dependent to both the services. Through the intermediate library we can continue the communication among the war files.

For this example, I'm using a service called, Front Service to accept the user request. Then a second service called 'Ground Service' that contains a method need to be called by the Front Service. The communication between the services are done with a JAR named 'Common-Lib'.

Common Lib has an interface which is similar to the Ground Service. It contains the method definitions of the Ground Service.



Common Lib also has a class that gets and sets the Service Instances registered to the jar.



Then let's develop the Front Service. This service is a simple Spring service, that gets a message from the Ground Service.



Finally we need to implement the Ground Service.
Here we have the Ground Service class and the helper classes to map a service instance to the Service Handler. There is a class named GroundServiceConfig, that sets an instance of the IGroundService interface to the Service Handler. IGroundService is implemented in the GroundServiceAdapter class.







Now we have the two services and the common library defined. It's important to note that although common-lib is a dependent of both the services, we do not bundle them with the service. Therefore in the POM.xml, we specify the 'common-lib' dependency as provided.



Therefore we need to deploy the common-lib in the 'lib' inside tomcat installation.

What happens here is that, when the Ground Service is initializing, it will assign a new instance of the IGroundService in the Service Handler. This instance can then be used to call the service methods from the other Services that uses 'Common-Lib' as a dependency.

Now let's deploy the two services inside the web-apps and put the common-lib inside the 'lib'

Let's go to the Front Service URL and check the result. As you can see we have got the message successfully from the GroundService.

The complete projects and code can be found here.

Hope that helps.
Thank You.



No comments:

Post a Comment