Reputation: 1547
I have two web application, webAppMaster and webAppSlave, deployed in same tomcat server. Now in webAppMaster application, there is a java class, RequestHandler whose processRequest method takes a customObject1 as parameter and returns customObject2. Now, from RequestCreator class of webAppSlave application, I want to invoke processRequest method of RequestHandler class of webAppMaster application. How this should be done ? Thanks in advance.
Upvotes: 3
Views: 4400
Reputation: 4942
You need to talk between applications as if you were talking between two distant applications. It does not matter that they are on the same server they simply must communicate using some protocol.
What you want to do is actually RMI (remote method invokation) - http://docs.oracle.com/javase/tutorial/rmi/
Instead of using rmi you can use some more lightweight way of communication. You can communicate via Rest for example. In this case create servlet in webAppMaster application which takes as parameters your customObject1 serialized to JSON (either as URL request params or use POST method). Than this servlet will do the translation of JSON string to customObject1 and call processRequest. Later after processRequest() returns customObject2 translate it to JSON and send back to client. On client side read the json and deserialize JSON back to customObject2 in webappSlave.
public class MasterServlet extends javax.servlet.http.HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CustomObject1 customObject1 = buildCustomObject1BasingOnRequestParams(HttpServletRequest request); // read the request params and build your object either from json or whatever format webappSlave used to send
CustomObject2 customObject2 = RequestHandler.processRequest(customObject1);
String json = transformTOJson(customObject2); // there are many libaries which does this
response.getWriter().print(json);
}
}
Your slave app would do the other way around. First serialize customObject1 to JSON, and later deserialize received JSON to customObjec2.
As a third option you can use HTTP tunneling to send objects between applications (reffer for example to this post: Serializing over HTTP correct way to convert object.) as an example.
Upvotes: 5