Reputation: 313
I have the need to communicate between two web applications. Both are tomcat projects. I want to avoid using http requests to communicate. After doing some research I discovered there is a ServletContext object which is meant to handle this.
I was following the guide on http://blog.imaginea.com/cross-context-communication-between-web-applications/ and decided to try this in my own quickstarts.
I made sure my tomcat server had its crossContext set to true.
<Context crossContext="true">
I created two wicket quickstarts one named bar and one named foo. The idea was that I could call a function in foo from bar.
Here is the code in bar
String methodName = getRequest().getParameter("PARAM_METHOD");
ServletContext srcServletContext = ((WebApplication)WebApplication.get()).getServletContext();
ServletContext targetServletContext = srcServletContext.getContext("/foo");
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Object object = targetServletContext.getAttribute("org.apache.wicket.protocol.http.WicketServlet.CONTEXT.foo");
ClassLoader targetServiceClassLoader = targetServletContext.getClass().getClassLoader();
Thread.currentThread().setContextClassLoader(targetServiceClassLoader);
// Causes a ClassNotFoundException
Class<?> classBarService = (Class<?>)targetServiceClassLoader.loadClass("com.foo.SomeUtil");
Method getTextMethod = object.getClass().getMethod("getText", String.class);
Object someUtil = getTextMethod.invoke(object, "someUtil");
Method targetMethod = classBarService.getMethod(methodName, (Class[])null);
Object responseFromTextMethod = targetMethod.invoke(someUtil, (Object[])null);
}
catch (Exception e)
{
text += e.toString();
}
finally
{
Thread.currentThread().setContextClassLoader(currentClassLoader);
}
foo has a class named SomeUtil in the package com.foo. But when I try to load the class after changing the class loader I am getting a "ClassNotFoundException: com.foo.SomeUtil". I can't really tell what I am doing wrong.
Any help is appreciated in advance.
Upvotes: 1
Views: 948
Reputation: 3035
Have you tried to replace
ClassLoader targetServiceClassLoader = targetServletContext.getClass().getClassLoader();
with
ClassLoader targetServiceClassLoader = object.getClass().getClassLoader();
like it was proposed in the tutorial?
Upvotes: 0
Reputation: 3035
Even if have CrossContext set to true
, the ClassLoader of the calling class needs to have access to the class that it is trying to load.
Try to deploy the jar(s) containing the classes to both web applications.
quote from the tutorial:
Solution-1: If we could externalize the custom data type classes which are accessed by multiple web applications (here accessed by ‘Foo’ and ‘Bar’) to a different library and place it inside the commons library location of the web container (In case of tomcat, it is <TOMCAT_HOME>\lib),
Either you deploy the jar to both webapps, or to commons/lib, but AFAIK, newer versions of tomcat don't provide commons/lib anymore by default
Upvotes: 1