How do I autowire a Spring TaskExecutor created thread?

后端 未结 3 1062
北海茫月
北海茫月 2021-02-04 08:18

According to Spring\'s documentation the way to use the TaskExecutor is as follows:

import org.springframework.core.task.TaskExecutor;

public class TaskExecutor         


        
相关标签:
3条回答
  • 2021-02-04 08:45

    You can also use the @Async annotation.

    public class TaskExecutorExample {
    
        @Autowired
        private MessagePrinterTask task;
    
        public void printMessages() {
            for(int i = 0; i < 25; i++) {
                task.printMessage();
            }
        }
    }
    
    @Component
    public class MessagePrinterTask implements Runnable {
    
        @Autowired
        private String message;
    
        @Async
        public void printMessage() {
          System.out.println(message);
        }
    
    }
    

    Any call to printMessage() will be executed asynchronously using a default executor, which you can configure using the following in your Spring xml config.

    <task:annotation-driven executor="myExecutor"/>
    <task:executor id="myExecutor" pool-size="5" />
    
    0 讨论(0)
  • 2021-02-04 08:53

    Try this method.

    @Service
    @Scope("prototype")
    public invokeClass {
    
        @Autowired
        private ApplicationContext applicationContext;
    
    
    
        public void methodName(TestRequest input){
            TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
    
            taskExecutor.execute(new Runnable() {
                public void run() {
    
                    applicationContext.getBean("testService", TestService.class).execute(input);
                }
            });
        }
    }
    
    0 讨论(0)
  • 2021-02-04 09:06

    There are two ways I think that you can go about this:

    a. Provide the dependencies to the Task - this way:

    class MessagePrinterTask implements Runnable {
        public MessagePrinterTask(ADependency aDependency){
            this.aDependency = aDependency;
        }
    
    
        private ADependency aDependency;
    
        public void run() {
            aDependency.doNotThrowNullPointerExceptionPlease();
        }
    }
    

    And in your TaskExectorExample which can be the singleton:

    import org.springframework.core.task.TaskExecutor;
    
    public class TaskExecutorExample {
    
      @Autowired  private ADependency aDependency;
    
      @Autowired
      public TaskExecutorExample(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
      }
    
      public void printMessages() {
        for(int i = 0; i < 25; i++) {
          taskExecutor.execute(new MessagePrinterTask(this.aDependency));
        }
      }
    }
    

    b. Using @Configurable annotation on your MesasgePrinterTask, this will inject in dependencies into MessagePrinterTask even though it is instantiated outside of a Spring Container - there are some catches in using @Configurable though(requires AspectJ):

    @Configurable
    class MessagePrinterTask implements Runnable {
    
    0 讨论(0)
提交回复
热议问题