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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinath.interwar; | |
/** | |
* Created by Kinath on 3/15/2017. | |
*/ | |
public interface IGroundService | |
{ | |
public String getMessage(); | |
} |
Common Lib also has a class that gets and sets the Service Instances registered to the jar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinath.interwar; | |
/** | |
* Created by Kinath on 3/15/2017. | |
*/ | |
public class ServiceHandler | |
{ | |
private static IGroundService iGroundService; | |
public static IGroundService getiGroundService() | |
{ | |
return iGroundService; | |
} | |
public static void setiGroundService( IGroundService groundService ) | |
{ | |
iGroundService = groundService; | |
} | |
} |
Then let's develop the Front Service. This service is a simple Spring service, that gets a message from the Ground Service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinath.interwar; | |
import org.springframework.web.bind.annotation.CrossOrigin; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RestController; | |
/** | |
* Created by Kinath on 3/15/2017. | |
*/ | |
@CrossOrigin | |
@RestController | |
@RequestMapping("/frontservice") | |
public class FronRestController | |
{ | |
@RequestMapping(value = "/frontmessage",method = RequestMethod.GET) | |
public String getFrontMessage() | |
{ | |
return ServiceHandler.getiGroundService().getMessage(); | |
} | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinath.interwar; | |
import org.springframework.web.bind.annotation.CrossOrigin; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RestController; | |
/** | |
* Created by Kinath on 3/15/2017. | |
*/ | |
@CrossOrigin | |
@RestController | |
@RequestMapping("/groundservice") public class GroundService | |
{ | |
private static GroundService instance; | |
public static GroundService getInstance() | |
{ | |
if( instance == null ) | |
{ | |
instance = new GroundService(); | |
} | |
return instance; | |
} | |
@RequestMapping(value = "/message", method = RequestMethod.GET) | |
public String getMessage() | |
{ | |
return "Message from Ground Service"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinath.interwar; | |
import org.springframework.context.annotation.Configuration; | |
/** | |
* Created by Kinath on 3/15/2017. | |
*/ | |
@Configuration | |
public class GroundServiceConfig | |
{ | |
private static boolean initialized; | |
public GroundServiceConfig() | |
{ | |
if( initialized ) | |
{ | |
return; | |
} | |
initialized = true; | |
if( ServiceHandler.getiGroundService() == null ) | |
{ | |
ServiceHandler.setiGroundService( new GroundServiceAdapter() ); | |
System.out.println("\n\n################################################"); | |
System.out.println("Service Handler set Ground Service Successfully"); | |
System.out.println("################################################\n\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinath.interwar; | |
import java.lang.reflect.Method; | |
/** | |
* Created by Kinath on 3/15/2017. | |
*/ | |
public class GroundServiceAdapter implements IGroundService | |
{ | |
public String getMessage() | |
{ | |
return GroundService.getInstance().getMessage(); | |
} | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.kinath.interwar</groupId> | |
<artifactId>common-lib</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<scope>provided</scope> | |
</dependency> |
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