views:

82

answers:

4

I am using Quartz to schedule background tasks for a web application. Some of these tasks just fire off requests against the same web application.

I want to avoid depending on any kind of network settings (for example a request with my own domain name might not be routed properly if made from within the data center). Is there a Java API to fire off a ServletRequest and have it handled by Tomcat internally (without any actual HTTP involved)?

I still want to wrap the work into a Servlet request rather than calling the Java code for the background tasks directly, so that I can go back to regular HTTP request as a configuration option.

Tomcat-specific code is acceptable.

A: 

Interesting notion. There's certainly no standard API for this, and I don't think there's a published Tomcat-specific mechanism either.

What would be receiving the request? A servlet? JSP? Spring controller? Depending on that, and depending on what the target does, you may possibly be able to invoke the target directly, rather than routing it through the container.But if you have JSPs, a stack of filters, and other servlet intricacies, this likely isn't an option.

If on the offchance you're using Spring, then your Quartz job could just be wired up directly with the controller and it'd be easy as you like. Are you that lucky?

skaffman
I want to go through a URL (as opposed to calling controllers directly) so that I can go back to regular HTTP later. Keeps things decoupled.
Thilo
standard API: If I was within a servlet myself, I guess I could do requestDispatcher.include(fakedRequest, fakedResponse). But I am not.
Thilo
+1  A: 

What do you want to get from that scheduled tasks? Standard web-server behavior is to get HTTP request and respond with particular data. I assume that your tasks don't need that, i.e. you want just to perform particular processing which codebase resides within the web-application.

If the assumption above is correct you can just decouple servlet/jsp logic from business-processing logic and call business-logic layer classes directly from your scheduled tasks.

denis.zhdanov
A: 

Use URLConnection, or apache-commons httpclient with localhost / 127.0.0.1 - thus it will be routed properly.

Bozho
That assumes the server is listening on 127.0.0.1, which isn't always the case.
skaffman
what about 0.0.0.0 ? :)
Bozho
A: 

The servlet code is having too much responsibilities. Refactor it to a plain vanilla Java class and let the task class access it (or the other way round, let it visit the task class). If necessary, make wisely use of the ServletContext.

If you elaborate a bit more about the actual problem and functional requirement for which you think that this is the solution, we may come up with much better suggestions.

BalusC
"actual problem and functional requirement": I do not really need to do this, I just want to know if I could.
Thilo
You could do so with `java.net.URLConnection` or Apache Commons HttpClient as mentioned several times. The complete picture is however a bit a smell, I just wanted to warn about that before it's too late.
BalusC