nettyserver await termination fail with spring boot

28 views
Skip to first unread message

omid pourhadi

unread,
Nov 10, 2018, 8:02:50 AM11/10/18
to grpc.io
Hi,

I'm trying to create a spring boot app with grpc so I implemented a grpc nettyserver to run spring boot on top of it but it terminates the app after running.
how can I use gprc nettyserver await termination in spring boot webserver?

here is the code : 

import java.io.IOException;

import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.server.WebServerException;

import io.grpc.Server;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;

public class NettyWebServer implements WebServer
{

    
    public static final int DEFAULT_PORT = 50051;
    
    Server server;
    
    @Override
    public void start() throws WebServerException
    {
        if(server == null)
            server = NettyServerBuilder.forPort(DEFAULT_PORT).build();
        
        try
        {            
            server.start();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run()
            {
                // Use stderr here since the logger may have been reset by its
                // JVM shutdown hook.
                System.err.println("*** shutting down gRPC server since JVM is shutting down");
                NettyWebServer.this.stop();
                System.err.println("*** server shut down");
            }
        });
        startDaemonAwaitThread();
        
    }

    @Override
    public void stop() throws WebServerException
    {
        if (server != null)
        {
            server.shutdown();
        }        
    }

    @Override
    public int getPort()
    {
        return DEFAULT_PORT;
    }
    
    private void startDaemonAwaitThread() {
        Thread awaitThread = new Thread(()->{
                try {
                    NettyWebServer.this.server.awaitTermination();
                } catch (InterruptedException e) {
//                    log.error("gRPC server stopped.", e);
                }
            });
        awaitThread.setContextClassLoader(getClass().getClassLoader());
        awaitThread.setDaemon(false);
        awaitThread.start();
    }

}



and here is the spring boot app config

@SpringBootApplication
public class GrpcBoot
{

    public static void main(String[] args) throws Exception
    {
        SpringApplication.run(GrpcBoot.class, args);
    }

    
    @Bean
    ServletWebServerFactory servletWebServerFactory()
    {
        return new ServletWebServerFactory() {
            
            @Override
            public WebServer getWebServer(ServletContextInitializer... initializers)
            {
                return new NettyWebServer();
            }
        };
    }
    
    
    
}


here is the log after termination :

2018-11-10 16:32:02.499  INFO 17044 --- [           main] com.omid.grpc.boot.GrpcBoot              : Starting GrpcBoot on opourhadi with PID 17044 (/home/omidp/workspace-grpc/grpc-crud/grpc-server/target/classes started 
2018-11-10 16:32:02.502  INFO 17044 --- [           main] com.omid.grpc.boot.GrpcBoot              : No active profile set, falling back to default profiles: default
2018-11-10 16:32:02.534  INFO 17044 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3c9d0b9d: startup date [Sat Nov 10 16:32:02 IRST 2018]; root of context hierarchy
2018-11-10 16:32:03.054  INFO 17044 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-11-10 16:32:03.063  INFO 17044 --- [           main] com.omid.grpc.boot.GrpcBoot              : Started GrpcBoot in 0.738 seconds (JVM running for 1.022)
2018-11-10 16:32:03.065  INFO 17044 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3c9d0b9d: startup date [Sat Nov 10 16:32:02 IRST 2018]; root of context hierarchy
2018-11-10 16:32:03.067  INFO 17044 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown


Carl Mastrangelo

unread,
Nov 14, 2018, 11:28:19 AM11/14/18
to grpc.io
Can you call awaitTermination in stop()?  You can use shutdownNow() also to cause a more aggressive shutdown.

omid pourhadi

unread,
Nov 18, 2018, 7:38:54 AM11/18/18
to grpc.io
thank you for your reply.

I managed to fix that with CommandLineRunner interface in spring boot.

Reply all
Reply to author
Forward
0 new messages