-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
RabbitMQDevServicesProcessor.java
397 lines (346 loc) · 17.1 KB
/
RabbitMQDevServicesProcessor.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package io.quarkus.smallrye.reactivemessaging.rabbitmq.deployment;
import java.io.Closeable;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.RabbitMQContainer;
import org.testcontainers.utility.DockerImageName;
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.DevServicesResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService;
import io.quarkus.deployment.builditem.DockerStatusBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.console.ConsoleInstalledBuildItem;
import io.quarkus.deployment.console.StartupLogCompressor;
import io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig;
import io.quarkus.deployment.logging.LoggingSetupBuildItem;
import io.quarkus.devservices.common.ContainerLocator;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.configuration.ConfigUtils;
/**
* Starts a RabbitMQ broker as dev service if needed.
*/
public class RabbitMQDevServicesProcessor {
private static final Logger log = Logger.getLogger(RabbitMQDevServicesProcessor.class);
/**
* Label to add to shared Dev Service for RabbitMQ running in containers.
* This allows other applications to discover the running service and use it instead of starting a new instance.
*/
private static final String DEV_SERVICE_LABEL = "quarkus-dev-service-rabbitmq";
private static final int RABBITMQ_PORT = 5672;
private static final int RABBITMQ_HTTP_PORT = 15672;
private static final ContainerLocator rabbitmqContainerLocator = new ContainerLocator(DEV_SERVICE_LABEL, RABBITMQ_PORT);
private static final String RABBITMQ_HOST_PROP = "rabbitmq-host";
private static final String RABBITMQ_PORT_PROP = "rabbitmq-port";
private static final String RABBITMQ_HTTP_PORT_PROP = "rabbitmq-http-port";
private static final String RABBITMQ_USERNAME_PROP = "rabbitmq-username";
private static final String RABBITMQ_PASSWORD_PROP = "rabbitmq-password";
static volatile RunningDevService devService;
static volatile RabbitMQDevServiceCfg cfg;
static volatile boolean first = true;
@BuildStep(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class)
public DevServicesResultBuildItem startRabbitMQDevService(
DockerStatusBuildItem dockerStatusBuildItem,
LaunchModeBuildItem launchMode,
RabbitMQBuildTimeConfig rabbitmqClientBuildTimeConfig,
Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem,
LoggingSetupBuildItem loggingSetupBuildItem,
GlobalDevServicesConfig devServicesConfig) {
RabbitMQDevServiceCfg configuration = getConfiguration(rabbitmqClientBuildTimeConfig);
if (devService != null) {
boolean shouldShutdownTheBroker = !configuration.equals(cfg);
if (!shouldShutdownTheBroker) {
return devService.toBuildItem();
}
shutdownBroker();
cfg = null;
}
StartupLogCompressor compressor = new StartupLogCompressor(
(launchMode.isTest() ? "(test) " : "") + "RabbitMQ Dev Services Starting:", consoleInstalledBuildItem,
loggingSetupBuildItem);
try {
RunningDevService newDevService = startRabbitMQBroker(dockerStatusBuildItem, configuration, launchMode,
devServicesConfig.timeout);
if (newDevService != null) {
devService = newDevService;
Map<String, String> config = devService.getConfig();
if (devService.isOwner()) {
log.info("Dev Services for RabbitMQ started.");
log.infof("Other Quarkus applications in dev mode will find the "
+ "broker automatically. For Quarkus applications in production mode, you can connect to"
+ " this by starting your application with -Drabbitmq-host=%s -Drabbitmq-port=%s -Drabbitmq-username=%s -Drabbitmq-password=%s",
config.get(RABBITMQ_HOST_PROP), config.get(RABBITMQ_PORT_PROP),
config.get(RABBITMQ_USERNAME_PROP), config.get(RABBITMQ_PASSWORD_PROP));
}
}
if (devService == null) {
compressor.closeAndDumpCaptured();
} else {
compressor.close();
}
} catch (Throwable t) {
compressor.closeAndDumpCaptured();
throw new RuntimeException(t);
}
if (devService == null) {
return null;
}
// Configure the watch dog
if (first) {
first = false;
Runnable closeTask = () -> {
if (devService != null) {
shutdownBroker();
log.info("Dev Services for RabbitMQ shut down.");
}
first = true;
devService = null;
cfg = null;
};
QuarkusClassLoader cl = (QuarkusClassLoader) Thread.currentThread().getContextClassLoader();
((QuarkusClassLoader) cl.parent()).addCloseTask(closeTask);
}
cfg = configuration;
return devService.toBuildItem();
}
private void shutdownBroker() {
if (devService != null) {
try {
devService.close();
} catch (Throwable e) {
log.error("Failed to stop the RabbitMQ broker", e);
} finally {
devService = null;
}
}
}
private RunningDevService startRabbitMQBroker(DockerStatusBuildItem dockerStatusBuildItem,
RabbitMQDevServiceCfg config, LaunchModeBuildItem launchMode,
Optional<Duration> timeout) {
if (!config.devServicesEnabled) {
// explicitly disabled
log.debug("Not starting Dev Services for RabbitMQ, as it has been disabled in the config.");
return null;
}
// Check if rabbitmq-port or rabbitmq-host are set
if (ConfigUtils.isPropertyPresent(RABBITMQ_HOST_PROP) || ConfigUtils.isPropertyPresent(RABBITMQ_PORT_PROP)) {
log.debug("Not starting Dev Services for RabbitMQ, the rabbitmq-host and/or rabbitmq-port are configured.");
return null;
}
// Verify that we have RabbitMQ channels without host and port
if (!hasRabbitMQChannelWithoutHostAndPort()) {
log.debug("Not starting Dev Services for RabbitMQ, all the channels are configured.");
return null;
}
if (!dockerStatusBuildItem.isDockerAvailable()) {
log.warn("Docker isn't working, please configure the RabbitMQ broker location.");
return null;
}
ConfiguredRabbitMQContainer container = new ConfiguredRabbitMQContainer(
DockerImageName.parse(config.imageName),
config.fixedExposedPort,
config.fixedExposedHttpPort,
launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT ? config.serviceName : null);
config.exchanges.forEach(x -> container.withExchange(x.name, x.type, x.autoDelete, false, x.durable, x.arguments));
config.queues.forEach(x -> container.withQueue(x.name, x.autoDelete, x.durable, x.arguments));
config.bindings
.forEach(b -> container.withBinding(b.source, b.destination, b.arguments, b.routingKey, b.destinationType));
final Supplier<RunningDevService> defaultRabbitMQBrokerSupplier = () -> {
// Starting the broker
timeout.ifPresent(container::withStartupTimeout);
container.start();
return getRunningDevService(container.getContainerId(), container::close, container.getHost(),
container.getPort(), container.getHttpPort(), container.getAdminUsername(), container.getAdminPassword());
};
Integer httpPort = rabbitmqContainerLocator
.locatePublicPort(config.serviceName, config.shared, launchMode.getLaunchMode(), RABBITMQ_HTTP_PORT)
.orElse(0);
return rabbitmqContainerLocator.locateContainer(config.serviceName, config.shared, launchMode.getLaunchMode())
.map(containerAddress -> getRunningDevService(containerAddress.getId(), null, containerAddress.getHost(),
containerAddress.getPort(), httpPort, container.getAdminUsername(),
container.getAdminPassword()))
.orElseGet(defaultRabbitMQBrokerSupplier);
}
private RunningDevService getRunningDevService(String containerId, Closeable closeable, String host, int port, int httpPort,
String username, String password) {
Map<String, String> configMap = new HashMap<>();
configMap.put(RABBITMQ_HOST_PROP, host);
configMap.put(RABBITMQ_PORT_PROP, String.valueOf(port));
configMap.put(RABBITMQ_HTTP_PORT_PROP, String.valueOf(httpPort));
configMap.put(RABBITMQ_USERNAME_PROP, username);
configMap.put(RABBITMQ_PASSWORD_PROP, password);
return new RunningDevService(Feature.SMALLRYE_REACTIVE_MESSAGING_RABBITMQ.getName(),
containerId, closeable, configMap);
}
private boolean hasRabbitMQChannelWithoutHostAndPort() {
Config config = ConfigProvider.getConfig();
for (String name : config.getPropertyNames()) {
boolean isIncoming = name.startsWith("mp.messaging.incoming.");
boolean isOutgoing = name.startsWith("mp.messaging.outgoing.");
boolean isConnector = name.endsWith(".connector");
boolean isConfigured = false;
if ((isIncoming || isOutgoing) && isConnector) {
String connectorValue = config.getValue(name, String.class);
boolean isRabbitMQ = connectorValue.equalsIgnoreCase("smallrye-rabbitmq");
boolean hasHost = ConfigUtils.isPropertyPresent(name.replace(".connector", ".host"));
boolean hasPort = ConfigUtils.isPropertyPresent(name.replace(".connector", ".port"));
isConfigured = isRabbitMQ && (hasHost || hasPort);
}
if (!isConfigured) {
return true;
}
}
return false;
}
private RabbitMQDevServiceCfg getConfiguration(RabbitMQBuildTimeConfig cfg) {
RabbitMQDevServicesBuildTimeConfig devServicesConfig = cfg.devservices;
return new RabbitMQDevServiceCfg(devServicesConfig);
}
private static final class RabbitMQDevServiceCfg {
static class Exchange {
String name;
String type;
Boolean autoDelete;
Boolean durable;
Map<String, Object> arguments;
Exchange(Map.Entry<String, RabbitMQDevServicesBuildTimeConfig.Exchange> entry) {
this(entry.getKey(), entry.getValue());
}
Exchange(String name, RabbitMQDevServicesBuildTimeConfig.Exchange source) {
this.name = name;
this.type = source.type;
this.autoDelete = source.autoDelete;
this.durable = source.durable;
this.arguments = source.arguments != null
? source.arguments.entrySet().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue))
: Map.of();
}
}
static class Queue {
String name;
Boolean autoDelete;
Boolean durable;
Map<String, Object> arguments;
Queue(Map.Entry<String, RabbitMQDevServicesBuildTimeConfig.Queue> entry) {
this(entry.getKey(), entry.getValue());
}
Queue(String name, RabbitMQDevServicesBuildTimeConfig.Queue source) {
this.name = name;
this.autoDelete = source.autoDelete;
this.durable = source.durable;
this.arguments = source.arguments != null
? source.arguments.entrySet().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue))
: Map.of();
}
}
static class Binding {
String source;
String destination;
String routingKey;
String destinationType;
Map<String, Object> arguments;
Binding(Map.Entry<String, RabbitMQDevServicesBuildTimeConfig.Binding> entry) {
this(entry.getKey(), entry.getValue());
}
Binding(String name, RabbitMQDevServicesBuildTimeConfig.Binding source) {
this.source = source.source.orElse(name);
this.routingKey = source.routingKey;
this.destination = source.destination.orElse(name);
this.destinationType = source.destinationType;
this.arguments = source.arguments != null
? source.arguments.entrySet().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue))
: Map.of();
}
}
private final boolean devServicesEnabled;
private final String imageName;
private final Integer fixedExposedPort;
private final Integer fixedExposedHttpPort;
private final boolean shared;
private final String serviceName;
private final List<Exchange> exchanges;
private final List<Queue> queues;
private final List<Binding> bindings;
public RabbitMQDevServiceCfg(RabbitMQDevServicesBuildTimeConfig devServicesConfig) {
this.devServicesEnabled = devServicesConfig.enabled.orElse(true);
this.imageName = devServicesConfig.imageName;
this.fixedExposedPort = devServicesConfig.port.orElse(0);
this.fixedExposedHttpPort = devServicesConfig.httpPort.orElse(0);
this.shared = devServicesConfig.shared;
this.serviceName = devServicesConfig.serviceName;
this.exchanges = devServicesConfig.exchanges != null
? devServicesConfig.exchanges.entrySet().stream().map(Exchange::new).collect(Collectors.toList())
: Collections.emptyList();
this.queues = devServicesConfig.queues != null
? devServicesConfig.queues.entrySet().stream().map(Queue::new).collect(Collectors.toList())
: Collections.emptyList();
this.bindings = devServicesConfig.bindings != null
? devServicesConfig.bindings.entrySet().stream().map(Binding::new).collect(Collectors.toList())
: Collections.emptyList();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RabbitMQDevServiceCfg that = (RabbitMQDevServiceCfg) o;
return devServicesEnabled == that.devServicesEnabled && Objects.equals(imageName, that.imageName)
&& Objects.equals(fixedExposedPort, that.fixedExposedPort);
}
@Override
public int hashCode() {
return Objects.hash(devServicesEnabled, imageName, fixedExposedPort);
}
}
/**
* Container configuring and starting the Artemis broker.
*/
private static final class ConfiguredRabbitMQContainer extends RabbitMQContainer {
private final int port;
private final int httpPort;
private ConfiguredRabbitMQContainer(DockerImageName dockerImageName, int fixedExposedPort, int fixedExposedHttpPort,
String serviceName) {
super(dockerImageName);
this.port = fixedExposedPort;
this.httpPort = fixedExposedHttpPort;
withNetwork(Network.SHARED);
withExposedPorts(RABBITMQ_PORT, RABBITMQ_HTTP_PORT);
if (serviceName != null) { // Only adds the label in dev mode.
withLabel(DEV_SERVICE_LABEL, serviceName);
}
if (!dockerImageName.getRepository().endsWith("rabbitmq")) {
throw new IllegalArgumentException("Only official rabbitmq images are supported");
}
}
@Override
protected void configure() {
super.configure();
if (port > 0) {
addFixedExposedPort(port, RABBITMQ_PORT);
}
if (httpPort > 0) {
addFixedExposedPort(httpPort, RABBITMQ_HTTP_PORT);
}
}
public int getPort() {
return getMappedPort(RABBITMQ_PORT);
}
}
}