Skip to content

Instantly share code, notes, and snippets.

@aamedina
Created June 17, 2014 20:38
Show Gist options
  • Select an option

  • Save aamedina/c97a994a82a200c64c0e to your computer and use it in GitHub Desktop.

Select an option

Save aamedina/c97a994a82a200c64c0e to your computer and use it in GitHub Desktop.
(defn server-handler []
(proxy [ChannelHandlerAdapter] []
(channelRead [^ChannelHandlerContext ctx ^ByteBuf msg]
(.write ctx msg)
(.flush ctx))
(exceptionCaught [^ChannelHandlerContext ctx ^Throwable cause]
(.printStackTrace cause)
(.close ctx))))
(defn handlers
[]
(into-array ChannelHandler [(server-handler)]))
(defn run
[port]
(let [bosses (NioEventLoopGroup.)
workers (NioEventLoopGroup.)]
(try
(let [bootstrap (doto (ServerBootstrap.)
(.group bosses workers)
(.channel NioServerSocketChannel)
(.childHandler (proxy [ChannelInitializer] []
(initChannel [^SocketChannel ch]
(-> (.pipeline ch)
(.addLast (handlers))))))
(.option ChannelOption/SO_BACKLOG (int 128))
(.childOption ChannelOption/SO_KEEPALIVE true))
f (.sync (.bind bootstrap (int port)))
channel (.channel f)]
(.sync (.closeFuture channel)))
(finally
(.shutdownGracefully workers)
(.shutdownGracefully bosses)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment