当前位置:网站首页>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();
}
边栏推荐
猜你喜欢
随机推荐
How to open a futures account? Is it safe to open an online futures account?
uniapp微信小程序获取页面二维码(带有参数)
1.类的继承(point)
天翼云乘风新基建,构建数字化转型“4+2”能力体系
超出文本部分用省略号表示
sql语法检测
nio编程service
nio使用可写事件处理一次性写不完情况
SAP教程中的ALV报告 - ABAP列表查看器-012
十九、Xv6上下文切换(上下文切换的实现;状态机的封装与恢复)
'不敢去怀疑代码,又不得不怀疑代码'记一次网络请求超时分析
Runtime -- explore the nature of classes, objects, and classifications
毕业季·本科毕业感想——机械er的自救之路
ALV report in SAP tutorial - ABAP list viewer -012
SAP价值流程&帮助请求流程-011
Google Chrome small details
使用stream api替代sql
SAP ABAP BAPI-016
默认函数控制 =default 与 =delete
Linux安装mysql





![Prometheus监控之Consul监控 [consul-exporter]](/img/9e/8547b2c38143ab0e051c1cf0b04986.png)


