diff --git a/xxl-job-executor-samples/xxl-job-executor-db/pom.xml b/xxl-job-executor-samples/xxl-job-executor-db/pom.xml new file mode 100644 index 00000000..3744960e --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + com.xuxueli + xxl-job-executor-samples + 1.9.1-SNAPSHOT + + xxl-job-executor-db + jar + + ${project.artifactId} + Example executor project for spring boot. + http://www.xuxueli.com/ + + + UTF-8 + UTF-8 + 1.7 + + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring-boot.version} + pom + import + + + + + org.eclipse.jetty + jetty-server + ${jetty-server.version} + + + org.eclipse.jetty + jetty-util + ${jetty-server.version} + + + org.eclipse.jetty + jetty-http + ${jetty-server.version} + + + org.eclipse.jetty + jetty-io + ${jetty-server.version} + + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + + com.xuxueli + xxl-job-core + ${project.parent.version} + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/Application.java b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/Application.java new file mode 100644 index 00000000..51a0e457 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/Application.java @@ -0,0 +1,13 @@ +package com.xxl.job.executor; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java new file mode 100644 index 00000000..a90e43e1 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java @@ -0,0 +1,58 @@ +package com.xxl.job.executor.core.config; + +import com.xxl.job.core.executor.XxlJobExecutor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +/** + * xxl-job config + * + * @author xuxueli 2017-04-28 + */ +@Configuration +@ComponentScan(basePackages = "com.xxl.job.executor.service.jobhandler") +public class XxlJobConfig { + private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); + + @Value("${xxl.job.admin.addresses}") + private String adminAddresses; + + @Value("${xxl.job.executor.appname}") + private String appName; + + @Value("${xxl.job.executor.ip}") + private String ip; + + @Value("${xxl.job.executor.port}") + private int port; + + @Value("${xxl.job.accessToken}") + private String accessToken; + + @Value("${xxl.job.executor.logpath}") + private String logPath; + + @Value("${xxl.job.executor.logretentiondays}") + private int logRetentionDays; + + + @Bean(initMethod = "start", destroyMethod = "destroy") + public XxlJobExecutor xxlJobExecutor() { + logger.info(">>>>>>>>>>> xxl-job config init."); + XxlJobExecutor xxlJobExecutor = new XxlJobExecutor(); + xxlJobExecutor.setAdminAddresses(adminAddresses); + xxlJobExecutor.setAppName(appName); + xxlJobExecutor.setIp(ip); + xxlJobExecutor.setPort(port); + xxlJobExecutor.setAccessToken(accessToken); + xxlJobExecutor.setLogPath(logPath); + xxlJobExecutor.setLogRetentionDays(logRetentionDays); + + return xxlJobExecutor; + } + +} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/mvc/controller/IndexController.java b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/mvc/controller/IndexController.java new file mode 100644 index 00000000..37c90719 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/mvc/controller/IndexController.java @@ -0,0 +1,18 @@ +//package com.xxl.job.executor.mvc.controller; +// +//import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.RequestMapping; +//import org.springframework.web.bind.annotation.ResponseBody; +// +//@Controller +//@EnableAutoConfiguration +//public class IndexController { +// +// @RequestMapping("/") +// @ResponseBody +// String index() { +// return "xxl job executor running."; +// } +// +//} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/service/jobhandler/RiskCountStatisticsJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/service/jobhandler/RiskCountStatisticsJobHandler.java new file mode 100644 index 00000000..6bafe975 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/service/jobhandler/RiskCountStatisticsJobHandler.java @@ -0,0 +1,33 @@ +package com.xxl.job.executor.service.jobhandler; + +import java.util.concurrent.TimeUnit; + +import org.springframework.stereotype.Component; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.JobHandler; +import com.xxl.job.core.log.XxlJobLogger; + +/** + * 任务Handler示例(Bean模式) + * + * 开发步骤: 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”; + * 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例; + * 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。 + * 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志; + * + * @author xuxueli 2015-12-19 19:43:36 + */ +@JobHandler(value = "risk-count-statistics") +@Component +public class RiskCountStatisticsJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String param) throws Exception { + XxlJobLogger.log("XXL-JOB, 1 Hello World."); + XxlJobLogger.log("beat at:"); + return SUCCESS; + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java new file mode 100644 index 00000000..b58871fe --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java @@ -0,0 +1,39 @@ +package com.xxl.job.executor.service.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.JobHandler; +import com.xxl.job.core.log.XxlJobLogger; +import com.xxl.job.core.util.ShardingUtil; +import org.springframework.stereotype.Service; + + +/** + * 分片广播任务 + * + * @author xuxueli 2017-07-25 20:56:50 + */ +@JobHandler(value="shardingJobHandler") +@Service +public class ShardingJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String param) throws Exception { + + // 分片参数 + ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); + XxlJobLogger.log("分片参数:当前分片序号 = {0}, 总分片数 = {1}", shardingVO.getIndex(), shardingVO.getTotal()); + + // 业务逻辑 + for (int i = 0; i < shardingVO.getTotal(); i++) { + if (i == shardingVO.getIndex()) { + XxlJobLogger.log("第 {0} 片, 命中分片开始处理", i); + } else { + XxlJobLogger.log("第 {0} 片, 忽略", i); + } + } + + return SUCCESS; + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-db/src/main/resources/application.properties b/xxl-job-executor-samples/xxl-job-executor-db/src/main/resources/application.properties new file mode 100644 index 00000000..4086953a --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/src/main/resources/application.properties @@ -0,0 +1,23 @@ +# web port +server.port=9001 + +# log config +logging.config=classpath:logback.xml + + +### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" +xxl.job.admin.addresses=http://127.0.0.1:8090 +#xxl.job.admin.addresses=http://dispatch-center.infincash.com + +### xxl-job executor address +xxl.job.executor.appname=executor-001 +xxl.job.executor.ip= +xxl.job.executor.port=9002 + +### xxl-job, access token +xxl.job.accessToken= + +### xxl-job log path +xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler +### xxl-job log retention days +xxl.job.executor.logretentiondays=-1 diff --git a/xxl-job-executor-samples/xxl-job-executor-db/src/main/resources/logback.xml b/xxl-job-executor-samples/xxl-job-executor-db/src/main/resources/logback.xml new file mode 100644 index 00000000..bbea067c --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/src/main/resources/logback.xml @@ -0,0 +1,37 @@ + + + + logback + + + + + + %d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n + + + + + ${log.path} + + ${log.path}.%d{yyyy-MM-dd}.zip + + + %date %level [%thread] %logger{36} [%file : %line] %msg%n + + + + + + + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-db/src/test/java/com/xxl/job/executor/test/XxlJobExecutorExampleBootApplicationTests.java b/xxl-job-executor-samples/xxl-job-executor-db/src/test/java/com/xxl/job/executor/test/XxlJobExecutorExampleBootApplicationTests.java new file mode 100644 index 00000000..9739989a --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-db/src/test/java/com/xxl/job/executor/test/XxlJobExecutorExampleBootApplicationTests.java @@ -0,0 +1,24 @@ +package com.xxl.job.executor.test; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.xxl.job.executor.Application; +import com.xxl.job.executor.service.jobhandler.RiskCountStatisticsJobHandler; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class ,webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) +public class XxlJobExecutorExampleBootApplicationTests { + + @Autowired + RiskCountStatisticsJobHandler jobHandler; + + @Test + public void test() throws Exception { + jobHandler.execute(""); + } + +} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties index 2f0c3661..854355f7 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties @@ -1,12 +1,14 @@ # web port -server.port=8081 +server.port=9000 # log config logging.config=classpath:logback.xml ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" -xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin +#xxl.job.admin.addresses=http://127.0.0.1:8090 +xxl.job.admin.addresses=http://dispatch-center.infincash.com + ### xxl-job executor address xxl.job.executor.appname=xxl-job-executor-sample