在 Tomcat 中触发内部 ServletRequest
我正在使用 Quartz 来安排 Web 应用程序的后台任务。其中一些任务只是针对同一 Web 应用程序发出请求。
我想避免依赖于任何类型的网络设置(例如,如果从数据中心内发出带有我自己域名的请求,则可能无法正确路由)。是否有 Java API 来触发 ServletRequest 并由 Tomcat 在内部处理(不涉及任何实际的 HTTP)?
我仍然想将工作包装到 Servlet 请求中,而不是直接调用后台任务的 Java 代码,这样我就可以返回到常规 HTTP 请求作为配置选项。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想从计划任务中得到什么?标准 Web 服务器行为是获取 HTTP 请求并使用特定数据进行响应。我假设您的任务不需要这样做,即您只想执行代码库驻留在网络应用程序中的特定处理。
如果上述假设正确,您可以将 servlet/jsp 逻辑与业务处理逻辑解耦,并直接从计划任务中调用业务逻辑层类。
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.
有趣的想法。当然没有这方面的标准 API,而且我认为也没有已发布的 Tomcat 特定机制。
什么会收到请求?一个servlet? JSP?弹簧控制器?根据这一点,并根据目标的作用,您可能能够直接调用目标,而不是通过容器路由它。但是,如果您有 JSP、一堆过滤器和其他错综复杂的 Servlet,则这可能是不是一个选择。
如果您偶然使用 Spring,那么您的 Quartz 作业可以直接与控制器连接,这将很容易,如您所愿。你有那么幸运吗?
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?
使用 URLConnection 或 apache-commons httpclient 与 localhost / 127.0.0.1 - 这样它将被正确路由。
Use URLConnection, or apache-commons httpclient with localhost / 127.0.0.1 - thus it will be routed properly.
servlet 代码承担了太多责任。将其重构为普通的 Java 类并让任务类访问它(或者反过来,让它访问任务类)。如有必要,请明智地使用
ServletContext
。如果您详细说明您认为这是解决方案的实际问题和功能需求,我们可能会提出更好的建议。
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.