Elastic job dynamically sets scheduled tasks and elasticjob dynamically

Source: Internet
Author: User

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 ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.