1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.trendmicro.grid.acl.ds.cache.commands;
18
19 import com.trendmicro.grid.acl.ds.cache.CacheDestination;
20 import com.trendmicro.grid.acl.ds.cache.CacheSettings;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.Collection;
25 import java.util.Map;
26 import java.util.concurrent.Callable;
27
28
29
30
31
32
33
34 public class BatchCommand<K, V> extends AbstractCommand<K, V> {
35
36 private static final Logger log = LoggerFactory.getLogger(BatchCommand.class);
37
38 Callable<Map<K, V>> wrappedCommand;
39 CacheDestination[] cacheDestination;
40
41
42
43
44
45
46
47
48 public BatchCommand(Collection<K> keys, Callable<Map<K, V>> wrappedCommand, CacheDestination... cacheDestination) {
49 super(keys);
50 this.wrappedCommand = wrappedCommand;
51 this.cacheDestination = cacheDestination;
52 }
53
54
55
56
57
58
59
60 protected Map<K, V> callInBatch() throws Exception {
61 boolean success = false;
62 boolean[] batchEnabled = new boolean[cacheDestination.length];
63
64 final boolean trace = log.isTraceEnabled();
65 try {
66 for (int i = 0; i < cacheDestination.length; i++) {
67 CacheDestination destination = cacheDestination[i];
68 if (destination.supportsBatching()) {
69 batchEnabled[i] = destination.startBatch();
70
71 if (trace) log.trace("Opened batch on destination {} (success: {})", destination, batchEnabled[i]);
72 }
73 }
74
75 Map<K, V> result = wrappedCommand.call();
76 success = true;
77 return result;
78
79 } finally {
80 for (int i = 0; i < cacheDestination.length; i++) {
81 try {
82 if (batchEnabled[i]) {
83 cacheDestination[i].endBatch(success);
84
85 if (trace) log.trace("Closed batch on destination {} (success: {})", cacheDestination[i], success);
86 }
87 } catch (Exception e) {
88 log.warn("TMACL-01730:Failed to close batch on cache destination " + cacheDestination[i], e);
89 }
90 }
91 }
92 }
93
94
95
96
97 @Override
98 public Map<K, V> call() throws Exception {
99 if (CacheSettings.NO_BATCHING)
100 return wrappedCommand.call();
101 else
102 return callInBatch();
103 }
104 }