parent
d47658b982
commit
8e49c70838
@ -0,0 +1,17 @@
|
|||||||
|
# xxl-job-executor-sample-nutzboot
|
||||||
|
|
||||||
|
用NutzBoot作为xxl-job-executor的示例
|
||||||
|
|
||||||
|
## 文件介绍
|
||||||
|
|
||||||
|
* MainLauncher.java NutzBoot启动类, 其中的init方法,扫描/加载/注册ioc容器内的IJobHandler
|
||||||
|
* XxlJobConfig.java 读取配置信息,声明XxlJobExecutor对象
|
||||||
|
* ShardingJobHandler.java和DemoJobHandler.java 2个示例IJobHandler实现类
|
||||||
|
|
||||||
|
## 环境要求
|
||||||
|
|
||||||
|
* JDK8u112 以上
|
||||||
|
|
||||||
|
## 我有疑问?
|
||||||
|
|
||||||
|
请访问 https://nutz.cn 获取帮助
|
@ -0,0 +1,104 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.xuxueli</groupId>
|
||||||
|
<artifactId>xxl-job-executor-samples</artifactId>
|
||||||
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>xxl-job-executor-sample-nutzboot</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>${project.artifactId}</name>
|
||||||
|
<description>Example executor project for nutzboot. https://nutz.io</description>
|
||||||
|
<url>http://www.xuxueli.com/</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<nutzboot.version>2.0</nutzboot.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.nutz</groupId>
|
||||||
|
<artifactId>nutzboot-starter-jetty</artifactId>
|
||||||
|
<version>${nutzboot.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.nutz</groupId>
|
||||||
|
<artifactId>nutzboot-starter-nutz-mvc</artifactId>
|
||||||
|
<version>${nutzboot.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- xxl-job-core -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.xuxueli</groupId>
|
||||||
|
<artifactId>xxl-job-core</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<!-- NutzBoot use Jetty 9.4.x -->
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-server</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>javax.servlet.jsp</groupId>
|
||||||
|
<artifactId>jsp-api</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
<version>1.7.25</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.nutz</groupId>
|
||||||
|
<artifactId>nutzboot-starter</artifactId>
|
||||||
|
<version>${nutzboot.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<transformers>
|
||||||
|
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
|
||||||
|
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||||
|
<resource>META-INF/nutz/org.nutz.boot.starter.NbStarter</resource>
|
||||||
|
</transformer>
|
||||||
|
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>com.xxl.job.executor.MainLauncher</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.xxl.job.executor;
|
||||||
|
|
||||||
|
import org.nutz.boot.NbApp;
|
||||||
|
import org.nutz.ioc.Ioc;
|
||||||
|
import org.nutz.ioc.loader.annotation.Inject;
|
||||||
|
import org.nutz.ioc.loader.annotation.IocBean;
|
||||||
|
import org.nutz.mvc.annotation.At;
|
||||||
|
import org.nutz.mvc.annotation.Ok;
|
||||||
|
|
||||||
|
import com.xxl.job.core.executor.XxlJobExecutor;
|
||||||
|
import com.xxl.job.core.handler.IJobHandler;
|
||||||
|
import com.xxl.job.core.handler.annotation.JobHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用NutzBoot作为xxl job executor的demo,简单修改后也可用于NutzMvc项目.
|
||||||
|
* <p/>
|
||||||
|
* 如有任何疑问,请访问 https://nutz.cn
|
||||||
|
*
|
||||||
|
* @author wendal(wendal1985@gmail.com)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@IocBean(create = "init")
|
||||||
|
public class MainLauncher {
|
||||||
|
|
||||||
|
@Inject("refer:$ioc")
|
||||||
|
protected Ioc ioc;
|
||||||
|
|
||||||
|
@At("/")
|
||||||
|
@Ok("raw")
|
||||||
|
public String index() {
|
||||||
|
// demo嘛, 简单做个入口方法,显示一句话就够了
|
||||||
|
return "xxl job executor running.";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果是普通Nutz MVC项目,这段代码放到MainSetup.init方法内就可以了
|
||||||
|
public void init() {
|
||||||
|
// 从ioc容器中找出所有实现了IJobHandler接口的对象,注册到XxlJobExecutor
|
||||||
|
for (String jobHandlerBeanName : ioc.getNamesByType(IJobHandler.class)) {
|
||||||
|
// 获取JobHandler实例
|
||||||
|
IJobHandler jobHandler = ioc.get(IJobHandler.class, jobHandlerBeanName);
|
||||||
|
// 看看有没有@JobHandler注解
|
||||||
|
JobHandler annoJobHandler = jobHandler.getClass().getAnnotation(JobHandler.class);
|
||||||
|
// 得到jobHandlerName
|
||||||
|
String jobHandlerName = annoJobHandler == null ? jobHandlerBeanName : annoJobHandler.value();
|
||||||
|
// 注册到XxlJobExecutor上下文
|
||||||
|
XxlJobExecutor.registJobHandler(jobHandlerName, jobHandler);
|
||||||
|
}
|
||||||
|
// 获取XxlJobExecutor,从而触发XxlJobExecutor的初始化
|
||||||
|
ioc.getByType(XxlJobExecutor.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new NbApp().run(); // 启动一切
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.xxl.job.executor.service.jobhandler;
|
||||||
|
|
||||||
|
import org.nutz.ioc.loader.annotation.IocBean;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分片广播任务
|
||||||
|
*
|
||||||
|
* @author xuxueli 2017-07-25 20:56:50
|
||||||
|
*/
|
||||||
|
@JobHandler(value="shardingJobHandler")
|
||||||
|
@IocBean
|
||||||
|
public class ShardingJobHandler extends IJobHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReturnT<String> execute(String... params) 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 ReturnT.SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.xxl.job.executor.service.jobhandler;
|
||||||
|
|
||||||
|
import org.nutz.ioc.impl.PropertiesProxy;
|
||||||
|
import org.nutz.ioc.loader.annotation.Inject;
|
||||||
|
import org.nutz.ioc.loader.annotation.IocBean;
|
||||||
|
import org.nutz.log.Log;
|
||||||
|
import org.nutz.log.Logs;
|
||||||
|
|
||||||
|
import com.xxl.job.core.executor.XxlJobExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xxl-job config
|
||||||
|
*
|
||||||
|
* @author wendal 2017-12-27
|
||||||
|
*/
|
||||||
|
@IocBean
|
||||||
|
public class XxlJobConfig {
|
||||||
|
|
||||||
|
private static Log log = Logs.get();
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
protected PropertiesProxy conf;
|
||||||
|
|
||||||
|
@IocBean(create = "start", depose = "destroy")
|
||||||
|
public XxlJobExecutor xxlJobExecutor() {
|
||||||
|
log.info(">>>>>>>>>>> xxl-job config init.");
|
||||||
|
XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
|
||||||
|
// 下列参数均为必填项,声明在application.properties
|
||||||
|
xxlJobExecutor.setIp(conf.check("xxl.job.executor.ip"));
|
||||||
|
xxlJobExecutor.setPort(conf.getInt("xxl.job.executor.port"));
|
||||||
|
xxlJobExecutor.setAppName(conf.check("xxl.job.executor.appname"));
|
||||||
|
xxlJobExecutor.setAdminAddresses(conf.check("xxl.job.admin.addresses"));
|
||||||
|
xxlJobExecutor.setLogPath(conf.check("xxl.job.executor.logpath"));
|
||||||
|
xxlJobExecutor.setAccessToken(conf.check("xxl.job.accessToken"));
|
||||||
|
return xxlJobExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
# web port
|
||||||
|
jetty.port=8081
|
||||||
|
|
||||||
|
# xxl-job
|
||||||
|
### 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 executor address
|
||||||
|
xxl.job.executor.appname=xxl-job-executor-sample
|
||||||
|
xxl.job.executor.ip=
|
||||||
|
xxl.job.executor.port=-1
|
||||||
|
|
||||||
|
### xxl-job log path
|
||||||
|
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/
|
||||||
|
|
||||||
|
### xxl-job, access token
|
||||||
|
xxl.job.accessToken=
|
@ -0,0 +1,8 @@
|
|||||||
|
log4j.rootLogger=info,Console
|
||||||
|
|
||||||
|
log4j.logger.org.nutz=info
|
||||||
|
log4j.logger.org.eclipse.jetty=info
|
||||||
|
|
||||||
|
log4j.appender.Console=org.apache.log4j.ConsoleAppender
|
||||||
|
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
|
||||||
|
log4j.appender.Console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss.SSS}] %5p [%t] --- %c{1}: %m%n
|
Loading…
Reference in new issue