2121import com .hivemq .bootstrap .LoggingBootstrap ;
2222import com .hivemq .bootstrap .ioc .Injector ;
2323import com .hivemq .bootstrap .ioc .Persistences ;
24- import com .hivemq .bootstrap .services .AfterHiveMQStartBootstrapService ;
2524import com .hivemq .bootstrap .services .AfterHiveMQStartBootstrapServiceImpl ;
2625import com .hivemq .common .shutdown .ShutdownHooks ;
2726import com .hivemq .configuration .info .SystemInformation ;
2827import com .hivemq .configuration .info .SystemInformationImpl ;
29- import com .hivemq .configuration .service .ApiConfigurationService ;
3028import com .hivemq .configuration .service .ConfigurationService ;
3129import com .hivemq .edge .modules .ModuleLoader ;
3230import com .hivemq .embedded .EmbeddedExtension ;
3331import com .hivemq .exceptions .HiveMQEdgeStartupException ;
32+ import com .hivemq .http .JaxrsHttpServer ;
3433import org .jetbrains .annotations .NotNull ;
3534import org .jetbrains .annotations .Nullable ;
36- import com .hivemq .http .JaxrsHttpServer ;
3735import org .slf4j .Logger ;
3836import org .slf4j .LoggerFactory ;
3937
40- import java .util .Objects ;
4138import java .util .concurrent .TimeUnit ;
4239
40+ import static java .util .Objects .requireNonNull ;
41+
4342public 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}
0 commit comments