/
Application.java
227 lines (192 loc) · 8.13 KB
/
Application.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
/**
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hono.adapter.http.quarkus;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.hono.adapter.client.registry.DeviceRegistrationClient;
import org.eclipse.hono.adapter.client.registry.TenantClient;
import org.eclipse.hono.adapter.client.registry.amqp.ProtonBasedDeviceRegistrationClient;
import org.eclipse.hono.adapter.client.registry.amqp.ProtonBasedTenantClient;
import org.eclipse.hono.adapter.client.telemetry.amqp.ProtonBasedDownstreamSender;
import org.eclipse.hono.adapter.http.MicrometerBasedHttpAdapterMetrics;
import org.eclipse.hono.adapter.http.impl.VertxBasedHttpProtocolAdapter;
import org.eclipse.hono.cache.CacheProvider;
import org.eclipse.hono.cache.ExpiringValueCache;
import org.eclipse.hono.client.CommandTargetMapper;
import org.eclipse.hono.client.CredentialsClientFactory;
import org.eclipse.hono.client.DeviceConnectionClientFactory;
import org.eclipse.hono.client.HonoConnection;
import org.eclipse.hono.client.ProtocolAdapterCommandConsumerFactory;
import org.eclipse.hono.service.HealthCheckServer;
import org.eclipse.hono.service.VertxBasedHealthCheckServer;
import org.eclipse.hono.service.quarkus.CaffeineBasedExpiringValueCache;
import org.eclipse.hono.service.resourcelimits.ResourceLimitChecks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.opentracing.Tracer;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.Startup;
import io.quarkus.runtime.StartupEvent;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
/**
* The Hono HTTP adapter main application class.
*/
@ApplicationScoped
@Startup
public class Application {
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
@Inject
Vertx vertx;
@Inject
HonoConfig config;
@Inject
Tracer tracer;
@Inject
MicrometerBasedHttpAdapterMetrics metrics;
@Inject
List<Handler<Router>> healthCheckResourceProviders;
@Inject
ResourceLimitChecks resourceLimitChecks;
void onStart(final @Observes StartupEvent ev) {
LOG.info("deploying {} HTTP adapter instances ...", config.app.getMaxInstances());
final CompletableFuture<Void> startup = new CompletableFuture<>();
final Promise<String> deploymentTracker = Promise.promise();
vertx.deployVerticle(
() -> adapter(),
new DeploymentOptions().setInstances(config.app.getMaxInstances()),
deploymentTracker);
deploymentTracker.future()
.compose(s -> healthCheckServer().start())
.onSuccess(ok -> startup.complete(null))
.onFailure(t -> startup.completeExceptionally(t));
startup.join();
}
void onStop(final @Observes ShutdownEvent ev) {
LOG.info("shutting down HTTP adapter");
final CompletableFuture<Void> shutdown = new CompletableFuture<>();
healthCheckServer().stop()
.onComplete(ok -> {
vertx.close(attempt -> {
if (attempt.succeeded()) {
shutdown.complete(null);
} else {
shutdown.completeExceptionally(attempt.cause());
}
});
});
shutdown.join();
}
@Produces
VertxBasedHttpProtocolAdapter adapter() {
final VertxBasedHttpProtocolAdapter adapter = new VertxBasedHttpProtocolAdapter();
adapter.setCommandConsumerFactory(commandConsumerFactory());
adapter.setCommandTargetMapper(commandTargetMapper());
adapter.setConfig(config.http);
adapter.setCredentialsClientFactory(credentialsClientFactory());
adapter.setDeviceConnectionClientFactory(deviceConnectionClientFactory());
adapter.setEventSender(newDownstreamSender());
adapter.setHealthCheckServer(healthCheckServer());
adapter.setMetrics(metrics);
adapter.setRegistrationClient(registrationClient());
adapter.setTelemetrySender(newDownstreamSender());
adapter.setTenantClient(tenantClient());
adapter.setTracer(tracer);
adapter.setResourceLimitChecks(resourceLimitChecks);
return adapter;
}
private ProtonBasedDownstreamSender newDownstreamSender() {
return new ProtonBasedDownstreamSender(
HonoConnection.newConnection(vertx, config.messaging),
metrics,
config.http);
}
@Produces
HonoConnection commandConsumerConnection() {
return HonoConnection.newConnection(vertx, config.command);
}
@Produces
ProtocolAdapterCommandConsumerFactory commandConsumerFactory() {
return ProtocolAdapterCommandConsumerFactory.create(commandConsumerConnection());
}
@Produces
CommandTargetMapper commandTargetMapper() {
return CommandTargetMapper.create(tracer);
}
@Produces
CredentialsClientFactory credentialsClientFactory() {
return CredentialsClientFactory.create(
HonoConnection.newConnection(vertx, config.credentials),
newCaffeineCache(config.credentials.getResponseCacheMinSize(), config.credentials.getResponseCacheMaxSize()),
metrics);
}
@Produces
DeviceConnectionClientFactory deviceConnectionClientFactory() {
return DeviceConnectionClientFactory.create(HonoConnection.newConnection(vertx, config.deviceConnection), metrics);
}
@Singleton
HealthCheckServer healthCheckServer() {
final VertxBasedHealthCheckServer server = new VertxBasedHealthCheckServer(vertx, config.healthCheck);
server.setAdditionalResources(healthCheckResourceProviders);
return server;
}
@Produces
DeviceRegistrationClient registrationClient() {
return new ProtonBasedDeviceRegistrationClient(
HonoConnection.newConnection(vertx, config.registration),
metrics,
config.http,
newCaffeineCache(config.registration.getResponseCacheMinSize(), config.registration.getResponseCacheMaxSize()));
}
@Produces
TenantClient tenantClient() {
return new ProtonBasedTenantClient(
HonoConnection.newConnection(vertx, config.tenant),
metrics,
config.http,
newCaffeineCache(config.tenant.getResponseCacheMinSize(), config.tenant.getResponseCacheMaxSize()));
}
/**
* Create a new cache provider based on Caffeine.
*
* @param minCacheSize The minimum size of the cache.
* @param maxCacheSize the maximum size of the cache.
* @return A new cache provider or {@code null} if no cache should be used.
*/
private static CacheProvider newCaffeineCache(final int minCacheSize, final long maxCacheSize) {
if (maxCacheSize <= 0) {
return null;
}
final Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
.initialCapacity(minCacheSize)
.maximumSize(Math.max(minCacheSize, maxCacheSize));
return new CacheProvider() {
private final Map<String, ExpiringValueCache<Object, Object>> caches = new HashMap<>();
@Override
public ExpiringValueCache<Object, Object> getCache(final String cacheName) {
return caches.computeIfAbsent(cacheName, name -> new CaffeineBasedExpiringValueCache<>(caffeine.build()));
}
};
}
}