Skip to content

Instantly share code, notes, and snippets.

@mchung
Created June 14, 2009 01:11
Show Gist options
  • Save mchung/129504 to your computer and use it in GitHub Desktop.
Save mchung/129504 to your computer and use it in GitHub Desktop.
# To monitor this JRuby application with JMX goodness, export the following environment variable before starting the program
#
# export JAVA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8686 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
class Handler
def initialize(request)
@request = request
end
def run
os = Java::java.io.PrintStream.new(@request.get_output_stream)
os.println("<html><body>Hello, world</body></html>")
@request.close
end
end
def main
ss = Java::java.net.ServerSocket.new(4444)
pool = Java::java.util.concurrent.Executors.newFixedThreadPool(5)
puts "Starting the JRuby Server"
while true
request = ss.accept
pool.execute(Handler.new(request))
end
end
main
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class Handler implements Runnable {
private final Socket request;
public Handler(final Socket request) {
this.request = request;
}
public void run() {
// Get input from the client
try {
PrintStream ps = new PrintStream(this.request.getOutputStream());
ps.println("<html><body>Hello, world</body></html>");
this.request.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ServerEx {
public static void main(String argv[]) throws IOException {
final ServerSocket server = new ServerSocket(4444);
final ExecutorService pool = Executors.newFixedThreadPool(5);
System.out.println("Starting the Java ExecutorService powered server");
while (true) {
Socket request = server.accept();
pool.execute(new Handler(request));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment