Cross context communication between two web application deployed on same tomcat server

浪尽此生 提交于 2019-12-03 08:50:40
walkeros

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!