Get the App
SLTechnology News&Howtos  ›  Development  › 

How to configure ShedLock distributed timing tasks in SpringBoot Framework

Shulou Source: shulou.com Published: 2022-06-01 12:44:51 09月12日 Update

This article mainly introduces the "SpringBoot framework how to configure ShedLock distributed timing tasks" related knowledge, the editor through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope that this "SpringBoot framework how to configure ShedLock distributed timing tasks" article can help you solve the problem.

What is ShedLock?

ShedLock is a timed task framework used in a distributed environment to solve the problem that the same scheduled tasks of multiple instances in a distributed environment are executed repeatedly at the same point in time. The solution is to record and lock a table in a common database. At the same time, only the first node that executes the scheduled task and successfully writes the corresponding record in the database table can execute successfully, while other nodes skip the task directly. Of course, it is not just the database, the data storage types that have been implemented include MongoDB,Zookeeper,Redis,Hazelcast in addition to the classic relational database.

How to use

ShedLock adopts the idea of non-invasive programming and realizes the corresponding functions by means of annotations.

To use ShedLock, do the following

Enable and configure schedule locking

Comment on your scheduled task

Configure lock provider

1. Enable and configure schedule locking

First of all, introduce dependency

< dependency >

< groupId >

Net.javacrumbs.shedlock

< artifactId >

Shedlock-spring

< version >

2.5.0

Now we need to integrate the library into Spring. To enable schedule locking, use the @ EnableSchedulerLock annotation

@ Configuration @ EnableScheduling @ EnableSchedulerLock (defaultLockAtMostFor = "PT30S") class MySpringConfiguration {...}

Comment on your scheduled task

Import net.javacrumbs.shedlock.core.SchedulerLock;... @ Scheduled (...) @ SchedulerLock (name = "scheduledTaskName") public void scheduledTask () {/ / do sth}

The @ SchedulerLock annotation supports a total of five parameters, namely

Name is used to mark the name of a timing service, and it is used to write to the database as an identification to distinguish different services. If there are multiple timing tasks with the same name, only one of them executes successfully at the same time.

The maximum time (in milliseconds ms) that a node that successfully executes a task can have an exclusive lock

String representation of the longest exclusive lock that can be owned by the node of the lockAtMostForString that successfully executed the task, for example, "PT14M" is expressed as 14 minutes

The shortest time (in milliseconds ms) that a node that successfully executes a task in lockAtLeastFor can own an exclusive location

A string expression of the shortest time that the node of the lockAtLeastForString successfully executing the task can have an exclusive lock, for example, "PT14M" is expressed as 14 minutes

Integrated with Spring, ShedLock supports two Spring integration modes.

TaskScheduler Agent

Two Bean needs to be configured, one is lockProvider and the other is scheduler

By default, ShedLock creates an AOP proxy TaskScheduler around the Spring. If you do not specify a task scheduler, a default task scheduler is created for you. If you have special needs, just create a bean to implement the TaskScheduler interface, which will be automatically wrapped into the AOP proxy.

Because ShedLock uses Mongo,JDBC databases, Redis,Hazelcast,ZooKeeper and other external storage for coordination. So I chose redis.

Net.javacrumbs.shedlock shedlock-provider-redis-spring 2.5.0 org.springframework.boot spring-boot-starter-data-redis @ Configuration@EnableScheduling@EnableSchedulerLock (defaultLockAtMostFor = "PT5M") public class ShedLockConfig {@ Bean public LockProvider lockProvider (RedisTemplate redisTemplate) {return new RedisLockProvider (redisTemplate.getConnectionFactory ());} @ Bean public TaskScheduler taskScheduler () {return new MySpecialTaskScheduler ();}} scheduled method Agent

If you have more special needs, you can use the Scheduled Method agent like this

@ EnableSchedulerLock (mode = PROXY_METHOD,defaultLockAtMostFor = "PT30S")

If PROXY_METHOD selects mode, ShedLock creates an AOP proxy around each method annotated with @ SchedulerLock. The main advantage of this approach is that it does not rely on Spring scheduling. The disadvantage is that locking is applied even if you call the method directly. Also note that currently only void return methods are supported, and if you comment and call a method with a non-void return type, an exception will be thrown.

This is the end of the introduction on "how to configure ShedLock distributed scheduled tasks in the SpringBoot framework". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Task configuration success data method time database node agent distributed annotation framework support knowledge program different longest special unit only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple Huawei Shulou Information Microsoft