Commit baf65d97 authored by 刘晓滨's avatar 刘晓滨

Merge branch 'master_1204_cy' into 'master'

新增xxl-job-springboot-bak See merge request !5
parents 0e27fc29 a360eaa5
<?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.1-SNAPSHOT</version>
</parent>
<artifactId>com-spacetech-service-xxl-job-springboot-bak</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Example executor project for spring boot.</description>
<url>http://www.xuxueli.com/</url>
<properties>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot (依赖管理:继承一些默认的依赖,工程需要依赖的jar包的管理,申明其他dependency的时候就不需要version) -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty-server.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty-server.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>${jetty-server.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>${jetty-server.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${jetty-server.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- spring-boot-starter-web (spring-webmvc + tomcat) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<!-- 公用的服务接口 -->
<dependency>
<groupId>com.spacetech.gateway</groupId>
<artifactId>gateway</artifactId>
<version>2.98-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- spring-boot-maven-plugin (提供了直接运行项目的插件:如果是通过parent方式继承spring-boot-starter-parent则不用此插件) -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.xxl.job.executor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* @author xuxueli 2018-10-28 00:38:13
*/
@SpringBootApplication
public class XxlJobExecutorApplication {
public static void main(String[] args) {
SpringApplication.run(XxlJobExecutorApplication.class, args);
}
}
\ No newline at end of file
package com.xxl.job.executor.core.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
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;
import org.springframework.context.annotation.ImportResource;
/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@Configuration
@ComponentScan(basePackages = "com.xxl.job.executor.service.jobhandler")
@ImportResource(locations = "classpath:spacetech-consumer-apihub.xml")
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 XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
\ No newline at end of file
//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
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 org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* 命令行任务
*
* @author xuxueli 2018-09-16 03:48:34
*/
@JobHandler(value="commandJobHandler")
@Component
public class CommandJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
String command = param;
int exitValue = -1;
BufferedReader bufferedReader = null;
try {
// command process
Process process = Runtime.getRuntime().exec(command);
BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
// command log
String line;
while ((line = bufferedReader.readLine()) != null) {
XxlJobLogger.log(line);
}
// command exit
process.waitFor();
exitValue = process.exitValue();
} catch (Exception e) {
XxlJobLogger.log(e);
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
if (exitValue == 0) {
return IJobHandler.SUCCESS;
} else {
return new ReturnT<String>(IJobHandler.FAIL.getCode(), "command exit value("+exitValue+") is failed");
}
}
}
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 org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* 任务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="demoJobHandler")
@Component
public class DemoJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
XxlJobLogger.log("XXL-JOB, Hello World.");
for (int i = 0; i < 5; i++) {
XxlJobLogger.log("beat at:" + i);
TimeUnit.SECONDS.sleep(2);
}
return SUCCESS;
}
}
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 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 org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* 跨平台Http任务
*
* @author xuxueli 2018-09-16 03:48:34
*/
@JobHandler(value="httpJobHandler")
@Component
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();
}
}
}
}
package com.xxl.job.executor.service.jobhandler;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.spacetech.gateway.BaseDataApiHub;
import com.spacetech.gateway.SqxyApiHub;
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="pushKcDateHandler")
@Component
public class PushKcDateHandler extends IJobHandler {
private static Long SQXY_CAMPUSID = 3418L;
private static Integer TYPE_TEACHER = 1;
private static Integer TYPE_STUDENT = 2;
@Autowired
private SqxyApiHub sqxyApiHub;
@Override
public ReturnT<String> execute(String param) throws Exception {
XxlJobLogger.log("XXL-JOB, Hello World.");
String api = "sqxy_kcsj_push";
XxlJobLogger.log(" start - 课程开课提醒接口调用启动;time:" + new Date());
for (Integer type = TYPE_TEACHER; type <= TYPE_STUDENT; type++) {
JSONObject params = new JSONObject();
params.put("campusid", SQXY_CAMPUSID);
sqxyApiHub.callApi(api.split("_")[1] + "_" + api.split("_")[2], false, params);
}
XxlJobLogger.log(" end - 课程开课提醒接口调用结束;time:" + new Date());
return SUCCESS;
}
}
package com.xxl.job.executor.service.jobhandler;
import java.util.Date;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
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.ConstantVar;
import com.xxl.job.core.util.HttpClientUtil;
@JobHandler(value = "SendReadAlarmJobHandler")
@Component
public class SendReadAlarmJobHandler extends IJobHandler {
@Override
public ReturnT<String> execute(String param) throws Exception {
XxlJobLogger.log(" start -推送阅读提醒消息接口调用启动;time:" + new Date());
String responseMessage = HttpClientUtil.postTaskJob(ConstantVar.DEVELOP_URL, "taskJob_sendReadAlarm");
XxlJobLogger.log(" responseMessage: " + responseMessage);
JSONObject retJson = JSONObject.parseObject(responseMessage).getJSONObject("ret");
if (retJson == null || retJson.getString("code") != "200") {
return FAIL;
}
XxlJobLogger.log(" end - 推送阅读提醒消息接口调用结束;time:" + new Date());
return SUCCESS;
}
}
\ No newline at end of file
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<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;
}
}
# web port
server.port=-1
# 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:8181/xxl-job-admin
### xxl-job executor address
xxl.job.executor.appname=com-spacetech-service-xxl-job-springboot-bak
xxl.job.executor.ip=
xxl.job.executor.port=8888
### 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
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1 seconds">
<contextName>logback</contextName>
<property name="log.path" value="/mnt/work/dubbo/logs/xxl-job-executor-sample-springboot-bak.log"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
\ No newline at end of file
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="spacetech-consumer-xxljob"
owner="programmer" organization="dubbox" />
<dubbo:registry address="zookeeper://10.10.10.190:2182"/>
<dubbo:consumer retries="0" check="false" timeout="60000" />
<dubbo:reference id="appStoreApiHub"
interface="com.spacetech.gateway.AppStoreApiHub" check="false" />
<dubbo:reference id="electiveApiHub"
interface="com.spacetech.gateway.ElectiveApiHub" check="false" />
<dubbo:reference id="portalApiHub"
interface="com.spacetech.gateway.PortalApiHub" check="false">
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="messageApiHub"
interface="com.spacetech.gateway.MessageApiHub" check="false" />
<dubbo:reference id="microlectureApiHub"
interface="com.spacetech.gateway.MicrolectureApiHub" check="false" />
<dubbo:reference id="homeworkApiHub"
interface="com.spacetech.gateway.HomeworkApiHub" check="false" />
<dubbo:reference id="examScoreApiHub"
interface="com.spacetech.gateway.ExamScoreApiHub" check="false" />
<dubbo:reference id="oaApiHub" interface="com.spacetech.gateway.OaApiHub"
check="false" />
<dubbo:reference id="baseDataApiHub"
interface="com.spacetech.gateway.BaseDataApiHub" check="false" >
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="userApiHub" interface="com.spacetech.gateway.UserApiHub"
check="false" />
<dubbo:reference id="wechatApiHub"
interface="com.spacetech.gateway.WechatApiHub" check="false" />
<dubbo:reference id="evaluationApiHub" interface="com.spacetech.gateway.EvaluationApiHub" check="false" >
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="parentlicenseApiHub"
interface="com.spacetech.gateway.ParentlicenseApiHub" check="false" >
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="logApiHub" interface="com.spacetech.gateway.LogApiHub"
check="false" >
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="tokenApiHub" interface="com.spacetech.gateway.TokenApiHub"
check="false" />
<dubbo:reference id="faceApiHub" interface="com.spacetech.gateway.FaceApiHub"
check="false" />
<dubbo:reference id="growthApiHub" interface="com.spacetech.gateway.GrowthApiHub"
check="false" timeout="90000" retries="2">
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="surveyApiHub" interface="com.spacetech.gateway.SurveyApiHub"
check="false" >
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="attendanceApiHub" interface="com.spacetech.gateway.AttendanceApiHub" check="false" >
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="registrationApiHub" interface="com.spacetech.gateway.RegistrationApiHub"
check="false" >
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="metaqApiHub" interface="com.spacetech.gateway.MetaqApiHub"
check="false" >
<dubbo:method name="callApiAsync" async="true" return="false"/>
</dubbo:reference>
<dubbo:reference id="payApiHub" interface="com.spacetech.gateway.PayApiHub"
check="false" />
<dubbo:reference id="sqxyApiHub" interface="com.spacetech.gateway.SqxyApiHub"
check="false" />
<dubbo:reference id="qualityApiHub" interface="com.spacetech.gateway.QualityApiHub"
check="false" />
</beans>
\ No newline at end of file
package com.xxl.job.executor.test;
import org.junit.Test;
/*@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration*/
/*@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class ,webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT)*/
public class XxlJobExecutorExampleBootApplicationTests {
@Test
public void test() {
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment