From 1d7999e2618a753f72cc077d600f5517c6a37e6b Mon Sep 17 00:00:00 2001 From: Cousjava Date: Tue, 20 Jun 2017 21:03:05 +0100 Subject: [PATCH] PAYARA-1561 Payara Micro fails fast if there is an error in boot (#1690) * Payara-micro now fails fast * Added copyright --- .../java/fish/payara/micro/PayaraMicro.java | 8 ++- .../micro/boot/runtime/MicroGlassFish.java | 1 + .../payara/micro/impl/PayaraMicroImpl.java | 7 ++- .../v3/server/AppServerStartup.java | 38 ++++++++++--- .../notification/NotificationEventBus.java | 56 +++++++++++++++---- 5 files changed, 87 insertions(+), 23 deletions(-) diff --git a/appserver/extras/payara-micro/payara-micro-boot/src/main/java/fish/payara/micro/PayaraMicro.java b/appserver/extras/payara-micro/payara-micro-boot/src/main/java/fish/payara/micro/PayaraMicro.java index 8e77cdf8c94..bf9e419cfa7 100644 --- a/appserver/extras/payara-micro/payara-micro-boot/src/main/java/fish/payara/micro/PayaraMicro.java +++ b/appserver/extras/payara-micro/payara-micro-boot/src/main/java/fish/payara/micro/PayaraMicro.java @@ -354,8 +354,12 @@ public ClassLoader setThreadBootstrapLoader() { return result; } - public static void main(String ... args) throws Exception { - PayaraMicroLauncher.main(args); + public static void main(String ... args) { + try { + PayaraMicroLauncher.main(args); + } catch (Exception ex) { + Logger.getLogger(PayaraMicro.class.getName()).log(Level.SEVERE, null, ex); + } } private PayaraMicro() { diff --git a/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/boot/runtime/MicroGlassFish.java b/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/boot/runtime/MicroGlassFish.java index 350ef3dad25..76b2c48c6e5 100644 --- a/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/boot/runtime/MicroGlassFish.java +++ b/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/boot/runtime/MicroGlassFish.java @@ -77,6 +77,7 @@ public void stop() throws GlassFishException { status = Status.STOPPING; kernel.stop(); + habitat.shutdown(); status = Status.STOPPED; } diff --git a/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java b/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java index 290c72eeb7e..d4a5d0d49b7 100644 --- a/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java +++ b/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java @@ -990,7 +990,12 @@ public PayaraMicroRuntime bootStrap() throws BootstrapException { long end = System.currentTimeMillis(); dumpFinalStatus(end - start); return runtime; - } catch (GlassFishException ex) { + } catch (Exception ex) { + try { + gf.dispose(); + } catch (GlassFishException ex1) { + Logger.getLogger(PayaraMicroImpl.class.getName()).log(Level.SEVERE, null, ex1); + } throw new BootstrapException(ex.getMessage(), ex); } } diff --git a/nucleus/core/kernel/src/main/java/com/sun/enterprise/v3/server/AppServerStartup.java b/nucleus/core/kernel/src/main/java/com/sun/enterprise/v3/server/AppServerStartup.java index 54da33b625d..6481aedd26a 100644 --- a/nucleus/core/kernel/src/main/java/com/sun/enterprise/v3/server/AppServerStartup.java +++ b/nucleus/core/kernel/src/main/java/com/sun/enterprise/v3/server/AppServerStartup.java @@ -36,6 +36,8 @@ * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. + * + * Portions Copyright [2017] Payara Foundation and/or affiliates */ package com.sun.enterprise.v3.server; @@ -73,6 +75,7 @@ import org.glassfish.api.event.EventTypes; import org.glassfish.api.event.Events; import org.glassfish.common.util.Constants; +import org.glassfish.embeddable.GlassFishException; import org.glassfish.hk2.api.ActiveDescriptor; import org.glassfish.hk2.api.Descriptor; import org.glassfish.hk2.api.DynamicConfiguration; @@ -210,6 +213,7 @@ else if (POLICY_USE_NO_THREADS.equals(threadPolicy)) { } + @Override public synchronized void start() { ClassLoader origCL = Thread.currentThread().getContextClassLoader(); try { @@ -217,16 +221,24 @@ public synchronized void start() { Thread.currentThread().setContextClassLoader( commonCLS.getCommonClassLoader()); doStart(); + } catch (GlassFishException ex) { + throw new RuntimeException (ex); } finally { // reset the context classloader. See issue GLASSFISH-15775 Thread.currentThread().setContextClassLoader(origCL); } } - private void doStart() { + private void doStart() throws GlassFishException { - run(); + if (!run()){ + //startup failed + logger.log(Level.SEVERE, "Failed to start, exiting"); + throw new GlassFishException("Server failed to start"); + } + //if (appInstanceListener.) + final CountDownLatch latch = new CountDownLatch(1); // spwan a non-daemon thread that waits indefinitely for shutdown request. @@ -272,11 +284,16 @@ public void run() { } } - public void run() { + /** + * + * @return True is startup succeeded, false if an error occurred preventing + * the server from starting + */ + public boolean run() { if (context==null) { System.err.println("Startup context not provided, cannot continue"); - return; + return false; } if (platform==null) { @@ -314,7 +331,7 @@ public void run() { if (!proceedTo(InitRunLevel.VAL)) { appInstanceListener.stopRecordingTimes(); - return; + return false; } if (!logger.isLoggable(level)) { @@ -330,12 +347,12 @@ public void run() { appInstanceListener.startRecordingFutures(); if (!proceedTo(StartupRunLevel.VAL)) { appInstanceListener.stopRecordingTimes(); - return; + return false; } if (!postStartupJob()) { appInstanceListener.stopRecordingTimes(); - return; + return false; } if (logger.isLoggable(level)) { @@ -347,7 +364,7 @@ public void run() { if (!proceedTo(PostStartupRunLevel.VAL)) { appInstanceListener.stopRecordingTimes(); - return; + return false; } if (logger.isLoggable(level)) { @@ -356,8 +373,13 @@ public void run() { logger.log(level, "PostStartup level done in " + (postStartupFinishTime - startupFinishTime) + " ms"); } + return true; } + /** + * + * @return True if started successfully, false otherwise + */ private boolean postStartupJob() { LinkedList>> futures = appInstanceListener.getFutures(); diff --git a/nucleus/payara-modules/notification-core/src/main/java/fish/payara/nucleus/notification/NotificationEventBus.java b/nucleus/payara-modules/notification-core/src/main/java/fish/payara/nucleus/notification/NotificationEventBus.java index 91cf2d3a248..b300ff55cf1 100644 --- a/nucleus/payara-modules/notification-core/src/main/java/fish/payara/nucleus/notification/NotificationEventBus.java +++ b/nucleus/payara-modules/notification-core/src/main/java/fish/payara/nucleus/notification/NotificationEventBus.java @@ -1,21 +1,49 @@ /* - DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - Copyright (c) 2016 Payara Foundation. All rights reserved. - The contents of this file are subject to the terms of the Common Development - and Distribution License("CDDL") (collectively, the "License"). You - may not use this file except in compliance with the License. You can - obtain a copy of the License at - https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - or packager/legal/LICENSE.txt. See the License for the specific - language governing permissions and limitations under the License. - When distributing the software, include this License Header Notice in each - file and include the License file at packager/legal/LICENSE.txt. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2016-2017 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/master/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. */ package fish.payara.nucleus.notification; import com.google.common.eventbus.EventBus; import fish.payara.nucleus.notification.domain.NotificationEvent; import fish.payara.nucleus.notification.service.BaseNotifierService; +import java.util.logging.Level; +import java.util.logging.Logger; import org.glassfish.api.StartupRunLevel; import org.glassfish.hk2.runlevel.RunLevel; import org.jvnet.hk2.annotations.Service; @@ -37,7 +65,11 @@ public void register(BaseNotifierService notifier) { } public void unregister(BaseNotifierService notifier) { - eventBus.unregister(notifier); + try { + eventBus.unregister(notifier); + } catch (IllegalArgumentException e){ + Logger.getLogger(NotificationEventBus.class.getCanonicalName()).log(Level.WARNING, "Tried to unregister" + notifier.toString() + ", it may not have been previously registered"); + } } void postEvent(NotificationEvent event) {