-
Notifications
You must be signed in to change notification settings - Fork 105
/
MulticastParams.java
341 lines (281 loc) · 11.1 KB
/
MulticastParams.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// info@rabbitmq.com.
package com.rabbitmq.perf;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ShutdownSignalException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MulticastParams {
private long confirm = -1;
private int consumerCount = 1;
private int producerCount = 1;
private int consumerChannelCount = 1;
private int producerChannelCount = 1;
private int consumerTxSize = 0;
private int producerTxSize = 0;
private int channelPrefetch = 0;
private int consumerPrefetch = 0;
private int minMsgSize = 0;
private int timeLimit = 0;
private float producerRateLimit = 0;
private float consumerRateLimit = 0;
private int producerMsgCount = 0;
private int consumerMsgCount = 0;
private String exchangeName = "direct";
private String exchangeType = "direct";
private List<String> queueNames = new ArrayList<String>();
private String routingKey = null;
private boolean randomRoutingKey = false;
private List<?> flags = new ArrayList<Object>();
private int multiAckEvery = 0;
private boolean autoAck = true;
private boolean autoDelete = false;
private List<String> bodyFiles = new ArrayList<String>();
private String bodyContentType = null;
private boolean predeclared;
private Map<String, Object> queueArguments;
private int consumerLatencyInMicroseconds;
public void setExchangeType(String exchangeType) {
this.exchangeType = exchangeType;
}
public void setExchangeName(String exchangeName) {
this.exchangeName = exchangeName;
}
public void setQueueNames(List<String> queueNames) {
if(queueNames == null) {
this.queueNames = new ArrayList<String>();
} else {
this.queueNames = new ArrayList<String>(queueNames);
}
}
public void setRoutingKey(String routingKey) {
this.routingKey = routingKey;
}
public void setRandomRoutingKey(boolean randomRoutingKey) {
this.randomRoutingKey = randomRoutingKey;
}
public void setProducerRateLimit(float producerRateLimit) {
this.producerRateLimit = producerRateLimit;
}
public void setProducerCount(int producerCount) {
this.producerCount = producerCount;
}
public void setProducerChannelCount(int producerChannelCount) {
this.producerChannelCount = producerChannelCount;
}
public void setConsumerRateLimit(float consumerRateLimit) {
this.consumerRateLimit = consumerRateLimit;
}
public void setConsumerCount(int consumerCount) {
this.consumerCount = consumerCount;
}
public void setConsumerChannelCount(int consumerChannelCount) {
this.consumerChannelCount = consumerChannelCount;
}
public void setProducerTxSize(int producerTxSize) {
this.producerTxSize = producerTxSize;
}
public void setConsumerTxSize(int consumerTxSize) {
this.consumerTxSize = consumerTxSize;
}
public void setConfirm(long confirm) {
this.confirm = confirm;
}
public void setAutoAck(boolean autoAck) {
this.autoAck = autoAck;
}
public void setMultiAckEvery(int multiAckEvery) {
this.multiAckEvery = multiAckEvery;
}
public void setChannelPrefetch(int channelPrefetch) {
this.channelPrefetch = channelPrefetch;
}
public void setConsumerPrefetch(int consumerPrefetch) {
this.consumerPrefetch = consumerPrefetch;
}
public void setMinMsgSize(int minMsgSize) {
this.minMsgSize = minMsgSize;
}
public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
}
public void setProducerMsgCount(int producerMsgCount) {
this.producerMsgCount = producerMsgCount;
}
public void setConsumerMsgCount(int consumerMsgCount) {
this.consumerMsgCount = consumerMsgCount;
}
public void setMsgCount(int msgCount) {
setProducerMsgCount(msgCount);
setConsumerMsgCount(msgCount);
}
public void setFlags(List<?> flags) {
this.flags = flags;
}
public void setAutoDelete(boolean autoDelete) {
this.autoDelete = autoDelete;
}
public void setPredeclared(boolean predeclared) {
this.predeclared = predeclared;
}
public void setQueueArguments(Map<String, Object> queueArguments) {
this.queueArguments = queueArguments;
}
public void setConsumerLatencyInMicroseconds(int consumerLatencyInMicroseconds) {
this.consumerLatencyInMicroseconds = consumerLatencyInMicroseconds;
}
public int getConsumerCount() {
return consumerCount;
}
public int getConsumerChannelCount() {
return consumerChannelCount;
}
public int getConsumerThreadCount() {
return consumerCount * consumerChannelCount;
}
public int getProducerCount() {
return producerCount;
}
public int getProducerChannelCount() {
return producerChannelCount;
}
public int getProducerThreadCount() {
return producerCount * producerChannelCount;
}
public int getMinMsgSize() {
return minMsgSize;
}
public String getRoutingKey() {
return routingKey;
}
public boolean getRandomRoutingKey() {
return randomRoutingKey;
}
public void setBodyFiles(List<String> bodyFiles) {
if (bodyFiles == null) {
this.bodyFiles = new ArrayList<String>();
} else {
this.bodyFiles = new ArrayList<String>(bodyFiles);
}
}
public void setBodyContentType(String bodyContentType) {
this.bodyContentType = bodyContentType;
}
public Producer createProducer(Connection connection, Stats stats, String id) throws IOException {
Channel channel = connection.createChannel();
if (producerTxSize > 0) channel.txSelect();
if (confirm >= 0) channel.confirmSelect();
if (!predeclared || !exchangeExists(connection, exchangeName)) {
channel.exchangeDeclare(exchangeName, exchangeType);
}
MessageBodySource messageBodySource = null;
if (bodyFiles.size() > 0) {
messageBodySource = new LocalFilesMessageBodySource(bodyFiles, bodyContentType);
} else {
messageBodySource = new TimeSequenceMessageBodySource(minMsgSize);
}
final Producer producer = new Producer(channel, exchangeName, id,
randomRoutingKey, flags, producerTxSize,
producerRateLimit, producerMsgCount,
timeLimit,
confirm, messageBodySource, stats);
channel.addReturnListener(producer);
channel.addConfirmListener(producer);
return producer;
}
public Consumer createConsumer(Connection connection, Stats stats, String id) throws IOException {
Channel channel = connection.createChannel();
if (consumerTxSize > 0) channel.txSelect();
List<String> generatedQueueNames = configureQueues(connection, id);
if (consumerPrefetch > 0) channel.basicQos(consumerPrefetch);
if (channelPrefetch > 0) channel.basicQos(channelPrefetch, true);
return new Consumer(channel, id, generatedQueueNames,
consumerTxSize, autoAck, multiAckEvery,
stats, consumerRateLimit, consumerMsgCount, timeLimit, consumerLatencyInMicroseconds);
}
public boolean shouldConfigureQueues() {
// don't declare any queues when --predeclared is passed,
// otherwise unwanted server-named queues without consumers will pile up. MK.
return consumerCount == 0 && !predeclared && !(queueNames.size() == 0);
}
public List<String> configureQueues(Connection connection, String id) throws IOException {
Channel channel = connection.createChannel();
if (!predeclared || !exchangeExists(connection, exchangeName)) {
channel.exchangeDeclare(exchangeName, exchangeType);
}
// To ensure we get at-least 1 default queue:
if (queueNames.isEmpty()) {
queueNames.add("");
}
List<String> generatedQueueNames = new ArrayList<String>();
for (String qName : queueNames) {
if (!predeclared || !queueExists(connection, qName)) {
qName = channel.queueDeclare(qName,
flags.contains("persistent"),
false,
autoDelete,
queueArguments).getQueue();
}
generatedQueueNames.add(qName);
channel.queueBind(qName, exchangeName, id);
}
channel.abort();
return generatedQueueNames;
}
private static boolean exchangeExists(Connection connection, final String exchangeName) throws IOException {
if ("".equals(exchangeName)) {
// NB: default exchange always exists
return true;
} else {
return exists(connection, new Checker() {
public void check(Channel ch) throws IOException {
ch.exchangeDeclarePassive(exchangeName);
}
});
}
}
private static boolean queueExists(Connection connection, final String queueName) throws IOException {
return queueName != null && exists(connection, new Checker() {
public void check(Channel ch) throws IOException {
ch.queueDeclarePassive(queueName);
}
});
}
private interface Checker {
void check(Channel ch) throws IOException;
}
private static boolean exists(Connection connection, Checker checker) throws IOException {
try {
Channel ch = connection.createChannel();
checker.check(ch);
ch.abort();
return true;
}
catch (IOException e) {
ShutdownSignalException sse = (ShutdownSignalException) e.getCause();
if (!sse.isHardError()) {
AMQP.Channel.Close closeMethod = (AMQP.Channel.Close) sse.getReason();
if (closeMethod.getReplyCode() == AMQP.NOT_FOUND) {
return false;
}
}
throw e;
}
}
}