Commit 13831a3a authored by yanzg's avatar yanzg

将源码打包进jar包

parent 277352c0
package com.yanzuoguang.redis; package com.yanzuoguang.redis;
import com.yanzuoguang.redis.vo.PlanLevelType;
/** /**
* 用于确定任务所属的级别 * 用于确定任务所属的级别
* *
* @author 颜佐光 * @author 颜佐光
*/ */
public interface PlanGlobal { public interface PlanLevel {
/** /**
* 是否全局运行 * 获取任务程序级别
* 1. 返回true表示全局运行,将采用plan_plan队列来执行,不实现本接口时默认为false
* 2. 返回false表示程序运行
* *
* @return 是否全局运行 * @return 应用程序级别
*/ */
boolean isGlobal(); PlanLevelType getLevel();
} }
package com.yanzuoguang.redis;
/**
* 获取应用程序级别
*
* @author 颜佐光
*/
public interface PlanLevelNamespace extends PlanLevel {
/**
* 获取任务级别命名空间
*
* @return 任务级别关键字
*/
String getLevelNamespace();
}
package com.yanzuoguang.redis;
/**
* 获取应用程序级别
*
* @author 颜佐光
*/
public interface PlanLevelNamespaceDefault extends PlanLevelNamespace {
}
package com.yanzuoguang.redis.def;
import com.yanzuoguang.redis.PlanGlobal;
/**
* 默认非全局任务
*
* @author 颜佐光
*/
public class PlanGlobalDefault implements PlanGlobal {
@Override
public boolean isGlobal() {
return false;
}
}
package com.yanzuoguang.redis.def;
import com.yanzuoguang.redis.PlanLevel;
import com.yanzuoguang.redis.vo.PlanLevelType;
/**
* 默认Application应用程序级别
*
* @author 颜佐光
*/
public class PlanLevelDefault implements PlanLevel {
@Override
public PlanLevelType getLevel() {
return PlanLevelType.Application;
}
}
package com.yanzuoguang.redis.level;
import com.yanzuoguang.redis.PlanLevelNamespaceDefault;
import com.yanzuoguang.redis.vo.PlanLevelType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 应用程序级别
*
* @author 颜佐光
*/
@Component
public class PlanLevelNamespaceApplication implements PlanLevelNamespaceDefault {
@Value("${spring.application.name:}")
private String applicationName;
@Override
public String getLevelNamespace() {
return applicationName;
}
@Override
public PlanLevelType getLevel() {
return PlanLevelType.Application;
}
}
package com.yanzuoguang.redis.level;
import com.yanzuoguang.redis.PlanLevelNamespaceDefault;
import com.yanzuoguang.redis.vo.PlanLevelType;
import com.yanzuoguang.util.contants.SystemContants;
import org.springframework.stereotype.Component;
/**
* 全局级别
*
* @author 颜佐光
*/
@Component
public class PlanLevelNamespaceGlobal implements PlanLevelNamespaceDefault {
@Override
public String getLevelNamespace() {
return SystemContants.SYSTEM;
}
@Override
public PlanLevelType getLevel() {
return PlanLevelType.Global;
}
}
package com.yanzuoguang.redis.level;
import com.yanzuoguang.redis.PlanLevelNamespaceDefault;
import com.yanzuoguang.redis.vo.PlanLevelType;
import org.springframework.stereotype.Component;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
/**
* 程序级别
*
* @author 颜佐光
*/
@Component
public class PlanLevelNamespaceProgram implements PlanLevelNamespaceDefault {
@Override
public String getLevelNamespace() {
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
return runtimeMxBean.getName();
}
@Override
public PlanLevelType getLevel() {
return PlanLevelType.Program;
}
}
package com.yanzuoguang.redis.level;
import com.yanzuoguang.redis.PlanLevelNamespaceDefault;
import com.yanzuoguang.redis.vo.PlanLevelType;
import com.yanzuoguang.util.helper.UrlHelper;
import org.springframework.stereotype.Component;
/**
* 服务器Ip级别
*
* @author 颜佐光
*/
@Component
public class PlanLevelNamespaceServerIp implements PlanLevelNamespaceDefault {
@Override
public String getLevelNamespace() {
return UrlHelper.getIp();
}
@Override
public PlanLevelType getLevel() {
return PlanLevelType.ServerIp;
}
}
...@@ -33,9 +33,8 @@ public class PlanConsumer implements InitializingBean { ...@@ -33,9 +33,8 @@ public class PlanConsumer implements InitializingBean {
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
planProcedure.initQueue();
// 应用程序级的任务交给队列处理 // 应用程序级的任务交给队列处理
mqService.setQueueConsumer(planProcedure.getApplicationPlanQueueName(), (message, channel) -> planProcedure.initQueue((message, channel) ->
PlanConsumer.this.plan(new String(message.getBody(), StandardCharsets.UTF_8), message, channel) PlanConsumer.this.plan(new String(message.getBody(), StandardCharsets.UTF_8), message, channel)
); );
} }
......
...@@ -5,12 +5,15 @@ import com.yanzuoguang.mq.vo.MessageVo; ...@@ -5,12 +5,15 @@ import com.yanzuoguang.mq.vo.MessageVo;
import com.yanzuoguang.mq.vo.QueueVo; import com.yanzuoguang.mq.vo.QueueVo;
import com.yanzuoguang.redis.PlanContains; import com.yanzuoguang.redis.PlanContains;
import com.yanzuoguang.redis.PlanInfo; import com.yanzuoguang.redis.PlanInfo;
import com.yanzuoguang.redis.service.PlanRegister; import com.yanzuoguang.redis.PlanLevelNamespace;
import com.yanzuoguang.redis.vo.PlanConfigVo; import com.yanzuoguang.redis.vo.PlanConfigVo;
import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
/** /**
* 订单消息队列 * 订单消息队列
* *
...@@ -20,47 +23,54 @@ import org.springframework.stereotype.Component; ...@@ -20,47 +23,54 @@ import org.springframework.stereotype.Component;
public class PlanProcedure { public class PlanProcedure {
private final MqService mqService; private final MqService mqService;
private final PlanRegister planRegister; private final List<PlanLevelNamespace> planLevelNamespaceList;
public PlanProcedure(MqService mqService, PlanRegister planRegister) { public PlanProcedure(MqService mqService, List<PlanLevelNamespace> planLevelNamespaceList) {
this.mqService = mqService; this.mqService = mqService;
this.planRegister = planRegister; this.planLevelNamespaceList = planLevelNamespaceList;
} }
public void initQueue() { public void initQueue(ChannelAwareMessageListener messageListener) {
// 库存任务 // 库存任务
mqService.createQueue(new QueueVo(PlanContains.YZG_PLAN_PLAN)); planLevelNamespaceList.forEach(k -> {
mqService.createQueue(new QueueVo(this.getApplicationPlanQueueName())); String queueName = getQueueName(k);
// 创建队列,注册回调
mqService.createQueue(new QueueVo(queueName));
// 应用程序级的任务交给队列处理
mqService.setQueueConsumer(queueName, messageListener);
});
} }
/** /**
* 应用程序级队列名称 * 执行库存任务
* *
* @return 应用程序级别队列名称 * @param req 待执行的任务
*/ */
public String getApplicationPlanQueueName() { public void plan(PlanConfigVo config, PlanInfo<?> req) {
return StringHelper.getId(PlanContains.YZG_PLAN_PLAN, planRegister.getApplicationName()); plan(config, JsonHelper.serialize(req));
} }
/** /**
* 执行库存任务 * 执行库存任务
* *
* @param req 待执行的任务 * @param json 执行的任务
*/ */
public void plan(PlanConfigVo planConfigVo, PlanInfo<?> req) { private void plan(PlanConfigVo config, String json) {
plan(planConfigVo, JsonHelper.serialize(req)); String queueName = getQueueName(config.getPlanLevelNamespace());
mqService.message(new MessageVo(queueName, json, 0));
} }
/** /**
* 执行库存任务 * 获取队列名称
* *
* @param json 执行的任务 * @param planLevelNamespace 命名空间
* @return 队列名称
*/ */
private void plan(PlanConfigVo planConfigVo, String json) { private String getQueueName(PlanLevelNamespace planLevelNamespace) {
if (planConfigVo.getPlanGlobal().isGlobal()) { return StringHelper.getId(
mqService.message(new MessageVo(PlanContains.YZG_PLAN_PLAN, json, 0)); PlanContains.YZG_PLAN_PLAN,
} else { planLevelNamespace.getLevel().getName(),
mqService.message(new MessageVo(this.getApplicationPlanQueueName(), json, 0)); planLevelNamespace.getLevelNamespace()
} );
} }
} }
package com.yanzuoguang.redis.service; package com.yanzuoguang.redis.service;
import com.yanzuoguang.redis.Plan; import com.yanzuoguang.redis.*;
import com.yanzuoguang.redis.PlanGlobal; import com.yanzuoguang.redis.def.PlanLevelDefault;
import com.yanzuoguang.redis.PlanName;
import com.yanzuoguang.redis.PlanStart;
import com.yanzuoguang.redis.def.PlanGlobalDefault;
import com.yanzuoguang.redis.def.PlanNameDefault; import com.yanzuoguang.redis.def.PlanNameDefault;
import com.yanzuoguang.redis.def.PlanStartDefault; import com.yanzuoguang.redis.def.PlanStartDefault;
import com.yanzuoguang.redis.vo.PlanConfigVo; import com.yanzuoguang.redis.vo.PlanConfigVo;
import org.springframework.beans.factory.annotation.Value; import com.yanzuoguang.redis.vo.PlanLevelType;
import com.yanzuoguang.util.log.Log;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.LinkedHashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/** /**
* 任务工具类 * 任务工具类
...@@ -31,13 +26,12 @@ public class PlanRegister { ...@@ -31,13 +26,12 @@ public class PlanRegister {
* 任务默认值 * 任务默认值
*/ */
private final PlanStartDefault planStartDefault = new PlanStartDefault(); private final PlanStartDefault planStartDefault = new PlanStartDefault();
private final PlanGlobalDefault planGlobalDefault = new PlanGlobalDefault(); private final PlanLevelDefault planLevelDefault = new PlanLevelDefault();
private final Map<PlanLevelType, PlanLevelNamespaceDefault> mapLevelTypeNamespaceDefault = new HashMap<>();
@Value("${spring.application.name:}") public PlanRegister(Optional<List<Plan>> plans, List<PlanLevelNamespaceDefault> planLevelNamespaceDefaults) {
private String applicationName;
public PlanRegister(Optional<List<Plan>> plans) {
plans.ifPresent(planList -> planList.forEach(this::register)); plans.ifPresent(planList -> planList.forEach(this::register));
planLevelNamespaceDefaults.forEach(k -> mapLevelTypeNamespaceDefault.put(k.getLevel(), k));
} }
/** /**
...@@ -49,15 +43,6 @@ public class PlanRegister { ...@@ -49,15 +43,6 @@ public class PlanRegister {
return mapPlan; return mapPlan;
} }
/**
* 应用程序名称
*
* @return 应用程序名称
*/
public String getApplicationName() {
return applicationName;
}
/** /**
* 获取执行任务 * 获取执行任务
* *
...@@ -77,28 +62,83 @@ public class PlanRegister { ...@@ -77,28 +62,83 @@ public class PlanRegister {
if (plan == null) { if (plan == null) {
return; return;
} }
PlanLevel planLevel = getPlanLevel(plan);
PlanLevelNamespace planLevelNamespace = getPlanLevelNamespace(plan, planLevel);
if (planLevelNamespace == null) {
return;
}
PlanStart planStart = getPlanStart(plan);
PlanName planName = getPlanName(plan);
// 任务配置信息
PlanConfigVo planConfigVo = new PlanConfigVo(plan, planName, planStart, planLevel, planLevelNamespace);
// 任务配置
mapPlan.put(planName.getPlanKey(), planConfigVo);
}
/**
* 任务在应用程序打开时是否运行
*
* @param plan 任务
* @return 任务在应用程序打开时是否运行的配置
*/
private PlanStart getPlanStart(Plan plan) {
PlanStart planStart; PlanStart planStart;
if (plan instanceof PlanStart) { if (plan instanceof PlanStart) {
planStart = (PlanStart) plan; planStart = (PlanStart) plan;
} else { } else {
planStart = planStartDefault; planStart = planStartDefault;
} }
PlanGlobal planGlobal; return planStart;
if (plan instanceof PlanGlobal) { }
planGlobal = (PlanGlobal) plan;
/**
* 获取任务的级别
*
* @param plan 任务
* @return 任务级别
*/
private PlanLevel getPlanLevel(Plan plan) {
PlanLevel planLevel;
if (plan instanceof PlanLevel) {
planLevel = (PlanLevel) plan;
} else { } else {
planGlobal = planGlobalDefault; planLevel = planLevelDefault;
} }
return planLevel;
}
/**
* 获取任务名称
*
* @param plan 任务
* @return 任务名称
*/
private PlanName getPlanName(Plan plan) {
PlanName planName; PlanName planName;
if (plan instanceof PlanName) { if (plan instanceof PlanName) {
planName = (PlanName) plan; planName = (PlanName) plan;
} else { } else {
planName = new PlanNameDefault(plan.getClass()); planName = new PlanNameDefault(plan.getClass());
} }
// 任务配置信息 return planName;
PlanConfigVo planConfigVo = new PlanConfigVo(plan, planName, planGlobal, planStart); }
// 任务配置
mapPlan.put(planName.getPlanKey(), planConfigVo); /**
* 获取应用名称
*
* @param plan 任务
* @param planLevel 任务程序级别
* @return 任务程序级别名称
*/
private PlanLevelNamespace getPlanLevelNamespace(Plan plan, PlanLevel planLevel) {
if (plan instanceof PlanLevelNamespace) {
return (PlanLevelNamespace) plan;
}
PlanLevelNamespace defaultLevelNamespace = mapLevelTypeNamespaceDefault.get(planLevel.getLevel());
if (defaultLevelNamespace != null) {
return defaultLevelNamespace;
}
Log.error(plan.getClass(), "没有实现 PlanLevelNamespace 接口,或者没有默认级别处理程序");
return null;
} }
} }
...@@ -5,8 +5,10 @@ import com.alicp.jetcache.anno.CacheType; ...@@ -5,8 +5,10 @@ import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache; import com.alicp.jetcache.anno.CreateCache;
import com.yanzuoguang.redis.PlanContains; import com.yanzuoguang.redis.PlanContains;
import com.yanzuoguang.redis.PlanInfo; import com.yanzuoguang.redis.PlanInfo;
import com.yanzuoguang.redis.PlanLevelNamespace;
import com.yanzuoguang.redis.mq.PlanProcedure; import com.yanzuoguang.redis.mq.PlanProcedure;
import com.yanzuoguang.redis.vo.PlanConfigVo; import com.yanzuoguang.redis.vo.PlanConfigVo;
import com.yanzuoguang.redis.vo.PlanLevelType;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -141,10 +143,8 @@ public class PlanService { ...@@ -141,10 +143,8 @@ public class PlanService {
* @return 任务关键字 * @return 任务关键字
*/ */
private String getCacheKey(PlanConfigVo config, String key) { private String getCacheKey(PlanConfigVo config, String key) {
if (config.getPlanGlobal().isGlobal()) { PlanLevelNamespace planLevelNamespace = config.getPlanLevelNamespace();
return StringHelper.getId(key); PlanLevelType level = planLevelNamespace.getLevel();
} else { return StringHelper.getId(level.getName(), planLevelNamespace.getLevelNamespace(), key);
return StringHelper.getId(planRegister.getApplicationName(), key);
}
} }
} }
package com.yanzuoguang.redis.vo; package com.yanzuoguang.redis.vo;
import com.yanzuoguang.redis.Plan; import com.yanzuoguang.redis.*;
import com.yanzuoguang.redis.PlanGlobal;
import com.yanzuoguang.redis.PlanName;
import com.yanzuoguang.redis.PlanStart;
/** /**
* 任务配置 * 任务配置
...@@ -19,20 +16,29 @@ public class PlanConfigVo { ...@@ -19,20 +16,29 @@ public class PlanConfigVo {
* 任务名称 * 任务名称
*/ */
private final PlanName planName; private final PlanName planName;
/**
* 是否全局运行
*/
private final PlanGlobal planGlobal;
/** /**
* 是否立即运行 * 是否立即运行
*/ */
private final PlanStart planStart; private final PlanStart planStart;
/**
* 应用程序级别
*/
private final PlanLevel planLevel;
/**
* 应用程序级别命名空间
*/
private final PlanLevelNamespace planLevelNamespace;
public PlanConfigVo(Plan plan, PlanName planName, PlanGlobal planGlobal, PlanStart planStart) { public PlanConfigVo(Plan plan,
PlanName planName,
PlanStart planStart,
PlanLevel planLevel,
PlanLevelNamespace planLevelNamespace) {
this.plan = plan; this.plan = plan;
this.planName = planName; this.planName = planName;
this.planGlobal = planGlobal; this.planLevel = planLevel;
this.planStart = planStart; this.planStart = planStart;
this.planLevelNamespace = planLevelNamespace;
} }
public Plan getPlan() { public Plan getPlan() {
...@@ -43,11 +49,15 @@ public class PlanConfigVo { ...@@ -43,11 +49,15 @@ public class PlanConfigVo {
return planName; return planName;
} }
public PlanGlobal getPlanGlobal() {
return planGlobal;
}
public PlanStart getPlanStart() { public PlanStart getPlanStart() {
return planStart; return planStart;
} }
public PlanLevel getPlanLevel() {
return planLevel;
}
public PlanLevelNamespace getPlanLevelNamespace() {
return planLevelNamespace;
}
} }
package com.yanzuoguang.redis.vo;
/**
* 任务级别
*
* @author 颜佐光
*/
public enum PlanLevelType {
/**
* SpringCloud全局级别
*/
Global("Global", 0),
/**
* SpringBoot同Application.Name 级别
*/
Application("Application", 1),
/**
* 同一个程序级别
*/
ServerIp("ServerIp", 2),
/**
* 同一个程序级别
*/
Program("Program", 3),
/**
* 自定义级别,
* 自定义需要任务实现 PlanLevelNamespace 接口,
* 或者自己全局定义实现 PlanLevelNamespaceDefault 接口;
* 当没有实现的时候,该任务会错误提示并且将不会运行
*/
Define("Define", 4);
/**
* Id
*/
private int id;
/**
* 成员变量
*/
private String name;
/**
* 构造方法
*
* @param name 名称
* @param id id
*/
private PlanLevelType(String name, int id) {
this.name = name;
this.id = id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
/**
* 覆盖方法
*
* @return 获取名称
*/
@Override
public String toString() {
return this.id + "-" + this.name;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment