模板方法设计模式

自己整理的设计模式案例,如果不对欢迎大家吐槽!

 

模板方法设计模式

解释一下模板方法模式,就是指:一个抽象类中,有一个主方法,再定义1...n个方法,可以是抽象的,也可以是实际的方法,定义一个类,继承该抽象类,重写抽象方法,通过调用抽象类,实现对子类的调用

 

具体使用场景


quartz 定时任务举例

 

public abstract class QuartzJobBean implements Job {

	private static final Method getSchedulerMethod;

	private static final Method getMergedJobDataMapMethod;


	static {
		try {
			Class jobExecutionContextClass =
					QuartzJobBean.class.getClassLoader().loadClass("org.quartz.JobExecutionContext");
			getSchedulerMethod = jobExecutionContextClass.getMethod("getScheduler");
			getMergedJobDataMapMethod = jobExecutionContextClass.getMethod("getMergedJobDataMap");
		}
		catch (Exception ex) {
			throw new IllegalStateException("Incompatible Quartz API: " + ex);
		}
	}


	/**
	 * This implementation applies the passed-in job data map as bean property
	 * values, and delegates to {@code executeInternal} afterwards.
	 * @see #executeInternal
	 */
	public final void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			// Reflectively adapting to differences between Quartz 1.x and Quartz 2.0...
			Scheduler scheduler = (Scheduler) ReflectionUtils.invokeMethod(getSchedulerMethod, context);
			Map mergedJobDataMap = (Map) ReflectionUtils.invokeMethod(getMergedJobDataMapMethod, context);

			BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
			MutablePropertyValues pvs = new MutablePropertyValues();
			pvs.addPropertyValues(scheduler.getContext());
			pvs.addPropertyValues(mergedJobDataMap);
			bw.setPropertyValues(pvs, true);
		}
		catch (SchedulerException ex) {
			throw new JobExecutionException(ex);
		}
		executeInternal(context);
	}

	/**
	 * Execute the actual job. The job data map will already have been
	 * applied as bean property values by execute. The contract is
	 * exactly the same as for the standard Quartz execute method.
	 * @see #execute
	 */
	protected abstract void executeInternal(JobExecutionContext context) throws JobExecutionException;

}

 

上面的executeInternal 方法就是让子类去实现的抽象方法

 

 

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class TaskDetailJob extends QuartzJobBean {
	
	private static final Logger log = LoggerFactory.getLogger(TaskDetailJob.class);

	/**
	 * 任务明细定时任务,每天00:10分执行
	 * 0 10 0 * * ?
	 */
	@Override
	protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		log.info("定时执行创建红包定时任务" + new Date());
		
		TaskDetailService taskDetailService = getApplicationContext(jobExecutionContext).getBean("taskDetailService", TaskDetailService.class);
		taskDetailService.doScheduledJob();
	}
	
	private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) {
		try {
			return (ApplicationContext) jobexecutioncontext.getScheduler().getContext().get("applicationContext");
		} catch (SchedulerException e) {
			log.error("jobexecutioncontext.getScheduler().getContext() error!", e);
			throw new RuntimeException(e);
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值