之前一篇用过了如何在使用创建最简单的任务:比如每天定时清空系统的缓存
这篇文章主要讲解:如何运用elastic-job-lite做灵活的细粒度任务,比如:
如何定时取消某个订单在下订单后30分钟未支付的订单,并改变订单状态?
如何让某个用户在获得7天体验会员在七天后改变这个会员的会员状态?
某个用户想定时发布一篇文章?
如何给某个会员在生日当天发送一条祝福短信?
elastic-job-lite 就能实现这样的需求……
主要是任务配置,任务执行类都是一样的,下面贴出了demo,仅限于单应用节点时,主要为了实现如何动态的配置任务参数并达到上述需求,方法应用比较简单
首先要有任务(作业)类,并交给spring管理类
/* * Copyright 1999-2015 dangdang.com. * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * </p> */ package com.dianji.task_server.job.exec; import com.dangdang.ddframe.job.api.ShardingContext; import com.dangdang.ddframe.job.api.simple.SimpleJob; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.text.MessageFormat; @Slf4j @Component public class OrderExpireJob implements SimpleJob { @Value("${serverFlag}") private String serverFlag; @Override public void execute(final ShardingContext shardingContext) { int shardingItem = shardingContext.getShardingItem(); String jobName = shardingContext.getJobName(); String jobParameter = shardingContext.getJobParameter(); String logRule = "「执行订单超时任务」任务名:{0},订单号:{1},任务分片索引:{2},服务进程「{3}」"; String logStr = MessageFormat.format(logRule, jobName, jobParameter, shardingItem, serverFlag); log.info(logStr); } }