2

I'm using Spring and I have a scheduled task that makes some operations on the database. I figured out this task is executed on each pool instead I would like to have only one execution of it. For example in my log file I read:

2016-08-04 01:01:01 [pool-2-thread-1] INFO  c.w.c.FleetAndCarControllerImpl - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
2016-08-04 01:01:01 [pool-3-thread-1] INFO  c.w.c.FleetAndCarControllerImpl - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS

I have this configuration:

@Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(100);
        executor.initialize();
        return executor;
    }

and this is the task:

//This task is executed every day at 01:01 a.m.
    @Scheduled(cron = "0 1 1 * * ?")
    public void smartQuery(){
        try {
            LOG.info("SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS");
            notificationManagementServices.deleteOldNotifications();
        } catch (QueryException e) {
            ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
            LOG.error("Threw exception in WakeUpDatabase::smartQuery :" + errorResponse.getStacktrace());
        }
    }   

Is it possible?thanks

luca
  • 3,248
  • 10
  • 66
  • 145
  • Set core pool size to `1`. – Ali Dehghani Aug 04 '16 at 06:39
  • 2
    "I figured out this task is executed on each thread" how exactly did you figure that out? – Kayaman Aug 04 '16 at 06:44
  • In my log file I read the log message printed several times. I have updated main post with some row – luca Aug 04 '16 at 06:56
  • @luca Based on your log message, it's not being scheduled in *each* thread. It's being scheduled on 1 thread, but in 2 pools. – Kayaman Aug 04 '16 at 07:57
  • yes, I'm sorry for wrong information, I didn't remember fine. This server is in development and only few users make request. It seems that each pool executes scheduled task. Who create the pools?tomcat or Spring? – luca Aug 04 '16 at 08:51
  • Normally @Scheduler annotation use default thread pool with size 1, u can assign new thread pool to scheduler task or increase default thread pool size, but u cant assign thread pool more than one.I think ur scheduler running in two instance each of run independently. – divilipir Jun 03 '22 at 14:33

2 Answers2

1

I know it's late, but I've faced the same problem and for me the solution was to annotate my Scheduler class with @Singleton, then Scheduler just runned on time in all those clusters and instances.

As seemed at https://docs.oracle.com/cd/E19226-01/820-7627/6nisfjmnv/index.html

0

Do you mean this:
You want to have multiple threads, and some task only gets executed in one thread, or each task runs in it's own thread.

If so, the most easy way is to create several TaskSchedulers, with pool size is 1. And you can trigger the scheduled job in java with TaskScheduler.schedule(...). If you have several schedulers, you need to inject different schedulers with qualified name.

Mavlarn
  • 3,807
  • 2
  • 37
  • 57
  • yes, I would like to have multiple thread but some task, like scheduled task, should be executed one time and not for all the threads (it is useless to make the same query for each thread) – luca Aug 04 '16 at 07:03
  • I don't think you can assign one task only executed in one thread in a pool. So my solution is to create several task schedulers, and you assign and trigger the task in java code, you can get one specified task scheduler with qualified name, and schedule the job as you want. – Mavlarn Aug 04 '16 at 07:08
  • do you have an example code? it's odd that it isn't possible to create only one scheduled task – luca Aug 04 '16 at 07:45
  • For qualifier, check http://stackoverflow.com/questions/9182805/spring-attaching-qualifer-to-java-configured-beans , And TaskScheduler bean config, check the example here: https://github.com/joshlong/boot-examples/blob/master/websockets/src/main/java/demo/Application.java – Mavlarn Aug 04 '16 at 08:06