Created
June 14, 2009 01:11
-
-
Save mchung/129504 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 |
This file contains hidden or 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
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