English 中文(简体)
Fire off an internal ServletRequest in Tomcat
原标题:

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.

问题回答

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.

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?

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

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签