一、代码
1 、引入依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
2、书写对应的执行任务,这里书写了三个任务
demoyuanyuanJobHandler,call-rabbitmq-yuan,call-rabbitmq-yuan-jiaohuanji
package com.yy.xxljob;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.Method;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Slf4j
public class OrderXxlJob {
/**
* 1、简单任务示例(Bean模式)
*/
@XxlJob("demoyuanyuanJobHandler")//括号中填写任务名
public void demoJobHandler() throws Exception {
XxlJobHelper.log("demoJobHandler 执行了");
log.info("demoJobHandler 执行了");
}
/**
* 2、检测队列中消息是否超过5任务
*/
@XxlJob("call-rabbitmq-yuan")//括号中填写任务名
public void demoJobHandler2() throws Exception {
String param = XxlJobHelper.getJobParam();
HashMap paramObj = JSONUtil.toBean(param, HashMap.class);
Object host = paramObj.get("host");
Object port = paramObj.get("port");
String url = StrUtil.format("http://{}:{}/api/queues/%2F/范鹏磊.%23",host,port);
String body = HttpRequest.of(url).method(Method.GET).header("Authorization", "Basic Z3Vlc3Q6Z3Vlc3Q=").execute().body();
JSONObject jsonObject = JSONUtil.parseObj(body);
Object messages = jsonObject.get("messages");
Object queueName = jsonObject.get("name");
int messageCount = Integer.parseInt(messages.toString());
if(messageCount >= 5){
log.debug("报警-> 队列{},消息数量:{}",queueName,messageCount);
} else {
log.debug("正常");
}
log.info("参数:{}",param);
}
//检查交换机的所有队列中消息是否超过5发送钉钉提醒任务
@XxlJob("call-rabbitmq-yuan-jiaohuanji")//括号中填写任务名
public void demoJobHandler3() throws Exception {
String param = XxlJobHelper.getJobParam();
HashMap paramObj = JSONUtil.toBean(param, HashMap.class);
Object host = paramObj.get("host"); //http://192.168.11.99:15672/#/exchanges/%2F/ex.game
Object port = paramObj.get("port");
String url = StrUtil.format("http://{}:{}/api/vhosts",host,port);
String body = HttpRequest.of(url).method(Method.GET).header("Authorization", "Basic Z3Vlc3Q6Z3Vlc3Q=").execute().body();
JSONArray queues = JSONUtil.parseArray(body);
for (Object queue : queues) {
JSONObject jsonObject = JSONUtil.parseObj(queue);
Object name = jsonObject.get("name");
Object messages = jsonObject.get("messages");
Object queueName = jsonObject.get("name");
int messageCount = Integer.parseInt(messages.toString());
if(messageCount >= 5){
sendDingDing(queueName.toString(),messages.toString());
log.debug("钉钉报警-> 队列{},消息数量:{}",queueName,messageCount);
} else {
log.debug("正常");
}
log.info("参数:{}",param);
}
// Object messages = jsonObject.get("messages");
// Object queueName = jsonObject.get("name");
// int messageCount = Integer.parseInt(messages.toString());
// if(messageCount >= 5){
// log.debug("报警-> 队列{},消息数量:{}",queueName,messageCount);
// } else {
// log.debug("正常");
// }
log.info("参数:{}",param);
}
//钉钉发送消息的方法
private void sendDingDing(String name,String message){
String url = "https://oapi.dingtalk.com/robot/send?access_token=afa3850182a65faee2ee6913746793f65fb1f858bb7c921aaf5c7c49a9b33d9e";
JSONObject msg = new JSONObject();
String json= "{\n" +
" \"msgtype\": \"text\",\n" +
" \"text\": {\n" +
" \"content\": \"注意!!!!!队列"+name+"有"+message+"条消息,请及时审核处理1111\"\n" +
" }\n" +
"}";
String result = HttpRequest.post(url)
.body(json)
.execute().body();
log.debug("警告发送成功,队列{}的消息{}条,超过了限制",name,message);
}
}
在application.properties文件中配置参数
spring.application.name=yuan-yuan
#日志配置
logging.level.root=error
logging.level.com.yy = debug
#
## Spring Task 调度线程池大小,默认为 1,建议根据任务量进行调整。
## 如果不开启异步,可以理解为工厂经理们亲自处理任务
#spring.task.scheduling.thread-name-prefix = jingli-
#spring.task.scheduling.pool.size = 1
##当开启异步后,@EnableAsync 经理就不再干活了,而是工人干,经理只负责调度,管理
#
## 配置工人,调度线程名称前缀
#spring.task.execution.thread-name-prefix = worker-
## 任务执行线程池配置,核心线程池大小,默认为 8,长工2个
#spring.task.execution.pool.core-size= 2
## 最大线程池大小,可以根据实际情况调整,临时工+长工最多为6个
#spring.task.execution.pool.max-size= 6
## 线程空闲时间(超时时间),超过这个时间没有任务则关闭线程,单位秒
#spring.task.execution.pool.keep-alive= 60s
## 队列容量,用于存放等待执行的任务,如果不指定,int.max
#spring.task.execution.pool.queue-capacity= 2
## 拒绝策略,当线程池和队列都满时如何处理新提交的任务,可选值有 AbortPolicy, CallerRunsPolicy 等
#spring.task.execution.pool.rejection-policy= CallerRunsPolicy
#定时任务
spring.task1.period=5000
#xxl-job配置
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://192.168.11.99:8080/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=default_token
### xxl-job executor appname
xxl.job.executor.appname=yuanyuan
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
执行器配置,xxlJobConfig
package com.yy.xxljob;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.Method;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Slf4j
public class OrderXxlJob {
/**
* 1、简单任务示例(Bean模式)
*/
@XxlJob("demoyuanyuanJobHandler")//括号中填写任务名
public void demoJobHandler() throws Exception {
XxlJobHelper.log("demoJobHandler 执行了");
log.info("demoJobHandler 执行了");
}
/**
* 2、检测队列中消息是否超过5任务
*/
@XxlJob("call-rabbitmq-yuan")//括号中填写任务名
public void demoJobHandler2() throws Exception {
String param = XxlJobHelper.getJobParam();
HashMap paramObj = JSONUtil.toBean(param, HashMap.class);
Object host = paramObj.get("host");
Object port = paramObj.get("port");
String url = StrUtil.format("http://{}:{}/api/queues/%2F/队列名",host,port);
String body = HttpRequest.of(url).method(Method.GET).header("Authorization", "Basic Z3Vlc3Q6Z3Vlc3Q=").execute().body();
JSONObject jsonObject = JSONUtil.parseObj(body);
Object messages = jsonObject.get("messages");
Object queueName = jsonObject.get("name");
int messageCount = Integer.parseInt(messages.toString());
if(messageCount >= 5){
log.debug("报警-> 队列{},消息数量:{}",queueName,messageCount);
} else {
log.debug("正常");
}
log.info("参数:{}",param);
}
//检查交换机的所有队列中消息是否超过5发送钉钉提醒任务
@XxlJob("call-rabbitmq-yuan-jiaohuanji")//括号中填写任务名
public void demoJobHandler3() throws Exception {
String param = XxlJobHelper.getJobParam();
HashMap paramObj = JSONUtil.toBean(param, HashMap.class);
Object host = paramObj.get("host"); //http://192.168.11.99:15672/#/exchanges/%2F/ex.game
Object port = paramObj.get("port");
String url = StrUtil.format("http://{}:{}/api/queues",host,port);
String body = HttpRequest.of(url).method(Method.GET).header("Authorization", "Basic Z3Vlc3Q6Z3Vlc3Q=").execute().body();
JSONArray queues = JSONUtil.parseArray(body);
for (Object queue : queues) {
JSONObject jsonObject = JSONUtil.parseObj(queue);
Object name = jsonObject.get("name");
Object messages = jsonObject.get("messages");
Object queueName = jsonObject.get("name");
int messageCount = Integer.parseInt(messages.toString());
if(messageCount >= 5){
sendDingDing(queueName.toString(),messages.toString());
log.debug("钉钉报警-> 队列{},消息数量:{}",queueName,messageCount);
} else {
log.debug("正常");
}
log.info("参数:{}",param);
}
// Object messages = jsonObject.get("messages");
// Object queueName = jsonObject.get("name");
// int messageCount = Integer.parseInt(messages.toString());
// if(messageCount >= 5){
// log.debug("报警-> 队列{},消息数量:{}",queueName,messageCount);
// } else {
// log.debug("正常");
// }
log.info("参数:{}",param);
}
//钉钉发送消息的方法
private void sendDingDing(String name,String message){
String url = "https://oapi.dingtalk.com/robot/send?access_token=afa3850182a65faee2ee6913746793f65fb1f858bb7c921aaf5c7c49a9b33d9e";
JSONObject msg = new JSONObject();
String json= "{\n" +
" \"msgtype\": \"text\",\n" +
" \"text\": {\n" +
" \"content\": \"注意!!!!!队列"+name+"有"+message+"条消息,请及时审核处理1111\"\n" +
" }\n" +
"}";
String result = HttpRequest.post(url)
.body(json)
.execute().body();
log.debug("警告发送成功,队列{}的消息{}条,超过了限制",name,message);
}
}
二、开启任务
1、点击添加执行器,AppName要应项目中配置的名称
2、新建任务
监控队列时填写任务参数和任务名
查看执行日志