javaeclipsevert.xvertx-verticlevert.x-webclient

Vert.x application closes when run with Eclipse but does not close when run as a jar


Here is my code for main class:

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new MainVerticle(), whenDeployed -> {
                vertx.close();
//              System.exit(0);
            });
    }

Below is the code for main verticle:

public class MainVerticle extends AbstractVerticle {

    private static final ObjectMapper mapper = new ObjectMapper();
    private static final Logger log = LoggerFactory.getLogger(MainVerticle.class);
    
    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        
        Configuration config = null;
        
        try(FileInputStream fin = new FileInputStream("/opt/vertex-proxy/config.json")){
            config = mapper.readValue(fin, Configuration.class);
            
        } catch(Exception e) {
            log.error("Invalid configuration\n{}", e.getCause());
        }
        
        log.info("Started application\n");
        
        ProxyOptions proxyOptions = new ProxyOptions()
                .setHost(config.getProxy().getIp())
                .setPort(config.getProxy().getPort());
                
        WebClientOptions options = new WebClientOptions().setUserAgent("Yash client")
                .setProtocolVersion(HttpVersion.HTTP_2).setHttp2ClearTextUpgrade(false)
                .setConnectTimeout(config.getTarget().getConnectionTimeout())
                .setProxyOptions(proxyOptions);
        
        WebClient client = WebClient.create(vertx, options);

        
        HttpRequest<Buffer> req = client.get(config.getTarget().getPort(), config.getTarget().getIp(), config.getTarget().getUri());
        
        req.send().onSuccess(response -> {
            log.info("Received response with status code: " + response.statusCode());
            log.info("Response:\n" + response.bodyAsString());
            startPromise.complete();
            
        }).onFailure(err -> {
            log.error("Something went wrong\n" + err.getCause());
            startPromise.complete();
        });
        
    }
    
    @Override
    public void stop() throws Exception {
        System.out.println("\nStopping application");
    }

}

When I run the above code in Eclipse it performs perfectly fine and the program shuts down after execution. But when I build a maven jar out of this and try to run the jar using the command java -jar application.jar then the program executes and shows the response but it does not exits unless interupted manually.

Could anyone suggest a proper way to resolve this so it behaves the same in Eclipse as well as a jar?

I even tried using java.lang.System.exit() in the onSuccess and onFailure blocks and also in whenDeployed block in the main class and inside the stop() method too but still observed similar behaviour.


Solution

  • What you run in Eclipse is very likely your own main class you showed above, which also closes Vert.x.

    When you run the jar however, the Main-Class might be io.vertx.core.Launcher - depending on how you set up your build. Take a look in the jar's META-INF/MANIFEST.MF file.

    If you see an entry there like Main-Class: io.vertx.core.Launcher, then the jar is run with Vert.x launcher.

    And then your MainVerticle just sends a request and logs the response but does not close Vert.x - and that's what you coded.