简体   繁体   中英

Intercommunication between two wars in Tomcat

I have a fundamental question, is it possible to communicate between two wars which are running on same instance?

Another way, suppose Thread A [daemon] is running/initiated from A.war, and Thread B [daemon] is running/initiated from B.war, both wars are deployed in same tomcat instance. Can I communicate between Thread A to Thread B? If so, how it's possible [link to tutorial]?

Your 2 wars each have their own classLoader and components, so you cannot easily share an object instantiated in any one of them.

I'd say you have 2 typical ways to go:

  1. Remote access via some kind of web-service (based on sockets, RMI, HTTP... you name it). It even allows you to deploy your wars in different instances.
  2. Using a shared component, which has to be given as a resource by the server instance, typically via JNDI. As an example, look here at how it's done with Tomcat7 for dataSources (the most common use case).

By design two webapplications are separated by different classloaders and can't access each other's memory/classloaders. However, as the others have answered, there are certain ways around this - and depending on the underlying requirements you might accept the pros and cons of the various solutions.

However, I'd argue that uncoupling your application layers to different WAR files only to couple them again with classloader magic (without knowing more about the underlying application) might be overkill and relying too much on implementation details.

Given your description you probably can do well with configuring webservice-level access between the two applications. Refer to any webservice tutorial for this. Alternatively consider MessageBus-like implementations.

Something that would be too heavyweight to suggest based on your question is Liferay's service builder - this is a component that abstracts away the different application contexts for certain API calls and enables you to transparently call into a different application. It's bound to Liferay though, which might not be the solution that you're looking for.

Well, the simplest thing that could possibly work is to put a shared library (jar) in the common classloader (don't remember off-hand which directory it is, lib , pehaps). Then you can get as fancy as you wish regarding all the options you have from there, eg BlockingQueues, Semaphores and whatnot. Just be cautios in what Types you shuffle to and from each application, to avoid class cast exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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