- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
why a scala code fails to launch a daemon?
Mon, 2009-12-21, 18:04
Hello Scala Users,
This is a newbie question:
I am trying to build a simple scala web server using jetty embedded. My
scala code is:
--------------------- file jetty_handler.scala
----------------------------------
import org.mortbay.jetty.Server
import org.mortbay.jetty.Request
import org.mortbay.jetty.handler.DefaultHandler
import org.mortbay.jetty.handler.ResourceHandler
import org.mortbay.jetty.handler.ContextHandler
import org.mortbay.jetty.handler.AbstractHandler
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
class MutableHandler extends AbstractHandler {
var html = "
Hello
"
override def handle(target: String, request: HttpServletRequest,
response: HttpServletResponse, dispatch:Int ) = {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(html.toString());
(request.asInstanceOf[Request]).setHandled(true);
}
}
var server = new Server(8080)
var handler= new MutableHandler()
server.setHandler(handler)
server.start()
--------------------------------------------------
when I run the program using:
scala jetty_handler.scala
the socket does not listen on port 8080, however, when I start the
interactive mode using 'scala' and then cut and paste the above code,
the server does listen on the port 8080.
Do we have an explanation to that phenomenon?
What should I do to start a program that listen on a socket? Create a
'package' and have it run using java?
Thanks in advance
Tux
Mon, 2009-12-21, 18:47
#2
Re: why a scala code fails to launch a daemon?
Thank you Paul for your answer,
after digging a bit I saw at
http://cwiki.apache.org/BUILDR/how-to-run-jetty.html
that appending the line:
server.join()
to the scala file (after the server.start() line) does resolve the issue ;)
Thanks gain,
Tux
Paul Phillips wrote:
> On Mon, Dec 21, 2009 at 05:04:09PM +0000, Tux Racer wrote:
>
>> when I run the program using:
>>
>> scala jetty_handler.scala
>>
>> the socket does not listen on port 8080
>>
>
> I think it does. Then it immediately exist because there's no more
> code. Try putting a Thread.sleep or something after the start call.
>
>
>> however, when I start the interactive mode using 'scala' and then cut
>> and paste the above code, the server does listen on the port 8080.
>>
>
> In the interactive mode it doesn't exit until you quit.
>
>
On Mon, Dec 21, 2009 at 05:04:09PM +0000, Tux Racer wrote:
> when I run the program using:
>
> scala jetty_handler.scala
>
> the socket does not listen on port 8080
I think it does. Then it immediately exist because there's no more
code. Try putting a Thread.sleep or something after the start call.
> however, when I start the interactive mode using 'scala' and then cut
> and paste the above code, the server does listen on the port 8080.
In the interactive mode it doesn't exit until you quit.