import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface XxlJobRegister {
}
XxlJobRegister用来注册
XxlJobConfig 用来配置xxl-job参数
import io.jboot.Jboot;
import io.jboot.app.config.annotation.ConfigModel;
@ConfigModel(prefix = "xxl.job")
public class XxlJobConfig {
public static final XxlJobConfig me = Jboot.config(XxlJobConfig.class);
private String adminAddresses;
private String accessToken;
private String appname;
private String address;
private String ip;
private int port;
private String logPath;
private int logRetentionDays;
public String getAdminAddresses() {
return adminAddresses;
}
public void setAdminAddresses(String adminAddresses) {
this.adminAddresses = adminAddresses;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getAppname() {
return appname;
}
public void setAppname(String appname) {
this.appname = appname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getLogPath() {
return logPath;
}
public void setLogPath(String logPath) {
this.logPath = logPath;
}
public int getLogRetentionDays() {
return logRetentionDays;
}
public void setLogRetentionDays(int logRetentionDays) {
this.logRetentionDays = logRetentionDays;
}
}XxlJobPlugin 实现iplugin 插件
import com.itxfw.core.plugin.xxljob.annotation.XxlJobRegister;
import com.itxfw.core.plugin.xxljob.config.XxlJobConfig;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.IPlugin;
import com.xxl.job.core.executor.impl.XxlJobSimpleExecutor;
import io.jboot.utils.ClassScanner;
import io.jboot.utils.ClassUtil;
import java.util.ArrayList;
import java.util.List;
public class XxlJobPlugin implements IPlugin {
private XxlJobSimpleExecutor xxlJobExecutor;
private List<Object> xxlJobBeanList = new ArrayList<>();
@Override
public boolean start() {
initXxlJobExecutor();
return true;
}
@Override
public boolean stop() {
if (xxlJobExecutor != null) {
xxlJobExecutor.destroy();
}
return true;
}
private void initXxlJobExecutor() {
// init executor
xxlJobExecutor = new XxlJobSimpleExecutor();
xxlJobExecutor.setIp( XxlJobConfig.me.getIp());
xxlJobExecutor.setPort( XxlJobConfig.me.getPort());
xxlJobExecutor.setAppname( XxlJobConfig.me.getAppname());
xxlJobExecutor.setAdminAddresses( XxlJobConfig.me.getAdminAddresses());
xxlJobExecutor.setLogPath( XxlJobConfig.me.getLogPath());
xxlJobExecutor.setAccessToken( XxlJobConfig.me.getAccessToken());
xxlJobExecutor.setLogRetentionDays(XxlJobConfig.me.getLogRetentionDays());
// start executor
try {
List<Class> xxlJobRegisterClasses = ClassScanner.scanClassByAnnotation(XxlJobRegister.class, true);
if(xxlJobRegisterClasses!=null && xxlJobRegisterClasses.size()>0){
for (Class<?> xxlJobRegisterClass : xxlJobRegisterClasses) {
Object xxlJobRegisterObj = ClassUtil.newInstance(xxlJobRegisterClass, false);
if (xxlJobRegisterObj == null) {
throw new NullPointerException("can not newInstance for class : " + xxlJobRegisterClass);
}
xxlJobBeanList.add(xxlJobRegisterObj);
}
}
xxlJobExecutor.setXxlJobBeanList(xxlJobBeanList);
xxlJobExecutor.start();
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
}
}
}XxlJobInitializer 初始化
import io.jboot.aop.jfinal.JfinalPlugins;
import io.jboot.core.listener.JbootAppListenerBase;
/**
* 初始化插件,maven依赖,自动配置插件
*/
public class XxlJobInitializer extends JbootAppListenerBase {
@Override
public void onPluginConfig(JfinalPlugins plugins) {
plugins.add(new XxlJobPlugin());
}
}4个类就完成了集成,导入这个依赖,则自动加载了这个插件
开始使用:
com.itxfwcore-plugin-xxljob0.0.1-SNAPSHOT
import com.itxfw.core.plugin.xxljob.annotation.XxlJobRegister;
import com.itxfw.module.yundong.service.activity.ActivityUserDetailsService;
import com.itxfw.module.yundong.service.settings.ActiveSettingsService;
import com.itxfw.module.yundong.service.user.UserService;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.jboot.Jboot;
@XxlJobRegister
public class YundongXxlJob {
/**
* 1、简单任务示例(Bean模式)
*/
@XxlJob("demoJobHandler")
public void demoJobHandler() throws Exception {
UserService userService = Jboot.getBean(UserService.class);
System.out.println("==========测试任务==========");
}
}