Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

NanoHTTPD shutdown on its own

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have implement my own NanoHTTPD server
When i started, then it shutdown on its own in the main method
How to ensure it stays and listening

this is my code
 
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you copy the contents of the console from when you run the program that shows all its output and paste it here?
 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you copy the contents of the console from when you run the program that shows all its output and paste it here?



HttpServer property loaded...........
HttpServer startig ...........
httpd created huawei.vbp.api.http.server.HttpServer@5e91993f
HttpServer set Controllers done..........total controllers.3
HttpServer started...........
Running Shutdown Hook ...........
12:21:45.869 [Thread-2] INFO  huawei.vbp.api.Main - API Server is shutdown
BUILD SUCCESSFUL (total time: 0 seconds)
 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Lee wrote:

Norm Radder wrote:Can you copy the contents of the console from when you run the program that shows all its output and paste it here?



HttpServer property loaded...........
HttpServer startig ...........
httpd created huawei.vbp.api.http.server.HttpServer@5e91993f
HttpServer set Controllers done..........total controllers.3
HttpServer started...........
Running Shutdown Hook ...........

BUILD SUCCESSFUL (total time: 0 seconds)

 
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd think that the HttpServer object would create some daemon threads, so your application wouldn't terminate immediately. But I notice you didn't say it terminated immediately, only that it terminated. Perhaps the HttpServer just timed out? I'd suggest checking the parameters of the constructor, e.g. what appears to be a number of seconds or microseconds, and an unknown boolean value; I tried to google NanoHTTPD but I only found things which look nothing at all like your code.

 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You'd think that the HttpServer object would create some daemon threads, so your application wouldn't terminate immediately. But I notice you didn't say it terminated immediately, only that it terminated. Perhaps the HttpServer just timed out? I'd suggest checking the parameters of the constructor, e.g. what appears to be a number of seconds or microseconds, and an unknown boolean value; I tried to google NanoHTTPD but I only found things which look nothing at all like your code.



refer to below with timestamp
HttpServer property loaded...........
HttpServer startig ...........
Thu Feb 15 13:51:19 SGT 2018 httpd created huawei.vbp.api.http.server.HttpServer@39a054a5
Thu Feb 15 13:51:19 SGT 2018HttpServer set Controllers done..........total controllers.4
startedtrue
Thu Feb 15 13:51:19 SGT 2018HttpServer started...........false
Running Shutdown Hook ...........
13:51:19.761 [Thread-2] INFO  huawei.vbp.api.Main - Thu Feb 15 13:51:19 SGT 2018API Server is shutdown
BUILD SUCCESSFUL (total time: 0 seconds)
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is in the StartServer class?

Try an experiment:  Add a call to sleep() at the end of the main() method for long enough  to start a browser to connect to the server and do something.
That will keep the non-daemon thread alive that called the main() method.
 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:What is in the StartServer class?

Try an experiment:  Add a call to sleep() at the end of the main() method for long enough  to start a browser to connect to the server and do something.
That will keep the non-daemon thread alive that called the main() method.



this one I tried.
it works
i put Thread.sleep

but is that the correct way?
how much long I should put?
infinite ?

i dunno when the requrst call will come in
 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:What is in the StartServer class?

Try an experiment:  Add a call to sleep() at the end of the main() method for long enough  to start a browser to connect to the server and do something.
That will keep the non-daemon thread alive that called the main() method.



ServerStarter has nothing but

call the nanohttpd.start

 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it works with the sleep that means there are no non-daemon threads to keep the server running.
Please post the code for the StartServer class so we can see what it does.
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The Java Virtual Machine exits when the only threads running are all daemon threads.


From API doc
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quite often when people have hard-to-fix problems like this, people here would do a web search for the API documentation of the classes in question. However for this problem, I can't find any API for a class called "huawei.vbp.api.http.server.HttpServer". All I can find is documentation about consumer products distributed by Huawei. But you must have some documentation or similar information for that class, no? Hopefully it's online as well; could you post a link to it?
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found some parts of it here:  https://github.com/NanoHttpd/nanohttpd

I suspect the OP did the same as I did and wrapped his own controlling code around the code from github.
 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:If it works with the sleep that means there are no non-daemon threads to keep the server running.
Please post the code for the StartServer class so we can see what it does.



It is part of fi.iki.elonen.util.ServerRunner
I dun have the source, but reverse engineered is something like this
 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:I found some parts of it here:  https://github.com/NanoHttpd/nanohttpd

I suspect the OP did the same as I did and wrapped his own controlling code around the code from github.




That is custom java class, not open API huawei.vbp.api.http.server.HttpServer

It is based on samples HelloServer given in org.nanohttpd.samples.http


 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What does the ServerStarter class do?  You missed posting its code.
 
Norm Radder
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some how all the comments have been stripped from the posted code making it harder to understand.
The version of NanoHTTPD that I have has lots of comments that describe what it is doing.

Can you post the code with comments?
 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All guys (girls),
just to stop by and say, i managed to resolved this by using another nanohttpd
org.nanohttpd.*

in case anyone need help or reference
 
Alex Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANKS SOOOO much for all your help
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic