Skip to content

Instantly share code, notes, and snippets.

@headius
Created October 13, 2010 04:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/623434 to your computer and use it in GitHub Desktop.
Save headius/623434 to your computer and use it in GitHub Desktop.
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
@behrangsa
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment