简体   繁体   English

部署在同一tomcat服务器上的两个Web应用程序之间的跨上下文通信

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

I have two web application, webAppMaster and webAppSlave , deployed in same tomcat server. 我有两个Web应用程序, webAppMasterwebAppSlave ,部署在同一个tomcat服务器中。 Now in webAppMaster application, there is a java class, RequestHandler whose processRequest method takes a customObject1 as parameter and returns customObject2 . 现在在webAppMaster应用程序中,有一个java类RequestHandler,processRequest方法将customObject1作为参数并返回customObject2 Now, from RequestCreator class of webAppSlave application, I want to invoke processRequest method of RequestHandler class of webAppMaster application. 现在,从RequestCreatorwebAppSlave应用,我想调用RequestHandlerwebAppMaster应用的processRequest方法。 How this should be done ? 应该怎么做? Thanks in advance. 提前致谢。

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/ 你想要做的实际上是RMI(远程方法调用) - http://docs.oracle.com/javase/tutorial/rmi/

Instead of using rmi you can use some more lightweight way of communication. 而不是使用rmi,您可以使用更轻量级的通信方式。 You can communicate via Rest for example. 例如,您可以通过Rest进行通信。 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). 在这种情况下,在webAppMaster应用程序中创建servlet,它将customObject1序列化为JSON作为参数(作为URL请求参数或使用POST方法)。 Than this servlet will do the translation of JSON string to customObject1 and call processRequest. 比这个servlet将JSON字符串转换为customObject1并调用processRequest。 Later after processRequest() returns customObject2 translate it to JSON and send back to client. 稍后在processRequest()返回customObject2后将其转换为JSON并发送回客户端。 On client side read the json and deserialize JSON back to customObject2 in webappSlave. 在客户端读取json并将JSON反序列化回webappSlave中的customObject2。

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. 首先将customObject1序列化为JSON,然后将收到的JSON反序列化为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. 作为第三个选项,您可以使用HTTP隧道在应用程序之间发送对象(例如,请参阅此帖子: 通过HTTP序列化以正确的方式转换对象。 )作为示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM