xxl-job作为服务,之前使用都是每个服务配置信息,采用方法的形式,xxl-job作为服务的话,只需要进行配置
配置信息
java
## xxljob 配置
xxl:
job:
enabled: false
# 执行器通讯TOKEN [选填]:非空时启用
# accessToken:
# 单个地址或多个地址逗号隔开或 注册中心服务名
adminAddress: http://127.0.0.1:8898/xxl-job-admin
# 调度中心的 context-path:
# jobAdminContextPath: /job-admin
### 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
appname: ${spring.application.name}
### 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 "IP:PORT" 作为注册地址。
### 从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
# address:
# ip:
# port: 9999
# 执行器运行日志文件存储磁盘路径
# logpath: logs/xxl-job
# 执行器日志文件保存天数
logRetentionDays: 180
xxl-job创建任务
xxl-job相关输入
clientClass 实现类
clientMethod 调用的方法
加参数,比如用户为张三,年龄18
,"name":"张三","age":18
JobHandler:CommonXxlJobHandler
任务参数:{"clientClass":"com.sxcoal.station.common.client.admin.JournalismAdminClient","clientMethod":"getNewsHitsTopTenList","type":1}
JobHandler 如何通信的呢
CommonXxlJobHandler 作为一个微服务的项目,去做了xxl-job的整合,通过反射调用,核心代码
java
package com.sxcoal.station.job.executor.config;
import com.alibaba.fastjson.JSONObject;
import com.sxcoal.common.utils.SpringContextUtils;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Slf4j
@Component
@RequiredArgsConstructor
public class CommonXxlJob {
@XxlJob("CommonXxlJobHandler")
public void CommonXxlJobHandler() throws Exception {
XxlJobHelper.log("通用任务执行:{}", XxlJobHelper.getJobParam());
log.info("通用任务执行:{}", XxlJobHelper.getJobParam());
JSONObject jsonObject = JSONObject.parseObject(XxlJobHelper.getJobParam());
Object object = SpringContextUtils.getBean(jsonObject.getString("clientClass"));
Method method = object.getClass().getMethod(jsonObject.getString("clientMethod"), JSONObject.class);
log.info("通用任务执行 client 调用返回:{}", method.invoke(object, jsonObject));
}
}
针对处理的类(DailyPriceClient内容)
ServiceNameConstants.JOURNALISM_ADMIN 代表哪个服务
java
package com.sxcoal.station.common.client.admin;
import com.alibaba.fastjson.JSONObject;
import com.sxcoal.common.domain.RespResult;
import com.sxcoal.common.model.mq.CommonMessageDto;
import com.sxcoal.station.common.constant.ServiceNameConstants;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(
value = ServiceNameConstants.JOURNALISM_ADMIN
, contextId = "journalismAdmin"
, fallbackFactory = JournalismAdminClientFallbackFactory.class
)
public interface JournalismAdminClient {
//新闻热榜定时任务
@PostMapping(value = "/news/getNewsHitsTopTenList")
RespResult<?> getNewsHitsTopTenList(@RequestBody JSONObject json);
}
异常收集(方法和内容一样,做异常处理)
java
package com.sxcoal.coalresource.common.client.fallback.admin;
import com.sxcoal.coalresource.common.client.admin.CombinationPackageMiddleClient;
import com.sxcoal.common.domain.RespEnum;
import com.sxcoal.common.domain.RespResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.List;
@Slf4j
@Component
public class CombinationPackageMiddleFallbackFactory implements FallbackFactory<CombinationPackageMiddleClient> {
@Override
public CombinationPackageMiddleClient create(Throwable cause) {
log.error("{} Client 调用异常: {}", "combinationPackageMiddleService", cause.getMessage(), cause);
return new CombinationPackageMiddleClient() {
@Override
public RespResult<List<Long>> getCombinationIdListBySingleId(Long singleId) {
return RespResult.error(RespEnum.SERVICE_FALLBACK_ERROR.getMessage(), cause.getMessage());
}
};
}
}
服务内容(实现的功能)
java
@ApiOperation(value = "新闻浏览量前10列表")
@PostMapping(value = "/getNewsHitsTopTenList")
public RespResult<?> getNewsHitsTopTenList(@RequestBody JSONObject jsonObject) {
String flagStr = newsService.getNewsHitsTopTenList(jsonObject.getInteger("type"));
if (StringUtils.equals("true", flagStr)) {
return RespResult.ok(null, "成功");
} else if (StringUtils.equals("false", flagStr)) {
return RespResult.error();
} else if (StringUtils.equals("news null", flagStr)) {
return RespResult.error("查询新闻点击量前10的数据结果为空!");
}
return null;
}