Created
October 13, 2010 04:15
-
-
Save headius/623434 to your computer and use it in GitHub Desktop.
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
require 'java' | |
begin | |
require 'jar/netty-3.2.2.Final' | |
rescue LoadError | |
require 'fileutils' | |
FileUtils.mkdir_p 'jar' | |
system "wget -O jar/netty-3.2.2.Final.jar http://repository.jboss.org/nexus/content/groups/public-jboss/org/jboss/netty/netty/3.2.2.Final/netty-3.2.2.Final.jar" | |
require 'jar/netty-3.2.2.Final' | |
end | |
class EchoServer | |
java_import java.net.InetSocketAddress | |
java_import java.util.concurrent.Executors | |
java_import org.jboss.netty.bootstrap.ServerBootstrap | |
java_import org.jboss.netty.channel.ChannelPipelineFactory | |
java_import org.jboss.netty.channel.Channels | |
java_import org.jboss.netty.channel.SimpleChannelUpstreamHandler | |
java_import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory | |
def initialize(port) | |
channel_factory = | |
NioServerSocketChannelFactory.new( | |
Executors.newCachedThreadPool, | |
Executors.newCachedThreadPool) | |
@bootstrap = ServerBootstrap.new(channel_factory) | |
@bootstrap.pipeline_factory = EchoPipelineFactory.new | |
@bootstrap.bind InetSocketAddress.new port | |
puts "Server ready on port #{port}" | |
end | |
class EchoServerHandler < SimpleChannelUpstreamHandler | |
# override messageReceived | |
def messageReceived(ctx, e) | |
e.channel.write(e.message) | |
end | |
end | |
class EchoPipelineFactory | |
include ChannelPipelineFactory | |
# implement getPipeline from ChannelPipelineFactory | |
def getPipeline | |
return Channels.pipeline EchoServerHandler.new | |
end | |
end | |
end | |
if __FILE__ == $0 | |
EchoServer.new 8080 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As it can be seen in this small code sample, one of the things that is badly missing in JRuby is something like Groovy's @grab annotation.