文章目录
- 1、添加依赖
- 2、application.yml 配置
- 3、 新建配置类
- 4、创建任务
- 5、将项目打包,在服务器上运行
- 6、编辑执行器机器地址
- 7、测试
1、添加依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
2、application.yml 配置
xxl:
job:
accessToken: default_token
admin:
addresses: http://{自己部署了调度中心的服务器ip}:{指定端口}/xxl-job-admin
executor:
appname: xxl-job-executor-sample
port: 9998
appname对应在调度中心创建的执行器名称:
3、 新建配置类
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.Configuration;
@Configuration
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.port}")
private int port;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setPort(port);
return xxlJobSpringExecutor;
}
}
}
4、创建任务
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
@Component
public class HelloJob {
@XxlJob("demoJobHandler")
public void helloJob(){
System.out.println("简单任务执行了。。。。");
}
}
@XxlJob注解里的内容为任务的处理程序JobHandler对应内容:(不要瞎写!!!)
5、将项目打包,在服务器上运行
6、编辑执行器机器地址
ip为网卡ip,通过ifconfig指令查看 eth0
端口为配置文件里设置的执行器端口
7、测试
设置调度周期为每秒执行一次:
成功: