-
Notifications
You must be signed in to change notification settings - Fork 219
/
RabbitMQConsumerActor.java
202 lines (177 loc) · 9.29 KB
/
RabbitMQConsumerActor.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.connectivity.messaging.rabbitmq;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
import org.eclipse.ditto.model.base.headers.DittoHeaderDefinition;
import org.eclipse.ditto.model.connectivity.AddressMetric;
import org.eclipse.ditto.model.connectivity.ConnectionStatus;
import org.eclipse.ditto.model.connectivity.ConnectivityModelFactory;
import org.eclipse.ditto.model.connectivity.Enforcement;
import org.eclipse.ditto.services.connectivity.mapping.MessageMappers;
import org.eclipse.ditto.services.connectivity.messaging.internal.RetrieveAddressMetric;
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.eclipse.ditto.services.models.connectivity.ExternalMessageBuilder;
import org.eclipse.ditto.services.models.connectivity.ExternalMessageFactory;
import org.eclipse.ditto.services.models.connectivity.placeholder.EnforcementFactoryFactory;
import org.eclipse.ditto.services.models.connectivity.placeholder.EnforcementFilterFactory;
import org.eclipse.ditto.services.models.connectivity.placeholder.PlaceholderFactory;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.Delivery;
import com.rabbitmq.client.Envelope;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.Creator;
import akka.japi.pf.ReceiveBuilder;
/**
* Actor which receives message from an RabbitMQ source and forwards them to a {@code MessageMappingProcessorActor}.
*/
public final class RabbitMQConsumerActor extends AbstractActor {
private static final String MESSAGE_ID_HEADER = "messageId";
private static final Set<String> CONTENT_TYPES_INTERPRETED_AS_TEXT = Collections.unmodifiableSet(new HashSet<>(
Arrays.asList(
"text/plain",
"text/html",
"text/yaml",
"application/json",
"application/xml"
)));
private final DiagnosticLoggingAdapter log = LogUtil.obtain(this);
private final String sourceAddress;
private final ActorRef messageMappingProcessor;
private final AuthorizationContext authorizationContext;
private final EnforcementFilterFactory<Map<String, String>, String> headerEnforcementFilterFactory;
private long consumedMessages = 0L;
private Instant lastMessageConsumedAt;
@Nullable private AddressMetric addressMetric = null;
private RabbitMQConsumerActor(final String sourceAddress, final ActorRef messageMappingProcessor, final
AuthorizationContext authorizationContext, @Nullable Enforcement enforcement) {
this.sourceAddress = checkNotNull(sourceAddress, "source");
this.messageMappingProcessor = checkNotNull(messageMappingProcessor, "messageMappingProcessor");
this.authorizationContext = authorizationContext;
headerEnforcementFilterFactory =
enforcement != null ? EnforcementFactoryFactory.newEnforcementFilterFactory(enforcement,
PlaceholderFactory.newHeadersPlaceholder()) : input -> null;
}
/**
* Creates Akka configuration object {@link Props} for this {@code RabbitMQConsumerActor}.
*
* @param source the source of messages
* @param messageMappingProcessor the message mapping processor where received messages are forwarded to
* @param authorizationContext the authorization context of this source
* @param enforcement the enforcement configuration
* @return the Akka configuration Props object.
*/
static Props props(final String source, final ActorRef messageMappingProcessor, final
AuthorizationContext authorizationContext, @Nullable final Enforcement enforcement) {
return Props.create(
RabbitMQConsumerActor.class, new Creator<RabbitMQConsumerActor>() {
private static final long serialVersionUID = 1L;
@Override
public RabbitMQConsumerActor create() {
return new RabbitMQConsumerActor(source, messageMappingProcessor, authorizationContext,
enforcement);
}
});
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(Delivery.class, this::handleDelivery)
.match(AddressMetric.class, this::handleAddressMetric)
.match(RetrieveAddressMetric.class, ram -> {
getSender().tell(ConnectivityModelFactory.newAddressMetric(
addressMetric != null ? addressMetric.getStatus() : ConnectionStatus.UNKNOWN,
addressMetric != null ? addressMetric.getStatusDetails().orElse(null) : null,
consumedMessages, lastMessageConsumedAt), getSelf());
})
.matchAny(m -> {
log.warning("Unknown message: {}", m);
unhandled(m);
}).build();
}
private void handleAddressMetric(final AddressMetric addressMetric) {
this.addressMetric = addressMetric;
}
private void handleDelivery(final Delivery delivery) {
consumedMessages++;
lastMessageConsumedAt = Instant.now();
final BasicProperties properties = delivery.getProperties();
final Envelope envelope = delivery.getEnvelope();
final byte[] body = delivery.getBody();
final String correlationId = properties.getCorrelationId();
LogUtil.enhanceLogWithCorrelationId(log, correlationId);
if (log.isDebugEnabled()) {
log.debug("Received message from RabbitMQ ({}//{}): {}", envelope, properties,
new String(body, StandardCharsets.UTF_8));
}
try {
final Map<String, String> headers = extractHeadersFromMessage(properties, envelope);
headers.put(DittoHeaderDefinition.SOURCE.getKey(), sourceAddress);
final ExternalMessageBuilder externalMessageBuilder =
ExternalMessageFactory.newExternalMessageBuilder(headers);
final String contentType = properties.getContentType();
if (shouldBeInterpretedAsText(contentType)) {
final String text = new String(body, MessageMappers.determineCharset(contentType));
externalMessageBuilder.withText(text);
} else {
externalMessageBuilder.withBytes(body);
}
externalMessageBuilder.withAuthorizationContext(authorizationContext);
externalMessageBuilder.withEnforcement(headerEnforcementFilterFactory.getFilter(headers));
final ExternalMessage externalMessage = externalMessageBuilder.build();
messageMappingProcessor.forward(externalMessage, getContext());
} catch (final Exception e) {
log.warning("Processing delivery {} failed: {}", envelope.getDeliveryTag(), e.getMessage(), e);
}
}
private static boolean shouldBeInterpretedAsText(@Nullable final String contentType) {
return contentType != null && CONTENT_TYPES_INTERPRETED_AS_TEXT.stream().anyMatch(contentType::startsWith);
}
private Map<String, String> extractHeadersFromMessage(final BasicProperties properties, final Envelope envelope) {
final Map<String, String> headersFromProperties =
Optional.ofNullable(properties.getHeaders())
.map(Map::entrySet)
.map(this::setToStringStringMap).orElseGet(HashMap::new);
// set headers specific to rmq messages
if (properties.getReplyTo() != null) {
headersFromProperties.put(ExternalMessage.REPLY_TO_HEADER, properties.getReplyTo());
}
if (properties.getCorrelationId() != null) {
headersFromProperties.put(DittoHeaderDefinition.CORRELATION_ID.getKey(), properties.getCorrelationId());
}
if (properties.getContentType() != null) {
headersFromProperties.put(ExternalMessage.CONTENT_TYPE_HEADER, properties.getContentType());
}
headersFromProperties.put(MESSAGE_ID_HEADER, Long.toString(envelope.getDeliveryTag()));
return headersFromProperties;
}
private Map<String, String> setToStringStringMap(final Set<Map.Entry<String, Object>> entries) {
return entries.stream()
.filter(entry -> Objects.nonNull(entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, entry -> String.valueOf(entry.getValue())));
}
}