Skip to content

Instantly share code, notes, and snippets.

@eoconnell
Last active June 20, 2018 11:58
Show Gist options
  • Save eoconnell/acde91588f1b45054ce261ab5de26edf to your computer and use it in GitHub Desktop.
Save eoconnell/acde91588f1b45054ce261ab5de26edf to your computer and use it in GitHub Desktop.
CentOS/6 - Upstart - Java
make deps
make
make install
make run
make test
# Upstart 0.6.5 - set of features is limited
description "Hello Java Service"
author "Evan"
#setuid helloapp
#setgid helloapp
respawn
#console log
exec java -classpath /etc/hello Service
JFLAGS = -g
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = Service.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
.PHONY: deps
deps:
yum -y install java-1.8.0-openjdk-devel
.PHONY: install
install:
cp hello.conf /etc/init/; mkdir -p /etc/hello; cp *.class /etc/hello/
.PHONY: run
run:
initctl start hello
.PHONY: test
test:
curl http://localhost:8000/test
import java.util.Date;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class Service {
public static void main(String[] args) throws Exception {
System.out.println("Started");
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/test", new Handler());
server.setExecutor(null); // creates a default executor
server.start();
}
public static class Handler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "this is the response " + new Date() + "\n";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment