2012-12-18 12 views
6

Ho scritto una Groovy MainApp con main (args).Come eseguire vert.x embedded?

All'avvio, la JVM esce direttamente ("Fine esecuzione JVM!").

import org.vertx.groovy.core.Vertx 

class MainApp { 

    public static void main(String[] args) { 

     Vertx vertx = VertxFactory.newVertx(); 

     vertx.createHttpServer().requestHandler{ request -> 
      println "A request has arrived on the server!" 
     }.listen(8080) 

     println "End of JVM execution !" 
    } 
} 

Come per funzionare correttamente un server HTTP integrato con vert.x?

risposta

2

Ho avuto lo stesso problema con Java. Alla fine ho messo un oggetto in .wait() dopo tutto il codice vert.x. Sembra orribile, ma in realtà ha senso perché mi dà un trigger per spegnere il server su richiesta (tramite .notify()).

Questo non è banale, deve essere menzionato sulla documentazione ufficiale Vert.x.

+2

Di fronte a questo, sembra che il vertx non abbia il tempo di avviarsi in modalità non cluster e jvm si ferma prima. L'ho risolto con 'TimeUnit.SECONDS.sleep (1);' alla fine: https://gist.github.com/yetanothercoder/21a2b47b686d902c5fee – yetanothercoder

+0

@yetanothercoder qualsiasi motivo per non usare '.wait' /' .notify' ? – user7610

0

Ho affrontato questo. Ho provato a passare il nome host quindi ha funzionato bene.

VertX VertX = Vertx.newVertx ("hostname")

Credo che ci sia qualche problema di determinare l'indirizzo IP durante l'esecuzione a livello locale e che sta fallendo.

+0

No, http://stackoverflow.com/a/14652319/365675 – yetanothercoder

0
RouteMatcher routeMatcher = new RouteMatcher(); 

     // HTTP server 
     HttpServer httpServer = vertx.createHttpServer(); 
     httpServer.requestHandler(routeMatcher); 
httpServer.listen(7777); 
0

Dalle documentazioni:

all Vert.x threads are daemon threads and they will not prevent the JVM for exiting. Therefore you must ensure that the main() method does not run to completion, e.g. in this example we have used System.in.read() to block the main thread waiting for IO from the console. 

Quindi, è necessario aggiungere il seguente alla fine del tuo metodo principale come questo:

// Prevent the JVM from exiting 
System.in.read();