Skip to content

Commit f2ebdee

Browse files
committed
code improvements
1 parent e3b0e08 commit f2ebdee

File tree

15 files changed

+326
-361
lines changed

15 files changed

+326
-361
lines changed

hivemq-edge/src/main/java/com/hivemq/HiveMQEdgeMain.java

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,32 @@
2121
import com.hivemq.bootstrap.LoggingBootstrap;
2222
import com.hivemq.bootstrap.ioc.Injector;
2323
import com.hivemq.bootstrap.ioc.Persistences;
24-
import com.hivemq.bootstrap.services.AfterHiveMQStartBootstrapService;
2524
import com.hivemq.bootstrap.services.AfterHiveMQStartBootstrapServiceImpl;
2625
import com.hivemq.common.shutdown.ShutdownHooks;
2726
import com.hivemq.configuration.info.SystemInformation;
2827
import com.hivemq.configuration.info.SystemInformationImpl;
29-
import com.hivemq.configuration.service.ApiConfigurationService;
3028
import com.hivemq.configuration.service.ConfigurationService;
3129
import com.hivemq.edge.modules.ModuleLoader;
3230
import com.hivemq.embedded.EmbeddedExtension;
3331
import com.hivemq.exceptions.HiveMQEdgeStartupException;
32+
import com.hivemq.http.JaxrsHttpServer;
3433
import org.jetbrains.annotations.NotNull;
3534
import org.jetbrains.annotations.Nullable;
36-
import com.hivemq.http.JaxrsHttpServer;
3735
import org.slf4j.Logger;
3836
import org.slf4j.LoggerFactory;
3937

40-
import java.util.Objects;
4138
import java.util.concurrent.TimeUnit;
4239

40+
import static java.util.Objects.requireNonNull;
41+
4342
public class HiveMQEdgeMain {
44-
private static final Logger log = LoggerFactory.getLogger(HiveMQEdgeMain.class);
43+
private static final @NotNull Logger log = LoggerFactory.getLogger(HiveMQEdgeMain.class);
4544

46-
private @Nullable ConfigurationService configService;
4745
private final @NotNull ModuleLoader moduleLoader;
4846
private final @NotNull MetricRegistry metricRegistry;
4947
private final @NotNull SystemInformation systemInformation;
50-
48+
private @Nullable ConfigurationService configService;
5149
private @Nullable JaxrsHttpServer jaxrsServer;
52-
5350
private @Nullable Injector injector;
5451
private @Nullable Thread shutdownThread;
5552

@@ -64,22 +61,30 @@ public HiveMQEdgeMain(
6461
this.moduleLoader = moduleLoader;
6562
}
6663

67-
public void bootstrap() throws HiveMQEdgeStartupException {
68-
// Already bootstrapped.
69-
if (injector != null) {
70-
return;
64+
public static void main(final String @NotNull [] args) throws Exception {
65+
log.info("Starting HiveMQ Edge...");
66+
final long startTime = System.nanoTime();
67+
final SystemInformationImpl systemInformation = new SystemInformationImpl(true);
68+
final ModuleLoader moduleLoader = new ModuleLoader(systemInformation);
69+
final HiveMQEdgeMain server = new HiveMQEdgeMain(systemInformation, new MetricRegistry(), null, moduleLoader);
70+
try {
71+
server.start(null);
72+
log.info("Started HiveMQ Edge in {}ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
73+
} catch (final HiveMQEdgeStartupException e) {
74+
log.error("HiveMQ Edge start was aborted with error.", e);
7175
}
72-
final HiveMQEdgeBootstrap bootstrap =
73-
new HiveMQEdgeBootstrap(metricRegistry, systemInformation, moduleLoader, configService);
74-
76+
}
7577

76-
injector = bootstrap.bootstrap();
77-
if (configService == null) {
78-
configService = injector.configurationService();
78+
public void bootstrap() throws HiveMQEdgeStartupException {
79+
if (injector == null) {
80+
injector =
81+
new HiveMQEdgeBootstrap(metricRegistry, systemInformation, moduleLoader, configService).bootstrap();
82+
if (configService == null) {
83+
configService = injector.configurationService();
84+
}
7985
}
8086
}
8187

82-
8388
protected void startGateway(final @Nullable EmbeddedExtension embeddedExtension) throws HiveMQEdgeStartupException {
8489
if (injector == null) {
8590
throw new HiveMQEdgeStartupException("invalid startup state");
@@ -90,9 +95,7 @@ protected void startGateway(final @Nullable EmbeddedExtension embeddedExtension)
9095
throw new HiveMQEdgeStartupException("User aborted.");
9196
}
9297

93-
final HiveMQEdgeGateway instance = injector.edgeGateway();
94-
instance.start(embeddedExtension);
95-
98+
injector.edgeGateway().start(embeddedExtension);
9699
initializeApiServer(injector);
97100
startApiServer();
98101
}
@@ -102,25 +105,21 @@ protected void stopGateway() {
102105
return;
103106
}
104107
final ShutdownHooks shutdownHooks = injector.shutdownHooks();
105-
// Already shutdown.
106108
if (shutdownHooks.isShuttingDown()) {
107109
return;
108110
}
109-
110111
shutdownHooks.runShutdownHooks();
111112

112113
//clear metrics
113114
metricRegistry.removeMatching(MetricFilter.ALL);
114115

115116
//Stop the API Webserver
116117
stopApiServer();
117-
118118
LoggingBootstrap.resetLogging();
119119
}
120120

121121
protected void initializeApiServer(final @NotNull Injector injector) {
122-
final ApiConfigurationService config = Objects.requireNonNull(configService).apiConfiguration();
123-
if (jaxrsServer == null && config.isEnabled()) {
122+
if (jaxrsServer == null && requireNonNull(configService).apiConfiguration().isEnabled()) {
124123
jaxrsServer = injector.apiServer();
125124
} else {
126125
log.info("API is DISABLED by configuration");
@@ -142,21 +141,18 @@ protected void stopApiServer() {
142141

143142
protected void afterStart() {
144143
afterHiveMQStartBootstrap();
145-
//hook method
146144
}
147145

148146
private void afterHiveMQStartBootstrap() {
149147
Preconditions.checkNotNull(injector);
150148
final Persistences persistences = injector.persistences();
151149
Preconditions.checkNotNull(persistences);
152150
Preconditions.checkNotNull(configService);
153-
154151
try {
155-
final AfterHiveMQStartBootstrapService afterHiveMQStartBootstrapService =
156-
AfterHiveMQStartBootstrapServiceImpl.decorate(injector.completeBootstrapService(),
152+
injector.commercialModuleLoaderDiscovery()
153+
.afterHiveMQStart(AfterHiveMQStartBootstrapServiceImpl.decorate(injector.completeBootstrapService(),
157154
injector.protocolAdapterManager(),
158-
injector.services().modulesAndExtensionsService());
159-
injector.commercialModuleLoaderDiscovery().afterHiveMQStart(afterHiveMQStartBootstrapService);
155+
injector.services().modulesAndExtensionsService()));
160156
} catch (final Exception e) {
161157
log.warn("Error on bootstrapping modules:", e);
162158
throw new HiveMQEdgeStartupException(e);
@@ -183,24 +179,7 @@ public void stop() {
183179
}
184180
}
185181

186-
public static void main(final String @NotNull [] args) throws Exception {
187-
log.info("Starting HiveMQ Edge...");
188-
final long startTime = System.nanoTime();
189-
final SystemInformationImpl systemInformation = new SystemInformationImpl(true);
190-
final ModuleLoader moduleLoader = new ModuleLoader(systemInformation);
191-
final HiveMQEdgeMain server =
192-
new HiveMQEdgeMain(systemInformation, new MetricRegistry(), null, moduleLoader);
193-
try {
194-
server.start(null);
195-
log.info("Started HiveMQ Edge in {}ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
196-
} catch (final HiveMQEdgeStartupException e) {
197-
log.error("HiveMQ Edge start was aborted with error.", e);
198-
}
199-
}
200-
201182
public @Nullable Injector getInjector() {
202183
return injector;
203184
}
204-
205-
206185
}

hivemq-edge/src/main/java/com/hivemq/api/resources/impl/ProtocolAdaptersResourceImpl.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -808,10 +808,7 @@ public int getDepth() {
808808
cfg.getSouthboundMappings(),
809809
cfg.getTags()))
810810
.map(newCfg -> {
811-
// Enable skip flag to prevent refresh() from restarting adapter
812-
// The flag will be cleared by refresh() when it checks it
813-
ProtocolAdapterManager.enableSkipNextRefresh();
814-
if (!configExtractor.updateAdapter(newCfg)) {
811+
if (!configExtractor.updateAdapter(newCfg, false)) {
815812
return adapterCannotBeUpdatedError(adapterId);
816813
}
817814
log.info("Successfully updated northbound mappings for adapter '{}' via hot-reload.",
@@ -858,10 +855,7 @@ public int getDepth() {
858855
converted,
859856
cfg.getTags()))
860857
.map(newCfg -> {
861-
// Enable skip flag to prevent refresh() from restarting adapter
862-
// The flag will be cleared by refresh() when it checks it
863-
ProtocolAdapterManager.enableSkipNextRefresh();
864-
if (!configExtractor.updateAdapter(newCfg)) {
858+
if (!configExtractor.updateAdapter(newCfg, false)) {
865859
return adapterCannotBeUpdatedError(adapterId);
866860
}
867861
log.info("Successfully updated southbound mappings for adapter '{}' via hot-reload.",

hivemq-edge/src/main/java/com/hivemq/bootstrap/ioc/Injector.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,4 @@ interface Builder {
177177

178178
Injector build();
179179
}
180-
181180
}

0 commit comments

Comments
 (0)