@@ -1241,6 +1241,34 @@ public static void runApp(@NonNull String[] args, @NonNull Consumer<Jooby> consu
12411241 runApp (args , ExecutionMode .DEFAULT , consumerProvider (consumer ));
12421242 }
12431243
1244+ /**
1245+ * Setup default environment, logging (logback or log4j2) and run application.
1246+ *
1247+ * @param args Application arguments.
1248+ * @param server Server to run.
1249+ * @param consumer Application consumer.
1250+ */
1251+ public static void runApp (
1252+ @ NonNull String [] args , @ NonNull Server server , @ NonNull Consumer <Jooby > consumer ) {
1253+ runApp (args , server , ExecutionMode .DEFAULT , consumer );
1254+ }
1255+
1256+ /**
1257+ * Setup default environment, logging (logback or log4j2) and run application.
1258+ *
1259+ * @param args Application arguments.
1260+ * @param server Server to run.
1261+ * @param consumer Application consumer.
1262+ */
1263+ public static void runApp (
1264+ @ NonNull String [] args ,
1265+ @ NonNull Server server ,
1266+ @ NonNull ExecutionMode executionMode ,
1267+ @ NonNull Consumer <Jooby > consumer ) {
1268+ configurePackage (consumer .getClass ().getPackage ());
1269+ runApp (args , server , executionMode , List .of (consumerProvider (consumer )));
1270+ }
1271+
12441272 /**
12451273 * Setup default environment, logging (logback or log4j2) and run application.
12461274 *
@@ -1319,28 +1347,44 @@ public static void runApp(
13191347 @ NonNull Server server ,
13201348 @ NonNull ExecutionMode executionMode ,
13211349 @ NonNull List <Supplier <Jooby >> provider ) {
1350+
13221351 /* Dump command line as system properties. */
13231352 parseArguments (args ).forEach (System ::setProperty );
13241353 var apps = new ArrayList <Jooby >();
13251354 var targetServer = server .getLoggerOff ().isEmpty () ? server : MutedServer .mute (server );
1326- for (var factory : provider ) {
1327- var app = createApp (executionMode , factory );
1328- app .server = targetServer ;
1329- /*
1330- When running a single app instance, there is no issue with server options, when multiple
1331- apps set options a warning will be printed
1332- */
1333- var options = app .serverOptions ;
1334- if (options == null ) {
1335- options = ServerOptions .from (app .getConfig ()).orElse (null );
1355+ try {
1356+ for (var factory : provider ) {
1357+ var app = createApp (executionMode , factory );
1358+ app .server = targetServer ;
1359+ /*
1360+ When running a single app instance, there is no issue with server options, when multiple
1361+ apps set options a warning will be printed
1362+ */
1363+ var options = app .serverOptions ;
1364+ if (options == null ) {
1365+ options = ServerOptions .from (app .getConfig ()).orElse (null );
1366+ }
1367+ if (options != null ) {
1368+ options .setServer (server .getName ());
1369+ server .setOptions (options );
1370+ }
1371+ apps .add (app );
13361372 }
1337- if (options != null ) {
1338- options .setServer (server .getName ());
1339- server .setOptions (options );
1373+ targetServer .start (apps .toArray (new Jooby [0 ]));
1374+ } catch (Throwable t ) {
1375+ if (targetServer != null ) {
1376+ try {
1377+ targetServer .stop ();
1378+ } catch (Exception ignore ) {
1379+ }
13401380 }
1341- apps .add (app );
1381+ LoggerFactory .getLogger (Jooby .class )
1382+ .error ("Application initialization resulted in exception" , t );
1383+
1384+ throw t instanceof StartupException
1385+ ? (StartupException ) t
1386+ : new StartupException ("Application initialization resulted in exception" , t );
13421387 }
1343- targetServer .start (apps .toArray (new Jooby [0 ]));
13441388 }
13451389
13461390 /**
@@ -1366,13 +1410,6 @@ public static Jooby createApp(
13661410 try {
13671411 BOOT_EXECUTION_MODE = executionMode ;
13681412 app = provider .get ();
1369- } catch (Throwable t ) {
1370- LoggerFactory .getLogger (Jooby .class )
1371- .error ("Application initialization resulted in exception" , t );
1372-
1373- throw t instanceof StartupException
1374- ? (StartupException ) t
1375- : new StartupException ("Application initialization resulted in exception" , t );
13761413 } finally {
13771414 BOOT_EXECUTION_MODE = ExecutionMode .DEFAULT ;
13781415 }
0 commit comments