提交 | 用户 | 时间
|
e7c126
|
1 |
package com.xxl.job.admin.core.route.strategy; |
H |
2 |
|
|
3 |
import com.xxl.job.admin.core.route.ExecutorRouter; |
|
4 |
import com.xxl.job.core.biz.model.ReturnT; |
|
5 |
import com.xxl.job.core.biz.model.TriggerParam; |
|
6 |
|
|
7 |
import java.util.List; |
|
8 |
import java.util.Random; |
|
9 |
import java.util.concurrent.ConcurrentHashMap; |
|
10 |
import java.util.concurrent.ConcurrentMap; |
|
11 |
import java.util.concurrent.atomic.AtomicInteger; |
|
12 |
|
|
13 |
/** |
|
14 |
* Created by xuxueli on 17/3/10. |
|
15 |
*/ |
|
16 |
public class ExecutorRouteRound extends ExecutorRouter { |
|
17 |
|
|
18 |
private static ConcurrentMap<Integer, AtomicInteger> routeCountEachJob = new ConcurrentHashMap<>(); |
|
19 |
private static long CACHE_VALID_TIME = 0; |
|
20 |
|
|
21 |
private static int count(int jobId) { |
|
22 |
// cache clear |
|
23 |
if (System.currentTimeMillis() > CACHE_VALID_TIME) { |
|
24 |
routeCountEachJob.clear(); |
|
25 |
CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*24; |
|
26 |
} |
|
27 |
|
|
28 |
AtomicInteger count = routeCountEachJob.get(jobId); |
|
29 |
if (count == null || count.get() > 1000000) { |
|
30 |
// 初始化时主动Random一次,缓解首次压力 |
|
31 |
count = new AtomicInteger(new Random().nextInt(100)); |
|
32 |
} else { |
|
33 |
// count++ |
|
34 |
count.addAndGet(1); |
|
35 |
} |
|
36 |
routeCountEachJob.put(jobId, count); |
|
37 |
return count.get(); |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public ReturnT<String> route(TriggerParam triggerParam, List<String> addressList) { |
|
42 |
String address = addressList.get(count(triggerParam.getJobId())%addressList.size()); |
|
43 |
return new ReturnT<String>(address); |
|
44 |
} |
|
45 |
|
|
46 |
} |