Elastic job dynamically sets scheduled tasks and elasticjob dynamically
1. Version
<!-- import elastic-job lite core --> <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-core</artifactId> <version>2.1.3</version> </dependency> <!-- import other module if need --> <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-spring</artifactId> <version>2.1.3</version> </dependency>
2. Configuration
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:reg="http://www.dangdang.com/schema/ddframe/reg" xmlns:job="http://www.dangdang.com/schema/ddframe/job" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.dangdang.com/schema/ddframe/reg http://www.dangdang.com/schema/ddframe/reg/reg.xsd http://www.dangdang.com/schema/ddframe/job http://www.dangdang.com/schema/ddframe/job/job.xsd "> <!--configure registry center --> <reg:zookeeper id="regCenter" server-lists="${job.registry.address}" namespace="${job.namespace}" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" /> <!--configure job --> <!--<job:simple id="myElasticJob" class="com.zhuanche.util.MyElasticJob" registry-center-ref="regCenter" cron="0/10 * * * * ?" sharding-total-count="1" sharding-item-parameters="0=A" />--></beans>
Code 3.1
Import com. dangdang. ddframe. job. config. jobCoreConfiguration; import com. dangdang. ddframe. job. config. simple. simpleJobConfiguration; import com. dangdang. ddframe. job. lite. api. jobScheduler; import com. dangdang. ddframe. job. lite. config. liteJobConfiguration; import com. dangdang. ddframe. job. reg. zookeeper. zookeeperRegistryCenter; String cron = DateUtil. getCron (activityEndTime); logger.info ("[scheduled task execution time] cron = {}", cron); int shardingTotalCount = 1; String jobName = UUID. randomUUID (). toString () + "-" + numprizeBaseId; JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration. newBuilder (jobName, cron, shardingTotalCount ). build (); SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration (jobCoreConfiguration, DynamicElasticJob. class. getCanonicalName (); JobScheduler jobschedcenter = new jobschedcenter (zookeeperRegistryCenter, LiteJobConfiguration. newBuilder (simpleJobConfiguration ). build (); try {jobScheduler. init ();} catch (Exception e) {e. printStackTrace (); throw new RuntimeException ("scheduled task creation failed ");}
3.2 tools
Import java. text. simpleDateFormat; import java. util. date;/*** Created by admin on April /10/23. */public class DateUtil {private static final String CRON_DATE_FORMAT = "ss mm HH dd MM? Yyyy ";/***** @ param date time * @ return cron type Date */public static String getCron (final date) {SimpleDateFormat sdf = new SimpleDateFormat (CRON_DATE_FORMAT ); string formatTimeStr = ""; if (date! = Null) {formatTimeStr = sdf. format (date) ;}return formatTimeStr ;}}