All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glowroot.agent.init.NettyWorkaround Maven / Gradle / Ivy

There is a newer version: 0.9.24
Show newest version
/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.glowroot.agent.init;

import java.lang.instrument.Instrumentation;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

import javax.annotation.Nullable;

import org.glowroot.agent.shaded.google.common.base.Stopwatch;
import org.glowroot.agent.shaded.slf4j.Logger;
import org.glowroot.agent.shaded.slf4j.LoggerFactory;

import static org.glowroot.agent.shaded.google.common.base.Preconditions.checkNotNull;
import static java.util.concurrent.TimeUnit.SECONDS;

public class NettyWorkaround {

    private static final Logger logger = LoggerFactory.getLogger(NettyWorkaround.class);

    private NettyWorkaround() {}

    public static void run(final @Nullable Instrumentation instrumentation,
            final Callable notInPremainCallable) throws Exception {
        if (instrumentation == null) {
            notInPremainCallable.call();
        } else {
            // cannot start netty in premain otherwise can crash JVM
            // see https://github.com/netty/netty/issues/3233
            // and https://bugs.openjdk.java.net/browse/JDK-8041920
            Executors.newSingleThreadExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        checkNotNull(instrumentation);
                        waitForMain(instrumentation);
                        notInPremainCallable.call();
                    } catch (Throwable t) {
                        logger.error(t.getMessage(), t);
                    }
                }
            });
        }
    }

    private static void waitForMain(Instrumentation instrumentation) throws InterruptedException {
        Stopwatch stopwatch = Stopwatch.createStarted();
        while (stopwatch.elapsed(SECONDS) < 60) {
            Thread.sleep(100);
            for (Class clazz : instrumentation.getInitiatedClasses(null)) {
                if (clazz.getName().equals("sun.misc.Launcher")) {
                    return;
                }
            }
        }
        // something has gone wrong
        logger.error("sun.misc.Launcher was never loaded");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy