From a83c21a43aa317cc02d3537661406abc1c46d5c5 Mon Sep 17 00:00:00 2001 From: "xueli.xue" Date: Fri, 28 Apr 2017 15:12:20 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=96=B0=E5=A2=9Espring-boot?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=89=A7=E8=A1=8C=E5=99=A8example=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=EF=BC=9B=202=E3=80=81=E5=8D=87=E7=BA=A7jetty=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=87=B39.2=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- pom.xml | 3 +- xxl-job-core/pom.xml | 2 +- .../rpc/netcom/jetty/server/JettyServer.java | 23 ++-- xxl-job-executor-springboot-example/pom.xml | 101 ++++++++++++++++++ .../com/xxl/job/executor/Application.java | 13 +++ .../executor/core/config/XxlJobConfig.java | 11 ++ .../executor/mvc/handler/IndexController.java | 18 ++++ .../service/jobhandler/DemoJobHandler.java | 39 +++++++ .../src/main/resources/application.properties | 1 + .../resources/applicationcontext-xxl-job.xml | 59 ++++++++++ .../src/main/resources/log4j.xml | 50 +++++++++ .../resources/xxl-job-executor.properties | 10 ++ ...obExecutorExampleBootApplicationTests.java | 19 ++++ 14 files changed, 340 insertions(+), 15 deletions(-) create mode 100644 xxl-job-executor-springboot-example/pom.xml create mode 100644 xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/Application.java create mode 100644 xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java create mode 100644 xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/mvc/handler/IndexController.java create mode 100644 xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java create mode 100644 xxl-job-executor-springboot-example/src/main/resources/application.properties create mode 100644 xxl-job-executor-springboot-example/src/main/resources/applicationcontext-xxl-job.xml create mode 100644 xxl-job-executor-springboot-example/src/main/resources/log4j.xml create mode 100644 xxl-job-executor-springboot-example/src/main/resources/xxl-job-executor.properties create mode 100644 xxl-job-executor-springboot-example/src/test/java/com/xxl/job/executor/test/XxlJobExecutorExampleBootApplicationTests.java diff --git a/README.md b/README.md index c799d2c7..fb9bde66 100644 --- a/README.md +++ b/README.md @@ -99,9 +99,9 @@ XXL-JOB是一个轻量级分布式任务调度框架,其核心设计目标是 ![输入图片说明](https://static.oschina.net/uploads/img/201704/25151032_nrJN.png "在这里输入图片标题") ### 1.5 环境 -- Servlet/JSP Spec:3.0/2.2 +- Servlet/JSP Spec:3.1/2.3 - JDK:1.7+ -- Tomcat:7+/Jetty8+ +- Tomcat:8.5.x+/Jetty9.2+ - Mysql:5.6+ - Maven:3+ @@ -813,6 +813,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段 #### 6.14 版本 V1.7.0 特性 (Coding) - 1、脚本任务:支持以GLUE模式开发和运行脚本任务,包括Shell、Python等类型脚本; +- 2、新增spring-boot类型执行器example项目; +- 3、升级jetty版本至9.2; - 2、执行器移除GlueLoader依赖改为推送方式,GLUE源码加载不再依赖JDBC; - 3、登陆拦截Redirect时获取项目名,解决非根据目录发布时跳转404问题; diff --git a/pom.xml b/pom.xml index 31a430a1..d191b8b9 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,8 @@ xxl-job-core xxl-job-admin xxl-job-executor-example - + xxl-job-executor-springboot-example + diff --git a/xxl-job-core/pom.xml b/xxl-job-core/pom.xml index d3788249..3faa4c36 100644 --- a/xxl-job-core/pom.xml +++ b/xxl-job-core/pom.xml @@ -34,7 +34,7 @@ org.eclipse.jetty jetty-server - 8.2.0.v20160908 + 9.2.21.v20170120 diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/rpc/netcom/jetty/server/JettyServer.java b/xxl-job-core/src/main/java/com/xxl/job/core/rpc/netcom/jetty/server/JettyServer.java index 09a82336..d866fd68 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/rpc/netcom/jetty/server/JettyServer.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/rpc/netcom/jetty/server/JettyServer.java @@ -5,8 +5,8 @@ import com.xxl.job.core.thread.ExecutorRegistryThread; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.HandlerCollection; -import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.util.thread.ExecutorThreadPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,21 +24,22 @@ public class JettyServer { thread = new Thread(new Runnable() { @Override public void run() { - server = new Server(); - server.setThreadPool(new ExecutorThreadPool(200, 200, 30000)); // 非阻塞 - - // connector - SelectChannelConnector connector = new SelectChannelConnector(); + + // The Server + server = new Server(new ExecutorThreadPool()); // 非阻塞 + + // HTTP connector + ServerConnector connector = new ServerConnector(server); connector.setPort(port); - connector.setMaxIdleTime(30000); - server.setConnectors(new Connector[] { connector }); - - // handler - HandlerCollection handlerc =new HandlerCollection(); + server.setConnectors(new Connector[]{connector}); + + // Set a handler + HandlerCollection handlerc =new HandlerCollection(); handlerc.setHandlers(new Handler[]{new JettyServerHandler()}); server.setHandler(handlerc); try { + // Start the server server.start(); logger.info(">>>>>>>>>>>> xxl-job jetty server start success at port:{}.", port); ExecutorRegistryThread.getInstance().start(port, ip, appName, registHelper); diff --git a/xxl-job-executor-springboot-example/pom.xml b/xxl-job-executor-springboot-example/pom.xml new file mode 100644 index 00000000..4ee92870 --- /dev/null +++ b/xxl-job-executor-springboot-example/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + + com.xuxueli + xxl-job + 1.7.0-SNAPSHOT + + xxl-job-executor-springboot-example + jar + xxl-job-executor-springboot-example + Example executor project for spring boot + + + 1.7.0-SNAPSHOT + + 1.3.8.RELEASE + + UTF-8 + UTF-8 + 1.7 + + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring-boot.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-logging + + + + + + org.springframework.boot + spring-boot-starter-log4j + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + com.mchange + c3p0 + 0.9.5.2 + + + + mysql + mysql-connector-java + 5.1.29 + + + + + com.xuxueli + xxl-job-core + ${xxl-job.version} + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/Application.java b/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/Application.java new file mode 100644 index 00000000..51a0e457 --- /dev/null +++ b/xxl-job-executor-springboot-example/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-springboot-example/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java b/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java new file mode 100644 index 00000000..4a8c1dae --- /dev/null +++ b/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java @@ -0,0 +1,11 @@ +package com.xxl.job.executor.core.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + + +@Configuration +@ImportResource("classpath:applicationcontext-xxl-job.xml") +public class XxlJobConfig { + +} \ No newline at end of file diff --git a/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/mvc/handler/IndexController.java b/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/mvc/handler/IndexController.java new file mode 100644 index 00000000..89c7fef4 --- /dev/null +++ b/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/mvc/handler/IndexController.java @@ -0,0 +1,18 @@ +package com.xxl.job.executor.mvc.handler; + +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 running."; + } + +} \ No newline at end of file diff --git a/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java b/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java new file mode 100644 index 00000000..8773fc78 --- /dev/null +++ b/xxl-job-executor-springboot-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.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.JobHander; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.util.concurrent.TimeUnit; + + +/** + * 任务Handler的一个Demo(Bean模式) + * + * 开发步骤: + * 1、继承 “IJobHandler” ; + * 2、装配到Spring,例如加 “@Service” 注解; + * 3、加 “@JobHander” 注解,注解value值为新增任务生成的JobKey的值;多个JobKey用逗号分割; + * + * @author xuxueli 2015-12-19 19:43:36 + */ +@JobHander(value="demoJobHandler") +@Service +public class DemoJobHandler extends IJobHandler { + private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class); + + @Override + public ReturnT execute(String... params) throws Exception { + logger.info("XXL-JOB, Hello World."); + + for (int i = 0; i < 5; i++) { + logger.info("beat at:{}", i); + TimeUnit.SECONDS.sleep(2); + } + return ReturnT.SUCCESS; + } + +} diff --git a/xxl-job-executor-springboot-example/src/main/resources/application.properties b/xxl-job-executor-springboot-example/src/main/resources/application.properties new file mode 100644 index 00000000..bafddced --- /dev/null +++ b/xxl-job-executor-springboot-example/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=8081 \ No newline at end of file diff --git a/xxl-job-executor-springboot-example/src/main/resources/applicationcontext-xxl-job.xml b/xxl-job-executor-springboot-example/src/main/resources/applicationcontext-xxl-job.xml new file mode 100644 index 00000000..d4dd0992 --- /dev/null +++ b/xxl-job-executor-springboot-example/src/main/resources/applicationcontext-xxl-job.xml @@ -0,0 +1,59 @@ + + + + + + + + classpath*:xxl-job-executor.properties + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-springboot-example/src/main/resources/log4j.xml b/xxl-job-executor-springboot-example/src/main/resources/log4j.xml new file mode 100644 index 00000000..b0d1cd87 --- /dev/null +++ b/xxl-job-executor-springboot-example/src/main/resources/log4j.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-springboot-example/src/main/resources/xxl-job-executor.properties b/xxl-job-executor-springboot-example/src/main/resources/xxl-job-executor.properties new file mode 100644 index 00000000..769c72e0 --- /dev/null +++ b/xxl-job-executor-springboot-example/src/main/resources/xxl-job-executor.properties @@ -0,0 +1,10 @@ +### xxl-job db +xxl.job.db.driverClass=com.mysql.jdbc.Driver +xxl.job.db.url=jdbc:mysql://localhost:3306/xxl-job?useUnicode=true&characterEncoding=UTF-8 +xxl.job.db.user=root +xxl.job.db.password=root_pwd + +### xxl-job executor address +xxl.job.executor.appname=xxl-job-executor-example +xxl.job.executor.ip= +xxl.job.executor.port=9999 \ No newline at end of file diff --git a/xxl-job-executor-springboot-example/src/test/java/com/xxl/job/executor/test/XxlJobExecutorExampleBootApplicationTests.java b/xxl-job-executor-springboot-example/src/test/java/com/xxl/job/executor/test/XxlJobExecutorExampleBootApplicationTests.java new file mode 100644 index 00000000..831ce52a --- /dev/null +++ b/xxl-job-executor-springboot-example/src/test/java/com/xxl/job/executor/test/XxlJobExecutorExampleBootApplicationTests.java @@ -0,0 +1,19 @@ +package com.xxl.job.executor.test; + +import com.xxl.job.executor.Application; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebAppConfiguration +public class XxlJobExecutorExampleBootApplicationTests { + + @Test + public void contextLoads() { + } + +} \ No newline at end of file