Skip to content

Instantly share code, notes, and snippets.

@mlooney
Created February 7, 2012 18:57
Show Gist options
  • Save mlooney/1761238 to your computer and use it in GitHub Desktop.
Save mlooney/1761238 to your computer and use it in GitHub Desktop.
Netty Discard server ported to jruby
require 'java'
require 'jar/netty-3.3.1.Final.jar'
import 'java.net.InetSocketAddress'
import 'java.util.concurrent.Executors'
import 'org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory'
import 'org.jboss.netty.bootstrap.ServerBootstrap'
import 'org.jboss.netty.channel.SimpleChannelHandler'
import 'org.jboss.netty.channel.ChannelPipelineFactory'
import 'org.jboss.netty.channel.ChannelFactory'
import 'org.jboss.netty.channel.Channels'
class DiscardHandler < SimpleChannelHandler
def exceptionCaught(ctx, e)
puts e.cause.print_stacktrace
e.channel.close
end
def messageReceived(ctx, msg)
puts "received message: #{msg}"
end
end
class Pipelinefactory
include ChannelPipelineFactory
def get_pipeline
Channels.pipeline(DiscardHandler.new)
end
end
class DiscardServer
def initialize
factory =NioServerSocketChannelFactory.new( Executors.new_cached_thread_pool, Executors.new_cached_thread_pool)
@bootstrap = ServerBootstrap.new(factory)
@bootstrap.pipeline_factory=Pipelinefactory.new
@bootstrap.set_option('child.tcpNoDelay', true)
@bootstrap.set_option('child.keepAlive', true)
end
def start
@bootstrap.bind(InetSocketAddress.new(2020))
end
end
DiscardServer.new.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment