11

What options are there to achieve low-latency communication between two wars running in the same jetty-container?

I basically need to call a service in one war from the other, but can't afford the overhead of calling it as a web service.

Since they are running in the same JVM, I'm hoping to avoid using RMI/JMS etc., but I don't know what other options I have?

I've looked at inter-servlet communication, but since direct method invocation is deprecated that doesn't seem to be the right choice?

I also found kyronet, but are there better solutions since this is in the same JVM?

What I'm looking for is something like Apache Camel's VM Component (seda between web-applications), but since only one of the applications is using Camel for this is not an option.

I know I might have to share some DTO's between the wars, but please don't suggest pulling the service into a shared library, if that was an option I wouldn't be asking this question:)

Edit:

Embedding an EJB-container is probably not an option either.

ebaxt
  • 8,287
  • 1
  • 34
  • 36

3 Answers3

5

Register interfaces with JNDI and make them global so that the 'other' servlet can retrieve them from the repository.

Check this

(note: we gave up JNDI in favor of our own registry implementation but we start the registry and Jetty programmatically in the same JVM)

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
  • Thanks for you answer! Why did you give up Jetty's JNDI support? So did you implemented your own NamingManager, but still use the Context API, or did you drop JNDI all together? Could you point me to some resource describing how to problematically register it with Jetty? Oh, and finally, are objects transferred through this solution passed by reference or serialized? – ebaxt Mar 21 '12 at 09:40
  • We pass by 'reference' so Objects Instances are directly addressable. JNDI is fine: try this link for some info [link](http://docs.codehaus.org/display/JETTY/JNDI). The reason we dropped it was two-fold: we wanted more flexibility (multiple registries with fixed interfaces, query functionalities, runtime registration) and a leaner package (JNDI is generic and provides functionality we do not need). Implementing your registry requires that you properly handle the webapp lifecyle, which may not be easy – Bruno Grieder Mar 21 '12 at 10:57
  • How can the same be done in Jboss, Basically I need to understand, how the service component be exposed to JNDI ? – Love Hasija Feb 20 '14 at 19:30
3

It is possible to "communicate" between two co-located web applications by using ServletContext.getContext(String uriPath) and RequestDispatchers (or even ServletContext listeners).

Following is the working code. Suggested by Peer Reynders (http://www.coderanch.com/t/222608/Web-Services/java/communicate-war-files)

public void doGet(HttpServletRequest Prequest, HttpServletResponse Presponse)
  throws IOException, ServletException {

  ServletContext    sc = getServletContext().getContext("/war2");
  RequestDispatcher rd = sc.getRequestDispatcher("/LoginHandler?UserId=DummyUser");
  rd.forward(Prequest, Presponse);
} /* End of doGet */
siddagrl
  • 371
  • 3
  • 4
2

Exposing the service as an EJB with local interface would be one option. The standard does not guarantee that this will be implemented as a direct call when done across apllications, but apparently most app servers implement it that way. For running in Jetty, you'd have to use an embeddable EJB container such as OpenEJB, JBoss embeddable or Spring's Pitchfork.

Community
  • 1
  • 1
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • Thanks! Since we have a big SOA-deployment based on a custom embedded-jetty wrapper, I don't thing embedding an EJB-container is going to fly with my manager. – ebaxt Mar 20 '12 at 09:22
  • @ebaxt: I suspect that hinges entirely on the label "EJB" and its bad reputation from 10 years ago. If no other simple solution turns up, it might be worth giving it a try. – Michael Borgwardt Mar 20 '12 at 09:30