java 调度池_一种基于线程池进行定时调度的实现方案

前言

实现定时调度的方案真的是太多了,此处实现经典的基于线程池的定时调度方案。

具体实现

1,编写调度线程管理类

@Slf4j

@Service

public class TimerTaskService {

private static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10, new ThreadFactory() {

AtomicInteger count = new AtomicInteger(0);

@Override

public Thread newThread(Runnable r) {

Thread t = new Thread(r);

t.setDaemon(true);

t.setName("Timer-" + count.getAndIncrement());

return t;

}

});

public static void scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,

TimeUnit unit) {

scheduledExecutorService.scheduleWithFixedDelay(command, initialDelay, delay, unit);

}

}

2,编写定时任务分发调度类

@Slf4j

@Service

public class TaskDispatchService implements Constant {

@Value("${spring.profiles.active}")

private String active;

@Autowired

private CheckMessageTask checkMessageTask;

@Autowired

private DelayMatchTask delayMatchTask;

long DELAY_INTERVAL_SECONDS = 60*10;

long CHECK_INTERVAL_SECONDS = 60*60;

@PostConstruct

public void init() {

log.info("TaskDispatchService init success .....");

if(!"prod".equals(active)) {

DELAY_INTERVAL_SECONDS = 60*5;

CHECK_INTERVAL_SECONDS = 60*5;

}

TimerTaskService.scheduleWithFixedDelay(delayMatchTask, 0L, DELAY_INTERVAL_SECONDS, TimeUnit.SECONDS);

TimerTaskService.scheduleWithFixedDelay(checkMessageTask, 0L, CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS);

}

}

3,编写具体的调度任务类

1)任务1: DelayMatchTask.java

@Slf4j

@Component

public class DelayMatchTask implements Runnable {

@Autowired

private SocialUserMatchMapper socialUserMatchMapper;

@Autowired

private SocialUserMatchService socialUserMatchService;

@Override

public void run() {

int startRow = 0;

int pageSize = 5;

try {

log.info("DelayMatchTask run start");

UserMatchInfo umi = new UserMatchInfo();

umi.setTipsFlag(0);

umi.setTipsTime(System.currentTimeMillis()/1000);

Map map = new HashMap<>();

map.put("userMatchInfo", umi);

map.put("startRow", startRow);

map.put("pageSize", pageSize);

boolean cycle = true;

while (true) {

if(!cycle) {

break;

}

List targetList = socialUserMatchMapper.queryMatchRoomNotTips(map);

if(null == targetList || targetList.size() <= 0) {

break;

} else if(targetList.size() < pageSize){

cycle = false;

}

for(UserMatchInfo userMatchInfo: targetList) {

try {

int count = socialUserMatchMapper.updateMatchRoom(userMatchInfo);

if (count > 1) {

socialUserMatchService.sendMatchSuccessMessage(userMatchInfo);

}

} catch (Exception e) {

log.error("run userMatchInfo:{} have exception:{}" , userMatchInfo, e.getLocalizedMessage());

}

}

startRow = startRow + pageSize;

}

} catch (Exception e) {

log.error("run have exception:{}" , e);

}

}

}

2)任务2: CheckMessageTask.java

@Slf4j

@Component

public class CheckMessageTask implements Runnable,RedisConstant,Constant {

@Autowired

private JedisCluster jedisCluster;

long RESERVED_LENGTH = 300;

@Value("${spring.profiles.active}")

private String active;

@Override

public void run() {

try {

log.info("CheckMessageTask run start");

if(!"prod".equals(active)) {

RESERVED_LENGTH = 50;

}

while (true) {

String activeRoomKey = REDIS_SOCIAL_CURRENT_ACTIVE_ROOM_SET;

long sLength = jedisCluster.scard(activeRoomKey);

if(sLength <= 0) {

return;

}

TimeUnit.SECONDS.sleep(1);

String roomId = jedisCluster.spop(activeRoomKey);

handleSingle(roomId);

}

} catch (Exception e) {

log.error("run have exception:{}" , e.getLocalizedMessage());

}

}

private void handleSingle(String roomId) {

String roomSetKey = String.format(REDIS_SOCIAL_ROOM_MESSAGE_ZSET, roomId);

long length = jedisCluster.zcard(roomSetKey);

log.info("roomSetKey:{} length:{}", roomSetKey, length);

if(length <= RESERVED_LENGTH) {

return;

}

long end = length - RESERVED_LENGTH - 1;

if(end < 0) {

return;

}

jedisCluster.zremrangeByRank(roomSetKey, 0, end);

}

}

本文地址:https://blog.csdn.net/FENGQIYUNRAN/article/details/107343511

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值