Redisson: Distributed scheduled executor service with Spring Boot

Created on 24 Jan 2020  路  4Comments  路  Source: redisson/redisson

Hello there.

I try to use Redisson for schedule tasks in my Spring Boot application. And I have the next issue: after application restart with graceful shutdown - Redison scheduled executor service is in shutdown state, and all my tasks are rejected with exception
java.util.concurrent.RejectedExecutionException: Task rejected. ExecutorService is in shutdown state

Here is my code:
I use Distributed scheduled executor service like a bean

@Configuration
public class RedissonScheduledExecutorConfig {

    private static final String EXECUTOR_SERVICE_NAME = "rScheduledExecutor";

    @Bean
    public RScheduledExecutorService rScheduledExecutorService(
            final RedissonClient redissonClient,
            final BeanFactory beanFactory
    ) {
        final WorkerOptions workerOptions = WorkerOptions.defaults().workers(1).beanFactory(beanFactory);
        final ExecutorOptions executorOptions = ExecutorOptions.defaults()
                .taskRetryInterval(0, TimeUnit.SECONDS);
        final RScheduledExecutorService executorService = redissonClient
                .getExecutorService(EXECUTOR_SERVICE_NAME, executorOptions);
        executorService.registerWorkers(workerOptions);
        return executorService;
    }

}

Send the task to executor:

@RestController
@RequiredArgsConstructor
public final class TestController {

    private final RScheduledExecutorService rScheduledExecutorService;

    @GetMapping("/test")
    public String test() {
        final RScheduledFuture<?> rScheduledFuture = this.rScheduledExecutorService.schedule(
                new RunnableTask(),
                60,
                TimeUnit.SECONDS
        );
    }

}

Task implementation:

public final class RunnableTask implements Runnable, Serializable {

    private static final long serialVersionUID = 1L;

    @Autowired
    private transient MyService myService;

    @Override
    public void run() {
        this.myService.sendEmail();
    }

}

I use spring boot version - 2.1.7.RELEASE, and redisson-spring-boot-starter with version 3.11.5

Thank you for any help!

question

Most helpful comment

Thanks, I have added destroyMethod = "" argument to @Bean annotation.

All 4 comments

Make sure you don't call RScheduledExecutorService.shutdown this cause to such issue.

Thanks, I have added destroyMethod = "" argument to @Bean annotation.

But, is it ok if RScheduledExecutorService will be destroyed without shutdown?

Yes, it doesn't spin up threads until registerWorkers method nvoked.

Was this page helpful?
0 / 5 - 0 ratings