xxl-job(2.4.1)使用spring-mvc替换netty的功能
1、xxl-job-core引入spring-mvc的依赖。
<!-- spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
2、com.xxl.job.core.controller包下添加XxlJobController类。
package com.xxl.job.core.controller;
import com.xxl.job.core.biz.impl.ExecutorBizImpl;
import com.xxl.job.core.biz.model.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
@author xxx
@date 2021/5/7
*/
@RestController
public class XxlJobController {
@PostMapping("/beat")
public ReturnT beat() {
return new ExecutorBizImpl().beat();
}
@PostMapping("/idleBeat")
public ReturnT idleBeat(@RequestBody IdleBeatParam param) {
return new ExecutorBizImpl().idleBeat(param);
}
@PostMapping("/run")
public ReturnT run(@RequestBody TriggerParam param) {
return new ExecutorBizImpl().run(param);
}
@PostMapping("/kill")
public ReturnT kill(@RequestBody KillParam param) {
return new ExecutorBizImpl().kill(param);
}
@PostMapping("/log")
public ReturnT log(@RequestBody LogParam param) {
return new ExecutorBizImpl().log(param);
}
}
3、修改com.xxl.job.core.server.EmbedServer的start方法,修改后为以下。
public void start(final String address, final int port, final String appname, final String accessToken) {
startRegistry(appname, address);
}
4、删除com.xxl.job.core.server.EmbedServer类的变量executorBiz和thread,以及删除xxl-job-core的pom文件的netty依赖,之后代码报错的部分也删除,最终EmbedServer类如下。
package com.xxl.job.core.server;
import com.xxl.job.core.thread.ExecutorRegistryThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Copy from : https://github.com/xuxueli/xxl-rpc
*
* @author xuxueli 2020-04-11 21:25
*/
public class EmbedServer {
private static final Logger logger = LoggerFactory.getLogger(EmbedServer.class);
public void start(final String address, final int port, final String appname, final String accessToken) {
startRegistry(appname, address);
}
public void stop() throws Exception {
// stop registry
stopRegistry();
logger.info(">>>>>>>>>>> xxl-job remoting server destroy success.");
}
// ---------------------- registry ----------------------
public void startRegistry(final String appname, final String address) {
// start registry
ExecutorRegistryThread.getInstance().start(appname, address);
}
public void stopRegistry() {
// stop registry
ExecutorRegistryThread.getInstance().toStop();
}
}
5、因为要复用spring-boot容器的端口号,所以这里注册的端口号要和它保持一致,修改com.xxl.job.core.executor.XxlJobExecutor的initEmbedServer方法,注释掉以下代码。
//port = port > 0 ? port : NetUtil.findAvailablePort(9999);
6、修改xxl.job.executor模块的的application.properties的配置文件xxl.job.executor.port=${server.port}。
7、在 com.xxl.job.core.config包下新建Config类。
package com.xxl.job.core.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
@author xxx
@date 2021/5/7
*/
@Configuration
@ComponentScan(basePackages = {"com.xxl.job.core.controller"})
public class Config {
}
8、
在.xxl.job.core的src/main/resources/META-INF文件夹下新建spring.factories文件,文件内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxl.job.core.config.Config
9、完成。