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

org.glowroot.agent.it.harness.impl.JavaagentMain Maven / Gradle / Ivy

There is a newer version: 0.9.24
Show newest version
/*
 * Copyright 2014-2016 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.it.harness.impl;

import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Nullable;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.glowroot.agent.shaded.grpc.Server;
import org.glowroot.agent.shaded.grpc.netty.NettyServerBuilder;
import org.glowroot.agent.shaded.netty.channel.EventLoopGroup;

import org.glowroot.agent.it.harness.grpc.JavaagentServiceGrpc;

import static java.util.concurrent.TimeUnit.SECONDS;

public class JavaagentMain {

    public static void main(String... args) throws Exception {

        try {
            Class.forName("org.slf4j.bridge.SLF4JBridgeHandler")
                    .getMethod("removeHandlersForRootLogger").invoke(null);
            Class.forName("org.slf4j.bridge.SLF4JBridgeHandler")
                    .getMethod("install").invoke(null);
        } catch (ClassNotFoundException e) {
            // this is needed when running logger plugin tests against old logback versions
        } catch (NoSuchMethodException e) {
            // this is needed when running logger plugin tests against old logback versions
        }

        int port = Integer.parseInt(args[0]);
        // socket is never closed since program is still running after main returns
        Socket socket = new Socket((String) null, port);
        ObjectOutputStream objectOut = new ObjectOutputStream(socket.getOutputStream());
        new Thread(new SocketHeartbeat(objectOut)).start();

        int javaagentServicePort = Integer.parseInt(args[1]);
        JavaagentServiceImpl javaagentService = new JavaagentServiceImpl();
        final EventLoopGroup bossEventLoopGroup = EventLoopGroups.create("Glowroot-grpc-boss-ELG");
        final EventLoopGroup workerEventLoopGroup =
                EventLoopGroups.create("Glowroot-grpc-worker-ELG");
        // need at least 2 threads, one for executeApp(), and another for handling interruptApp() at
        // the same time
        final ExecutorService executor = Executors.newCachedThreadPool(
                new ThreadFactoryBuilder()
                        .setDaemon(true)
                        .setNameFormat("Glowroot-grpc-executor-%d")
                        .build());
        final Server server = NettyServerBuilder.forPort(javaagentServicePort)
                .bossEventLoopGroup(bossEventLoopGroup)
                .workerEventLoopGroup(workerEventLoopGroup)
                .executor(executor)
                .addService(JavaagentServiceGrpc.bindService(javaagentService))
                .build()
                .start();
        javaagentService.setServerCloseable(new Callable() {
            @Override
            public @Nullable Void call() throws Exception {
                server.shutdown();
                if (!server.awaitTermination(10, SECONDS)) {
                    throw new IllegalStateException("Could not terminate gRPC channel");
                }
                executor.shutdown();
                if (!executor.awaitTermination(10, SECONDS)) {
                    throw new IllegalStateException("Could not terminate gRPC executor");
                }
                if (!bossEventLoopGroup.shutdownGracefully(0, 0, SECONDS).await(10, SECONDS)) {
                    throw new IllegalStateException(
                            "Could not terminate gRPC boss event loop group");
                }
                if (!workerEventLoopGroup.shutdownGracefully(0, 0, SECONDS).await(10, SECONDS)) {
                    throw new IllegalStateException(
                            "Could not terminate gRPC worker event loop group");
                }
                return null;
            }
        });

        // spin a bit to so that caller can capture a trace with  if desired
        for (int i = 0; i < 1000; i++) {
            timerMarkerOne();
            timerMarkerTwo();
            Thread.sleep(1);
        }
        // non-daemon threads started above keep jvm alive after main returns
        Thread.sleep(Long.MAX_VALUE);
    }

    private static void timerMarkerOne() throws InterruptedException {
        Thread.sleep(1);
    }

    private static void timerMarkerTwo() throws InterruptedException {
        Thread.sleep(1);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy