java - 在 Spring Web 服务器中创建线程以运行超时任务

标签 java spring multithreading spring-mvc

我正在尝试创建一个运行一组任务的线程,如果它没有在特定时间(比如 100 秒)内完成,则会抛出异常。目前我正在尝试通过将任务封装到 runnable 对象中并使用 ExecutorServiceFuture 类来执行超时执行来实现这一点。然而,当我启动我的 Web 服务时,我得到了这个异常:

java.util.concurrent.ExecutionException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.localhostInterpolatorHealthCheck': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:202)

我在网上看到它不是定义自己的线程并启动它们以在 Spring 上启动一个线程那么简单,所以我想知道如何做到这一点?我错过了什么?

最佳答案

试试这个:

   /**
     * Executes a task with a specified timeout.
     */
    public final class TimeoutController {

        /**
         * Do not instantiate objects of this class. Methods are static.
         */
        private TimeoutController() {
        }

        /**
         * Executes <code>task</code>. Waits for <code>timeout</code>
         * milliseconds for the task to end and returns. If the task does not return
         * in time, the thread is interrupted and an Exception is thrown.
         * The caller should override the Thread.interrupt() method to something that
         * quickly makes the thread die or use Thread.isInterrupted().
         * @param task The thread to execute
         * @param timeout The timeout in milliseconds. 0 means to wait forever.
         * @throws TimeoutException if the timeout passes and the thread does not return.
         */
        public static void execute(Thread task, long timeout) throws TimeoutException {
            task.start();
            try {
                task.join(timeout);
            } catch (InterruptedException e) {
                /* if somebody interrupts us he knows what he is doing */
            }
            if (task.isAlive()) {
                task.interrupt();
                throw new TimeoutException();
            }
        }

        /**
         * Executes <code>task</code> in a new deamon Thread and waits for the timeout.
         * @param task The task to execute
         * @param timeout The timeout in milliseconds. 0 means to wait forever.
         * @throws TimeoutException if the timeout passes and the thread does not return.
         */
        public static void execute(Runnable task, long timeout) throws TimeoutException {
            Thread t = new Thread(task, "Timeout guard");
            t.setDaemon(true);
            execute(t, timeout);
        }

        /**
         * Signals that the task timed out.
         */
        public static class TimeoutException extends Exception {
            /** Create an instance */
            public TimeoutException() {
            }
        }
    }

关于java - 在 Spring Web 服务器中创建线程以运行超时任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36601923/

相关文章:

Java 将日期转换为可读形式?

java - 如何禁用 javac 文件名检查?

java - 导出可运行 JAR 文件不起作用

java - 如何将 HTTP 生命周期中间件处理程序添加到 spring?

官方文档中的Java注解示例

java - Autowiring 错误

spring - 如何使用 Spring Data JPA 处理事务?

Python 多线程(while 和 apscheduler)

c - 服务器不会为每个新客户端(套接字)分配线程

java - 在多线程编程中使用静态类方法