Why webserver exist at the end of the main

67 views
Skip to first unread message

Bruce Chen

unread,
Sep 24, 2012, 9:30:24 AM9/24/12
to web...@googlegroups.com
I copy the code from https://github.com/webbit/webbit/wiki/Writing-HttpHandlers
public class MyApp {
  public static void main(String[] args) {
    WebServer webServer = WebServers.createWebServer(9996)
      .add(new HelloWorldHandler());
    webServer.start();
    System.out.println("Listening on " + webServer.getUri());
  }
}

If I run it, webServer will exist at the end of main,while I debug it, webServer will run.
I'm confusing.

aslak hellesoy

unread,
Sep 24, 2012, 9:32:30 AM9/24/12
to web...@googlegroups.com
It's running in a daemon thread.


Aslak
I'm confusing.

aslak hellesoy

unread,
Sep 24, 2012, 9:37:19 AM9/24/12
to web...@googlegroups.com


On Monday, September 24, 2012, aslak hellesoy wrote:


On Monday, September 24, 2012, Bruce Chen wrote:
I copy the code from https://github.com/webbit/webbit/wiki/Writing-HttpHandlers
public class MyApp {
  public static void main(String[] args) {
    WebServer webServer = WebServers.createWebServer(9996)
      .add(new HelloWorldHandler());
    webServer.start();
    System.out.println("Listening on " + webServer.getUri());
  }
}

If I run it, webServer will exist at the end of main,while I debug it, webServer will run.

It's running in a daemon thread.


Sorry, I meant user thread. The JVM will keep running until that thread is stopped, which you can do with WebServer.stop().

Aslak

Bruce Chen

unread,
Sep 24, 2012, 9:51:31 AM9/24/12
to web...@googlegroups.com
I didn't stop the webServer, but it existed, I think the webServer thread already stopped.

在 2012年9月24日星期一UTC+8下午9时37分20秒,Aslak Hellesøy写道:

aslak hellesoy

unread,
Sep 24, 2012, 10:11:43 AM9/24/12
to web...@googlegroups.com


On Monday, September 24, 2012, Bruce Chen wrote:
I didn't stop the webServer, but it existed, I think the webServer thread already stopped.


Maybe I misunderstood your question. What had you expected to happen?

Aslak

Bruce Chen

unread,
Sep 24, 2012, 10:20:05 AM9/24/12
to web...@googlegroups.com
I think webServer.start() in main is start the webserver thread and webServer still run when the main exist;if I want to stop the webserver, I must call webserver.stop().
But in the demo code, the webserver stopped while main existed.


在 2012年9月24日星期一UTC+8下午10时11分43秒,Aslak Hellesøy写道:

aslak hellesoy

unread,
Sep 24, 2012, 11:42:56 AM9/24/12
to web...@googlegroups.com


On Monday, September 24, 2012, Bruce Chen wrote:
I think webServer.start() in main is start the webserver thread and webServer still run when the main exist;if I want to stop the webserver, I must call webserver.stop().
But in the demo code, the webserver stopped while main existed.


I don't think it stopped, it's more likely that it never started.

WebServer.start() returns a Future object. This means start() can return before the server is ready to accept http requests. To wait until it's ready, call get() on the future. (I have updated the wiki to reflect this).

Does this answer your question? 
Message has been deleted

Bruce Chen

unread,
Sep 25, 2012, 10:21:21 AM9/25/12
to web...@googlegroups.com
Oh, I'm so stupid, I used webbit-0.4.9-full.jar as lib, so webserver can' work when main exist.
Now, I change it to webbit-0.4.14.jar, webserver works very well.
Thanks for your answers.

在 2012年9月24日星期一UTC+8下午11时42分57秒,Aslak Hellesøy写道:

aslak hellesoy

unread,
Sep 25, 2012, 10:35:13 AM9/25/12
to web...@googlegroups.com


On Tuesday, September 25, 2012, Bruce Chen wrote:
Sorry, problem is still on,here is my demo code.
[code]
public class HelloWorldHandler implements HttpHandler {

@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response,
HttpControl control) throws Exception {
// TODO Auto-generated method stub
response.header("Content-type", "text/html")
        .content("<html><body>Hello world!</body></html>")
        .end();
}

}

public class WebServerDemo {
public static void main(String[] args) throws InterruptedException, ExecutionException, MalformedURLException, IOException{
startServer();
   
}
public static void startServer() throws InterruptedException, ExecutionException, MalformedURLException, IOException{
Future<? extends WebServer> f = WebServers.createWebServer(9996)
.add("/",new HelloWorldHandler()).start();
WebServer webServer = f.get();
System.out.println(webServer.getUri().toString());
}
}
[/code]
I run it, then visit "http://localhost:9996",but the webserver isn't running.

Now, my question is simple: why I can't visit the "http://localhost:9996" ?

I haven't tried your code, but it looks correct.

What webbit and netty version are you using? What java version? What OS and what browser?
What do you see in the browser? Can you add a println to your http handler to verify whether a request comes in or not? What happens if you `curl http://localhost:9996` (assuming you're on Linux or OS X).

Aslak

在 2012年9月24日星期一UTC+8下午11时42分57秒,Aslak Hellesøy写道:

aslak hellesoy

unread,
Sep 25, 2012, 12:43:59 PM9/25/12
to web...@googlegroups.com


On Tuesday, September 25, 2012, Bruce Chen wrote:
Oh, I'm so stupid, I used webbit-0.4.9-full.jar as lib, so webserver can' work when main exist.

I don't understand what you mean by that ("as lib" and why it can't work with a main), but I'm glad you got it working.

Aslak 
Reply all
Reply to author
Forward
0 new messages