parent
ce673741c0
commit
af6c46743f
@ -0,0 +1,37 @@
|
||||
<?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>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>xxl-job-executor-sample-frameless</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>Example executor project for spring boot.</description>
|
||||
<url>http://www.xuxueli.com/</url>
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- slf4j -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>${slf4j-api.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- xxl-job-core -->
|
||||
<dependency>
|
||||
<groupId>com.xuxueli</groupId>
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,33 @@
|
||||
package com.xuxueli.executor.sample.frameless;
|
||||
|
||||
import com.xuxueli.executor.sample.frameless.config.FrameLessXxlJobConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author xuxueli 2018-10-31 19:05:43
|
||||
*/
|
||||
public class Application {
|
||||
private static Logger logger = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
// start
|
||||
FrameLessXxlJobConfig.getInstance().initXxlJobExecutor();
|
||||
|
||||
while (true) {
|
||||
TimeUnit.HOURS.sleep(1);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
} finally {
|
||||
// destory
|
||||
FrameLessXxlJobConfig.getInstance().destoryXxlJobExecutor();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package com.xuxueli.executor.sample.frameless.config;
|
||||
|
||||
import com.xuxueli.executor.sample.frameless.jobhandler.DemoJobHandler;
|
||||
import com.xuxueli.executor.sample.frameless.jobhandler.HttpJobHandler;
|
||||
import com.xuxueli.executor.sample.frameless.jobhandler.ShardingJobHandler;
|
||||
import com.xxl.job.core.executor.XxlJobExecutor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author xuxueli 2018-10-31 19:05:43
|
||||
*/
|
||||
public class FrameLessXxlJobConfig {
|
||||
private static Logger logger = LoggerFactory.getLogger(FrameLessXxlJobConfig.class);
|
||||
|
||||
|
||||
private static FrameLessXxlJobConfig instance = new FrameLessXxlJobConfig();
|
||||
public static FrameLessXxlJobConfig getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
private XxlJobExecutor xxlJobExecutor = null;
|
||||
|
||||
/**
|
||||
* init
|
||||
*/
|
||||
public void initXxlJobExecutor() {
|
||||
|
||||
// registry jobhandler
|
||||
XxlJobExecutor.registJobHandler("demoJobHandler", new DemoJobHandler());
|
||||
XxlJobExecutor.registJobHandler("shardingJobHandler", new ShardingJobHandler());
|
||||
XxlJobExecutor.registJobHandler("httpJobHandler", new HttpJobHandler());
|
||||
|
||||
// load executor prop
|
||||
Properties xxlJobProp = loadProperties("xxl-job-executor.properties");
|
||||
|
||||
|
||||
// init executor
|
||||
xxlJobExecutor = new XxlJobExecutor();
|
||||
xxlJobExecutor.setAdminAddresses(xxlJobProp.getProperty("xxl.job.admin.addresses"));
|
||||
xxlJobExecutor.setAppName(xxlJobProp.getProperty("xxl.job.executor.appname"));
|
||||
xxlJobExecutor.setIp(xxlJobProp.getProperty("xxl.job.executor.ip"));
|
||||
xxlJobExecutor.setPort(Integer.valueOf(xxlJobProp.getProperty("xxl.job.executor.port")));
|
||||
xxlJobExecutor.setAccessToken(xxlJobProp.getProperty("xxl.job.accessToken"));
|
||||
xxlJobExecutor.setLogPath(xxlJobProp.getProperty("xxl.job.executor.logpath"));
|
||||
xxlJobExecutor.setLogRetentionDays(Integer.valueOf(xxlJobProp.getProperty("xxl.job.executor.logretentiondays")));
|
||||
|
||||
// start executor
|
||||
try {
|
||||
xxlJobExecutor.start();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* destory
|
||||
*/
|
||||
public void destoryXxlJobExecutor() {
|
||||
if (xxlJobExecutor != null) {
|
||||
xxlJobExecutor.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Properties loadProperties(String propertyFileName) {
|
||||
InputStreamReader in = null;
|
||||
try {
|
||||
ClassLoader loder = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
in = new InputStreamReader(loder.getResourceAsStream(propertyFileName), "UTF-8");;
|
||||
if (in != null) {
|
||||
Properties prop = new Properties();
|
||||
prop.load(in);
|
||||
return prop;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("load {} error!", propertyFileName);
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
logger.error("close {} error!", propertyFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.xuxueli.executor.sample.frameless.jobhandler;
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.log.XxlJobLogger;
|
||||
import org.eclipse.jetty.client.HttpClient;
|
||||
import org.eclipse.jetty.client.api.ContentResponse;
|
||||
import org.eclipse.jetty.client.api.Request;
|
||||
import org.eclipse.jetty.http.HttpMethod;
|
||||
import org.eclipse.jetty.http.HttpStatus;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 跨平台Http任务
|
||||
*
|
||||
* @author xuxueli 2018-09-16 03:48:34
|
||||
*/
|
||||
public class HttpJobHandler extends IJobHandler {
|
||||
|
||||
@Override
|
||||
public ReturnT<String> execute(String param) throws Exception {
|
||||
|
||||
// valid
|
||||
if (param==null || param.trim().length()==0) {
|
||||
XxlJobLogger.log("URL Empty");
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
// httpclient
|
||||
HttpClient httpClient = null;
|
||||
try {
|
||||
httpClient = new HttpClient();
|
||||
httpClient.setFollowRedirects(false); // Configure HttpClient, for example:
|
||||
httpClient.start(); // Start HttpClient
|
||||
|
||||
// request
|
||||
Request request = httpClient.newRequest(param);
|
||||
request.method(HttpMethod.GET);
|
||||
request.timeout(5000, TimeUnit.MILLISECONDS);
|
||||
|
||||
// invoke
|
||||
ContentResponse response = request.send();
|
||||
if (response.getStatus() != HttpStatus.OK_200) {
|
||||
XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatus());
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
String responseMsg = response.getContentAsString();
|
||||
XxlJobLogger.log(responseMsg);
|
||||
return SUCCESS;
|
||||
} catch (Exception e) {
|
||||
XxlJobLogger.log(e);
|
||||
return FAIL;
|
||||
} finally {
|
||||
if (httpClient != null) {
|
||||
httpClient.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.xuxueli.executor.sample.frameless.jobhandler;
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.log.XxlJobLogger;
|
||||
import com.xxl.job.core.util.ShardingUtil;
|
||||
|
||||
/**
|
||||
* 分片广播任务
|
||||
*
|
||||
* @author xuxueli 2017-07-25 20:56:50
|
||||
*/
|
||||
public class ShardingJobHandler extends IJobHandler {
|
||||
|
||||
@Override
|
||||
public ReturnT<String> execute(String param) throws Exception {
|
||||
|
||||
// 分片参数
|
||||
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
|
||||
XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
|
||||
// 业务逻辑
|
||||
for (int i = 0; i < shardingVO.getTotal(); i++) {
|
||||
if (i == shardingVO.getIndex()) {
|
||||
XxlJobLogger.log("第 {} 片, 命中分片开始处理", i);
|
||||
} else {
|
||||
XxlJobLogger.log("第 {} 片, 忽略", i);
|
||||
}
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" threshold="null" debug="null">
|
||||
|
||||
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} xxl-job-executor-sample-frameless [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
|
||||
<param name="file" value="/data/applogs/xxl-job/xxl-job-executor-sample-frameless.log"/>
|
||||
<param name="append" value="true"/>
|
||||
<param name="encoding" value="UTF-8"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} xxl-job-executor-sample-frameless [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<root>
|
||||
<level value="INFO" />
|
||||
<appender-ref ref="CONSOLE" />
|
||||
<appender-ref ref="FILE" />
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
@ -0,0 +1,15 @@
|
||||
### 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=9995
|
||||
|
||||
### 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
|
Loading…
Reference in new issue