当前位置:网站首页>Implementing factory mode using enumeration
Implementing factory mode using enumeration
2022-06-22 16:29:00 【Stay for love】
For example, there are four dimensions of daily life / Week degree / Customize , Achieve scheduled scheduling , Now each dimension has an implementation class , When creating timers of different dimensions , Will trigger the implementation class to execute
Enumeration class :
package com.htf.damp.custom_enum;
import com.htf.damp.quartz.QuartzCustomManager;
import com.htf.damp.quartz.QuartzDayManager;
import com.htf.damp.quartz.QuartzMonthManager;
import com.htf.damp.quartz.QuartzWeekManager;
public enum FrequencyEnum {
DAY(1," Day degree ", QuartzDayManager.class),
WEEK(2," Week degree ", QuartzWeekManager.class),
MONTH(3," monthly ", QuartzMonthManager.class),
CUSTOM(4," Customize ", QuartzCustomManager.class);
private Integer code;
private String value;
private Class aClass;
private FrequencyEnum(Integer code,String value,Class aClass){
this.code=code;
this.value=value;
this.aClass=aClass;
}
public Integer getCode() {
return code;
}
public String getValue() {
return value;
}
public static String findValueByCode(Integer code){
for(FrequencyEnum v : values()){
if(v.code.equals(code)){
return v.value;
}
}
return null;
}
public static Class findClassByCode(Integer code){
for(FrequencyEnum v : values()){
if(v.code.equals(code)){
return v.aClass;
}
}
return null;
}
}
QuartzCustomManager Day degree ,QuartzDayManager Week degree ,QuartzMonthManager monthly
QuartzWeekManager Customize , All inherited CreateTaskQuartzManager class
call :
void processSchedule(RuleSchedulePO ruleSchedulePO,RuleExecutorPO ruleExecutorPO, String tableName,String executeType) {
String genMode = ruleSchedulePO.getTimeGenMode();
CreateTaskModel createTaskModel = new CreateTaskModel();
createTaskModel.setExecutorId(ruleExecutorPO.getExecutorId());
createTaskModel.setGenCode(genMode);
createTaskModel.setRuleId(ruleExecutorPO.getRuleId());
createTaskModel.setScheduleId(ruleSchedulePO.getId());
createTaskModel.setIsSkipWeekend(ruleSchedulePO.getIsSkipWeekend());
createTaskModel.setOverTime(ruleSchedulePO.getOverTime());
createTaskModel.setStartTime(ruleSchedulePO.getStartTime());
createTaskModel.setTableName(tableName);
createTaskModel.setFrequency(ruleSchedulePO.getFrequency());
createTaskModel.setScheduleTime(ruleSchedulePO.getScheduleTime());
CreateTaskQuartzManager createTaskQuartzManager = (CreateTaskQuartzManager) SpringContextHolder.getBean(FrequencyEnum.findClassByCode(ruleSchedulePO.getFrequency()));
if("addJob".equals(executeType)){
createTaskQuartzManager.addJob( createTaskModel);
}else{
createTaskQuartzManager.deleteJob(createTaskModel);
}
}I'm here through CreateTaskQuartzManager createTaskQuartzManager = (CreateTaskQuartzManager) SpringContextHolder.getBean(FrequencyEnum.findClassByCode(ruleSchedulePO.getFrequency())); To make the call ,SpringContextHolder.getBean It's from IOC Get objects from the container , That is to say, one line of code can pass through Frequency Get the corresponding implementation class
SpringContextHolder class :
package com.htf.damp.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* Realization ApplicationContextAware Interface context Injection function , Put it in a static variable .
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext; // NOSONAR
}
/**
* Gets the... Stored in a static variable ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* From static variables ApplicationContext Get in Bean, Automatically convert to the type of the assigned object .
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* From static variables ApplicationContext Get in Bean, Automatically convert to the type of the assigned object .
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBean(clazz);
}
/**
* eliminate applicationContext Static variables .
*/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException(
"applicaitonContext Not injected , Please be there. applicationContext.xml In the definition of SpringContextHolder");
}
}
public static void setHttpRequestResponseHolder(HttpServletRequest request, HttpServletResponse response){
responseThreadLocal.set(response);
ApplicationContext ap = WebApplicationContextUtils.getWebApplicationContext(null);
}
public static HttpServletResponse getHttpResponse(){
return responseThreadLocal.get();
}
public static void clean(){
responseThreadLocal.remove();
}
private static final ThreadLocal<HttpServletResponse> responseThreadLocal = new ThreadLocal();
}
边栏推荐
猜你喜欢
随机推荐
NiO uses writable events to handle the situation of one-time write incompleteness
用递归法求Fibonacci数列第n项的值
北京恢复堂食半月记:如何重燃门店经营烟火气
SAP ABAP 报告编程-08
为数字添加千分位符号(金额千分位)
【C语言深度解剖】关键字if&&else&&bool类型
nio使用可写事件处理一次性写不完情况
[Shanda conference] project initialization
How to open a futures account? Is it safe to open an online futures account?
SAP教程中的ALV报告 - ABAP列表查看器-012
首个赛博格人陨落背后:科技与渐冻症的极限赛跑
CUMT学习日记——数字图像处理考试速成笔记
SAP ABAP table control and example-07
SAP value process & help request process-011
什么是 SAP ABAP? 类型、ABAP 完整形式和含义
SLAM十四讲之第6讲--非线性优化
Reddit对LaMDA模型的探讨:并非无状态,采用双重过程,相比它编辑维基百科的方式,有没有感情并不重要
jmeter关联登录302类型的接口
Mr. Du built a domestic non garlic Statistics Platform
Linux安装mysql









