1.DownloadTable structure and mee_timed dependencies Dependencies and stored in the project or nexus private service
Define dependency dependencies in the
<dependency>
<groupId></groupId>
<artifactId>mee_timed</artifactId>
<version>1.0.1</version>
<scope>system</scope>
<systemPath>${}/src/main/resources/lib/mee_timed-1.0.</systemPath>
</dependency>
3. Import table structure (SQL)
Depending on the useddb
If you want to import the table structure supported by the corresponding vendor, you can do so only in the following waysmysql
、oracle
、postgresql
Support:
table_mysql.sql
table_oracle.sql
table_postgresql.sql
4. Define the configuration and beans
Currently there are only three configurations:
=${}
-name=SYS_SHEDLOCK_JOB
-app-name=SYS_SHEDLOCK_APP
where the configuration item-app-name
It is used to manage clusters and nodes, but can be left unconfigured if not needed.
Necessary initialization parameters are automatically written when the application starts, or initial data can be imported in advance.
Configure bean: This step is non-essential, only the configuration of the internal thread pool is more conservative, if you need to customize the following configuration to specify the number of threads and thread name prefix:
/**
* Setting the number of execution threads
* @return
*/
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
(PROCESSOR*2);
("SHEDLOCK-");
();
return scheduler;
}
5. Define timed tasks
Sample 1.
import ;
import ;
import ;
import ;
import org.;
import org.;
import ;
import ;
@Component
public class Job01TestService implements Job {
private static final Logger LOGGER = ();
@MeeTimed(fixedRate = 10000,lockAtLeastFor = "PT5S",lockAtMostFor ="PT5S" )
public void exec01() throws InterruptedException {
("=====> [exec01] Already Executed! <=====");
(6);
}
@MeeTimeds({
@MeeTimed(cron = "10,20,30,40,50 * * * * ?",lockAtMostFor ="PT5S",lockName = "execute1"),
@MeeTimed(cron = "0 0/2 * * * ?",lockAtMostFor ="PT1M",lockName = "execute2"),
@MeeTimed(cron = "0 0/4 * ? * MON-FRI",lockAtMostFor ="PT1M",lockName = "execute3"),
// New York time every year7moon9horn (wind instrument)22point (in space or time)2sub-executive
@MeeTimed(cron = "0 2 22 9 7 ?",lockAtMostFor ="PT1M",lockName = "execute4",zone = "America/New_York"),
// 每moon最后一天的十point (in space or time)半(eg:2024-07-31 10:30:00)
@MeeTimed(cron = "0 30 10 L * ?",lockAtMostFor ="PT1M",lockName = "execute5")
})
@Override
public void execute(JobExecutionContext context) {
("=====> proxy job exec! data:"+().getName()+" <=====");
try {
(8);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Sample two.
package ;
import ;
import ;
import org.;
import org.;
import ;
@Component
public class ScheduledTasks {
private static final Logger LOGGER = ();
@MeeTimeds({
@MeeTimed(fixedRate = 10000,lockAtLeastFor = "PT5S",lockAtMostFor ="PT5S",lockName = "T1"),
@MeeTimed(fixedDelay = 8000,lockAtLeastFor = "PT5S",lockAtMostFor ="PT5S",lockName = "T2"),
})
public void exec01() {
("=====> [exec01] Already Executed! <=====");
}
@MeeTimed(cron = "0/20 * * * * ?",lockAtLeastFor = "PT5S",lockAtMostFor ="PT10S" )
public void exec02(JobExecutionContext context) {
("=====> proxy job exec! data:"+()+" <=====");
}
}
Either way is fine, but if you need to pass an argument, the formal parameter of the function must beJobExecutionContext
or its implementation class
In case of multiple time configurations of the same function (using@MeeTimeds
configuration), each of itslockName
It cannot be empty!