21
21
import com .google .common .util .concurrent .ListenableFuture ;
22
22
import com .google .common .util .concurrent .ListeningExecutorService ;
23
23
import com .google .common .util .concurrent .MoreExecutors ;
24
- import lombok .Getter ;
25
24
26
25
import java .util .ArrayList ;
27
26
import java .util .Collection ;
28
27
import java .util .Collections ;
29
28
import java .util .Iterator ;
30
29
import java .util .LinkedList ;
31
30
import java .util .List ;
31
+ import java .util .Map ;
32
+ import java .util .Map .Entry ;
32
33
import java .util .concurrent .Callable ;
33
34
import java .util .concurrent .ExecutionException ;
34
35
import java .util .concurrent .ExecutorService ;
@@ -44,7 +45,6 @@ public final class ShardingExecuteEngine implements AutoCloseable {
44
45
45
46
private static final ExecutorService SHUTDOWN_EXECUTOR = Executors .newSingleThreadExecutor (ShardingThreadFactoryBuilder .build ("Executor-Engine-Closer" ));
46
47
47
- @ Getter
48
48
private final ListeningExecutorService executorService ;
49
49
50
50
public ShardingExecuteEngine (final int executorSize ) {
@@ -56,7 +56,7 @@ public ShardingExecuteEngine(final int executorSize) {
56
56
/**
57
57
* Execute all callbacks.
58
58
*
59
- * @param inputs sharding execute callbacks
59
+ * @param inputs input values
60
60
* @param callback sharding execute callback
61
61
* @param <I> type of input value
62
62
* @param <O> type of return value
@@ -70,7 +70,28 @@ public <I, O> List<O> execute(final Collection<I> inputs, final ShardingExecuteC
70
70
Iterator <I > inputIterator = inputs .iterator ();
71
71
I firstInput = inputIterator .next ();
72
72
Collection <ListenableFuture <O >> restFutures = asyncExecute (Lists .newArrayList (inputIterator ), callback );
73
- return getResults (callback .execute (firstInput ), restFutures );
73
+ return getResults (syncExecute (firstInput , callback ), restFutures );
74
+ }
75
+
76
+ /**
77
+ * Execute all callbacks.
78
+ *
79
+ * @param inputs input values
80
+ * @param firstCallback first sharding execute callback
81
+ * @param callback sharding execute callback
82
+ * @param <I> type of input value
83
+ * @param <O> type of return value
84
+ * @return execute result
85
+ * @throws Exception throw if execute failure
86
+ */
87
+ public <I , O > List <O > execute (final Collection <I > inputs , final ShardingExecuteCallback <I , O > firstCallback , final ShardingExecuteCallback <I , O > callback ) throws Exception {
88
+ if (inputs .isEmpty ()) {
89
+ return Collections .emptyList ();
90
+ }
91
+ Iterator <I > inputIterator = inputs .iterator ();
92
+ I firstInput = inputIterator .next ();
93
+ Collection <ListenableFuture <O >> restFutures = asyncExecute (Lists .newArrayList (inputIterator ), callback );
94
+ return getResults (syncExecute (firstInput , firstCallback ), restFutures );
74
95
}
75
96
76
97
private <I , O > Collection <ListenableFuture <O >> asyncExecute (final Collection <I > inputs , final ShardingExecuteCallback <I , O > callback ) {
@@ -87,6 +108,10 @@ public O call() throws Exception {
87
108
return result ;
88
109
}
89
110
111
+ private <I , O > O syncExecute (final I input , final ShardingExecuteCallback <I , O > callback ) throws Exception {
112
+ return callback .execute (input );
113
+ }
114
+
90
115
private <O > List <O > getResults (final O firstResult , final Collection <ListenableFuture <O >> restFutures ) throws ExecutionException , InterruptedException {
91
116
List <O > result = new LinkedList <>();
92
117
result .add (firstResult );
@@ -96,6 +121,75 @@ private <O> List<O> getResults(final O firstResult, final Collection<ListenableF
96
121
return result ;
97
122
}
98
123
124
+ /**
125
+ * execute all callbacks for group.
126
+ *
127
+ * @param inputs input value's map
128
+ * @param callback sharding execute callback
129
+ * @param <I> type of input value
130
+ * @param <O> type of return value
131
+ * @return execute result
132
+ * @throws Exception throw if execute failure
133
+ */
134
+ public <I , O > List <O > groupExecute (final Map <String , Collection <I >> inputs , final ShardingGroupExecuteCallback <I , O > callback ) throws Exception {
135
+ if (inputs .isEmpty ()) {
136
+ return Collections .emptyList ();
137
+ }
138
+ String firstKey = inputs .keySet ().iterator ().next ();
139
+ Collection <I > firstInputs = inputs .remove (firstKey );
140
+ Collection <ListenableFuture <Collection <O >>> restResultFutures = asyncGroupExecute (inputs , callback );
141
+ return getGroupResults (syncGroupExecute (firstKey , firstInputs , callback ), restResultFutures );
142
+ }
143
+
144
+ /**
145
+ * execute all callbacks for group.
146
+ *
147
+ * @param inputs input value's map
148
+ * @param callback sharding execute callback
149
+ * @param firstCallback first sharding execute callback
150
+ * @param <I> type of input value
151
+ * @param <O> type of return value
152
+ * @return execute result
153
+ * @throws Exception throw if execute failure
154
+ */
155
+ public <I , O > List <O > groupExecute (
156
+ final Map <String , Collection <I >> inputs , final ShardingGroupExecuteCallback <I , O > firstCallback , final ShardingGroupExecuteCallback <I , O > callback ) throws Exception {
157
+ if (inputs .isEmpty ()) {
158
+ return Collections .emptyList ();
159
+ }
160
+ String firstKey = inputs .keySet ().iterator ().next ();
161
+ Collection <I > firstInputs = inputs .remove (firstKey );
162
+ Collection <ListenableFuture <Collection <O >>> restResultFutures = asyncGroupExecute (inputs , callback );
163
+ return getGroupResults (syncGroupExecute (firstKey , firstInputs , firstCallback ), restResultFutures );
164
+ }
165
+
166
+ private <I , O > Collection <ListenableFuture <Collection <O >>> asyncGroupExecute (final Map <String , Collection <I >> inputs , final ShardingGroupExecuteCallback <I , O > callback ) {
167
+ Collection <ListenableFuture <Collection <O >>> result = new ArrayList <>(inputs .size ());
168
+ for (final Entry <String , Collection <I >> entry : inputs .entrySet ()) {
169
+ result .add (executorService .submit (new Callable <Collection <O >>() {
170
+
171
+ @ Override
172
+ public Collection <O > call () throws Exception {
173
+ return callback .execute (entry .getKey (), entry .getValue ());
174
+ }
175
+ }));
176
+ }
177
+ return result ;
178
+ }
179
+
180
+ private <I , O > Collection <O > syncGroupExecute (final String dataSourceName , final Collection <I > inputs , final ShardingGroupExecuteCallback <I , O > callback ) throws Exception {
181
+ return callback .execute (dataSourceName , inputs );
182
+ }
183
+
184
+ private <O > List <O > getGroupResults (final Collection <O > firstResults , final Collection <ListenableFuture <Collection <O >>> restFutures ) throws ExecutionException , InterruptedException {
185
+ List <O > result = new LinkedList <>();
186
+ result .addAll (firstResults );
187
+ for (ListenableFuture <Collection <O >> each : restFutures ) {
188
+ result .addAll (each .get ());
189
+ }
190
+ return result ;
191
+ }
192
+
99
193
@ Override
100
194
public void close () {
101
195
SHUTDOWN_EXECUTOR .execute (new Runnable () {
0 commit comments