我的线程池任务执行程序未在Spring中调用

时间:2015-11-27 05:33:02

标签: java multithreading spring spring-mvc asynchronous

我想要使用Spring 4& Spring MVC和我正在使用Repository方法的@Async注释进行异步处理。

但是在Async方法中,线程没有SecurityContextHolder的Authentication对象,所以我想自定义SimpleAsyncTaskExecutor。

 public class MyThreadPoolExecuter extends SimpleAsyncTaskExecutor{


    /**
     * 
     */
    private static final long serialVersionUID = 4096385850682910178L;

    RAThreadPoolExecuter() {
        super();
    }

    /* (non-Javadoc)
     * @see java.util.concurrent.ThreadPoolExecutor#execute(java.lang.Runnable)
     */
    @Override
    public void execute(final Runnable command) {
         final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
         super.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("running RA_T");
                    try {
                        final SecurityContext ctx = SecurityContextHolder.createEmptyContext();
                        ctx.setAuthentication(auth);
                        SecurityContextHolder.setContext(ctx);
                        command.run();
                    } finally {
                        SecurityContextHolder.clearContext();
                    }
                }
            });

    }
}

在spring config xml中我有以下条目

    <task:annotation-driven executor="myexecutor"/>
    <beans:bean id="myexecutor" class="com....MyThreadPoolExecuter"/>

之后它在日志中打印

[MyThreadPoolExecuter-1]用于异步方法,但它仍在使用默认执行程序,并且不会输入我的执行方法。

0 个答案:

没有答案
相关问题