// 支持动态扩容
private static String ip="192.168.1.3";
//获取所有任务
@Test
public void execute() throws InterruptedException {
CacheUtil cacheUtil01=context.getBean("cacheUtil01",CacheUtil.class);
cacheUtil01.lPush("ipList",ip); // 优化点 注册的成功后 获取返回的索引(分片Id)
//int sheardId=1;
//根据 分片id 获取任务
//List<Integer> jobs=getJobs(sheardId);
for(int i=0;i<365;i++){
List<String> list=cacheUtil01.hMget("ipList-Map",String.valueOf(i));
if(CollectionUtils.isEmpty(list) || list.get(0) == null) {
Thread.sleep(1000);
executeTask(i, "110");
}
}
}
//执行任务
public void executeTask(int day,String shopguid){
CacheUtil cacheUtil01=context.getBean("cacheUtil01",CacheUtil.class);
//list llen
List<String> ips=cacheUtil01.lRange("ipList",0,10);
int mode=day%ips.size();
if(ips.get(mode).equals(ip)){
cacheUtil01.hAdd("ipList-Map",String.valueOf(day),ip);
System.out.println(ip+":"+shopguid+"-第"+day+"天");
}
}
redis 模拟quartz sharding
最新推荐文章于 2025-04-21 09:36:27 发布
本文介绍了一种基于缓存的任务调度机制,通过动态扩容及任务分配策略实现高效的任务管理。利用缓存工具类进行任务的分配与存储,并通过循环算法确保任务能够均衡地分配到不同的IP地址上。
2145

被折叠的 条评论
为什么被折叠?



