Servlet 中的后台进程

发布于 2024-09-06 21:30:25 字数 158 浏览 4 评论 0原文

是否可以在 servlet 中实现后台进程!?

让我解释一下。 我有一个 servlet,可以显示一些数据并生成一些报告。 报告的生成意味着数据已经存在,而且是这样的:其他人上传了这些数据。

除了生成报告之外,我还应该实现一种在新数据(已上传)到达时发送电子邮件的方法。

Is it possible to implement a background process in a servlet!?

Let me explain.
I have a servlet that shows some data and generate some reports.
The generation of the report implies that the data is already present, and it is this: someone else upload these data.

In addition to report generation, I should implement a way to send an email on arrival of new data (uploaded).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2024-09-13 21:30:25

功能需求尚不清楚,但要回答实际问题:是的,可以在 servletcontainer 中运行后台进程。

如果您想要应用程序范围的后台线程,请使用 ServletContextListener来挂钩 webapp 的启动和关闭并使用 ExecutorService 来运行它。

@WebListener
public class Config implements ServletContextListener {

    private ExecutorService executor;

    public void contextInitialized(ServletContextEvent event) {
        executor = Executors.newSingleThreadExecutor();
        executor.submit(new Task()); // Task should implement Runnable.
    }

    public void contextDestroyed(ServletContextEvent event) {
        executor.shutdown();
    }

}

如果您尚未使用 Servlet 3.0,因此无法使用 @WebListener,请在 web.xml 中按如下方式注册:

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

如果您想要一个会话范围的后台线程,使用 HttpSessionBindingListener启动和停止它。

public class Task extends Thread implements HttpSessionBindingListener {

    public void run() {
        while (true) {
            someHeavyStuff();
            if (isInterrupted()) return;
        }
    }

    public void valueBound(HttpSessionBindingEvent event) {
        start(); // Will instantly be started when doing session.setAttribute("task", new Task());
    }

    public void valueUnbound(HttpSessionBindingEvent event) {
        interrupt(); // Will signal interrupt when session expires.
    }

}

在第一次创建和启动时,只需执行

request.getSession().setAttribute("task", new Task());

The functional requirement is unclear, but to answer the actual question: yes, it's possible to run a background process in servletcontainer.

If you want an applicationwide background thread, use ServletContextListener to hook on webapp's startup and shutdown and use ExecutorService to run it.

@WebListener
public class Config implements ServletContextListener {

    private ExecutorService executor;

    public void contextInitialized(ServletContextEvent event) {
        executor = Executors.newSingleThreadExecutor();
        executor.submit(new Task()); // Task should implement Runnable.
    }

    public void contextDestroyed(ServletContextEvent event) {
        executor.shutdown();
    }

}

If you're not on Servlet 3.0 yet and thus can't use @WebListener, register it as follows in web.xml instead:

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

If you want a sessionwide background thread, use HttpSessionBindingListener to start and stop it.

public class Task extends Thread implements HttpSessionBindingListener {

    public void run() {
        while (true) {
            someHeavyStuff();
            if (isInterrupted()) return;
        }
    }

    public void valueBound(HttpSessionBindingEvent event) {
        start(); // Will instantly be started when doing session.setAttribute("task", new Task());
    }

    public void valueUnbound(HttpSessionBindingEvent event) {
        interrupt(); // Will signal interrupt when session expires.
    }

}

On first creation and start, just do

request.getSession().setAttribute("task", new Task());
离去的眼神 2024-09-13 21:30:25

谢谢你!我想知道这是否应该在请求范围内更好地完成,例如:

public class StartServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {                   
        request.getSession().setAttribute("task", new Task());     
    }
}

这样,当用户离开页面时,进程就会停止。

Thank you! I was wondering whether this should be better done inside of an request scope like:

public class StartServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {                   
        request.getSession().setAttribute("task", new Task());     
    }
}

This way, the process is stopped when the user leaves the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文