How can I create a <thread> created Spring TaskExecutor thread?
According to the Spring documentation, the way to use TaskExecutor is as follows:
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorExample {
private class MessagePrinterTask implements Runnable {
private String message;
public MessagePrinterTask(String message) {
this.message = message;
}
public void run() {
System.out.println(message);
}
}
private TaskExecutor taskExecutor;
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask("Message" + i));
}
}
}
However, if MessagePrinterTask has autonomous dependencies, they will not be configured using Spring, because we are creating a bean instance outside the Spring context (at least as I understand it), although Spring will provide the actual creation of the threads. If MessagePrinterTask must have autonomous dependencies, how can we get Spring to recognize them? I tried the following modified example to no avail (and yes, auto-connect is enabled correctly):
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorExample {
@Component
private class MessagePrinterTask implements Runnable {
@Autowired
private autoWiredDependency;
public void run() {
autoWiredDependency.doNotThrowNullPointerExceptionPlease();
}
}
private TaskExecutor taskExecutor;
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask());
}
}
}
+10
3 answers
, :
. - :
class MessagePrinterTask implements Runnable {
public MessagePrinterTask(ADependency aDependency){
this.aDependency = aDependency;
}
private ADependency aDependency;
public void run() {
aDependency.doNotThrowNullPointerExceptionPlease();
}
}
TaskExectorExample, 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));
}
}
}
. @Configurable annotation MesasgePrinterTask, MessagePrinterTask, Spring - @Configurable, ( AspectJ):
@Configurable
class MessagePrinterTask implements Runnable {
+15
@Async.
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);
}
}
printMessage() , Spring xml.
<task:annotation-driven executor="myExecutor"/>
<task:executor id="myExecutor" pool-size="5" />
+3
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