Created
July 1, 2012 03:48
-
-
Save gkojax/3026734 to your computer and use it in GitHub Desktop.
Finagleのhttpサーバーをcommons-daemonでデーモン化した
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/sh | |
CLASSPATH=.:./finagle.jar | |
PID_FILE=./pidfile | |
BACKUP_DIR=`pwd` | |
cd `dirname $0` | |
CUR_DIR=`pwd` | |
start() | |
{ | |
arch -arch x86_64 /usr/local/bin/jsvc \ | |
-server \ | |
-home $JAVA_HOME \ | |
-cp $CLASSPATH \ | |
-pidfile $PID_FILE \ | |
-Dlogback.configurationFile=./logback.xml \ | |
-debug -verbose \ | |
-outfile stdout.log \ | |
-errfile stderr.log \ | |
org.example.Application $CUR_DIR | |
} | |
stop() | |
{ | |
arch -arch x86_64 /usr/local/bin/jsvc \ | |
-stop \ | |
-pidfile $PID_FILE \ | |
org.example.Application | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
restart) | |
stop | |
start | |
;; | |
*) | |
echo "Usage $0 start/stop" | |
exit 1;; | |
esac | |
cd $BACKUP_DIR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example | |
import java.io.File | |
import java.util.concurrent.TimeUnit | |
import java.net.InetSocketAddress | |
import com.twitter.finagle.{Service, SimpleFilter} | |
import com.twitter.util.{Future, Duration} | |
import com.twitter.finagle.http.path._ | |
import com.twitter.finagle.builder.{Server, ServerBuilder} | |
import com.twitter.finagle.http._ | |
import org.apache.commons.daemon.Daemon | |
import org.apache.commons.daemon.DaemonContext | |
class MyServer extends Service[Request, Response] { | |
def apply(request: Request) = { | |
Future.value(Response()) | |
} | |
} | |
class Application extends Daemon { | |
var server: Server = _ | |
var applicationPath: File = _ | |
override def init(dc: DaemonContext) { | |
applicationPath = new File(dc.getArguments()(0)) | |
} | |
override def start { | |
server = ServerBuilder() | |
.codec(RichHttp[Request](Http())) | |
.bindTo(new InetSocketAddress(8080)) | |
.name("httpserver") | |
.build(new MyServer) | |
} | |
override def stop { | |
if (server != null) server.close(Duration(1, TimeUnit.MICROSECONDS)) | |
} | |
override def destroy { | |
server = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コメント
http://tmblr.co/ZkKCZyOSsNjW