Какая-либо хорошая поточная обработка Spring с TaskExecutor примеры? [закрытый]

Вот как будет выглядеть MySQL-запрос:

SELECT 
    (
      SELECT COUNT(*)
      FROM TableScore X
      WHERE X.ts > T.ts OR (X.ts = T.ts AND X.avg_times_taken < T.avg_times_taken)
    ) + 1 AS position   
FROM TableScore T      
WHERE T.user_id = '7';

РАБОТА С DB-FIDDLE, ПРИМЕР ЗДЕСЬ

25
задан James McMahon 12 May 2009 в 13:20
поделиться

1 ответ

It's pretty simple. The idea is that you have an executor object that's a bean, which is passed into whatever object wants to fire the new task (in a new thread). The nice thing is that you can modify what type of task executor to use just by changing the Spring config. In the example below I'm taking some example class (ClassWithMethodToFire) and wrapping it in a Runnable object to do the fire; you could also actually implement Runnable in a class of your own, and then in the execute method you'd just call classWithMethodToFire.run().

Here's a very simple example.

public class SomethingThatShouldHappenInAThread {
     private TaskExecutor taskExecutor;
     private ClassWithMethodToFire classWithMethodToFire;

     public SomethingThatShouldHappenInAThread(TaskExecutor taskExecutor,
                                               ClassWithMethodToFire classWithMethodToFire) {
          this.taskExecutor = taskExecutor;
          this.classWithMethodToFire = classWithMethodToFire;
     }

     public void fire(final SomeParameterClass parameter) {
          taskExecutor.execute( new Runnable() {
               public void run() {
                    classWithMethodToFire.doSomething( parameter );
               }
          });
     }
}

And here are the Spring beans:

<bean name="somethingThatShouldHappenInAThread" class="package.name.SomethingThatShouldHappenInAThread">
     <constructor-arg type="org.springframework.core.task.TaskExecutor" ref="taskExecutor" />
     <constructor-arg type="package.name.ClassWithMethodToFire" ref="classWithMethodToFireBean"/>
</bean>

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
     <property name="corePoolSize" value="5" />
     <property name="maxPoolSize" value="10" />
     <property name="queueCapacity" value="25" />
</bean>
34
ответ дан 28 November 2019 в 21:32
поделиться
Другие вопросы по тегам:

Похожие вопросы: