Skip to content

Instantly share code, notes, and snippets.

@shlomiv
Last active December 14, 2015 20:19
Show Gist options
  • Save shlomiv/5142879 to your computer and use it in GitHub Desktop.
Save shlomiv/5142879 to your computer and use it in GitHub Desktop.
;;; based on the discussion at https://groups.google.com/d/msg/netty/mcGswAZ5NeQ/ivG9snRSFy4J
(ns netty4.core
(:import [io.netty.bootstrap AbstractBootstrap ServerBootstrap]
[io.netty.channel ChannelFuture ChannelInitializer ChannelOption
ChannelHandlerContext ChannelInboundHandlerAdapter ChannelHandler]
[io.netty.handler.logging LogLevel LoggingHandler]
io.netty.buffer.ByteBuf
io.netty.channel.socket.SocketChannel
io.netty.channel.nio.NioEventLoopGroup
io.netty.channel.socket.nio.NioServerSocketChannel
java.util.concurrent.atomic.AtomicInteger)
(:use [clojure.stacktrace])
(:gen-class))
(def echo-server-handler
(proxy [ChannelInboundHandlerAdapter] []
(channelRead [ctx msg] (.write ctx msg))
(channelReadComplete [ctx] (.flush ctx))
(exceptionCaught [ctx cause]
(print-stack-trace cause)
(.close ctx))))
(defn -main []
(let [b (ServerBootstrap.)
elg1 (NioEventLoopGroup.)
elg2 (NioEventLoopGroup.)
shutdown #(do (.shutdownGracefully elg1)
(.shutdownGracefully elg2))]
(-> b
(.group elg1 elg2)
(.channel io.netty.channel.socket.nio.NioServerSocketChannel)
(.option ChannelOption/SO_BACKLOG (int 100))
(.childHandler
(proxy [ChannelInitializer] []
(initChannel [ch]
(-> ch (.pipeline)
(.addLast
(into-array ChannelHandler [echo-server-handler]))))))
(.bind 2424))
(println b) ; print the server bootstrap object
shutdown))
(def r (-main))
(r) ;; call to terminate server
(ns index-server.netty
(:import [io.netty.bootstrap AbstractBootstrap ServerBootstrap]
[io.netty.channel ChannelFuture ChannelInitializer ChannelOption
ChannelHandlerContext ChannelInboundByteHandlerAdapter]
[io.netty.handler.logging LogLevel LoggingHandler]
io.netty.buffer.ByteBuf
io.netty.channel.socket.SocketChannel
io.netty.channel.nio.NioEventLoopGroup
io.netty.channel.socket.nio.NioServerSocketChannel
java.util.concurrent.atomic.AtomicInteger)
(:use [clojure.stacktrace])
(:gen-class))
(defn echo-server-handler []
(proxy [ChannelInboundByteHandlerAdapter] []
(inboundBufferUpdated [this ctx in]
(let [out (.nextOutboundByteBuffer ctx)]
(.writeBytes out in)
(.flust ctx)))
(exceptionCaught [this ctx cause]
(print-stack-trace cause)
(.close ctx))))
(defn -main []
(let [b (ServerBootstrap.)]
(try
(-> b
(.group (NioEventLoopGroup.) (NioEventLoopGroup.))
(.channel io.netty.channel.socket.nio.NioServerSocketChannel)
(.option ChannelOption/SO_BACKLOG 100)
(.childHandler
(proxy [ChannelInitializer] []
(initChannel [this ch]
(-> ch
(.pipeline)
(.addLast (LoggingHandler. LogLevel/INFO)
(echo-server-handler)))))))
(println b) ; print the server bootstrap object
(let [f (-> b
(.bind 2424)
(.sync))]
(-> f
(.channel)
(.closeFuture)
(.sync)))
(finally (.shutdown b)))))
(ns test-app.core
(:import [io.netty.bootstrap AbstractBootstrap ServerBootstrap]
[io.netty.channel ChannelFuture ChannelInitializer ChannelOption
ChannelHandlerContext ChannelInboundByteHandlerAdapter]
[io.netty.handler.logging LogLevel LoggingHandler]
io.netty.buffer.ByteBuf
io.netty.channel.socket.SocketChannel
io.netty.channel.nio.NioEventLoopGroup
io.netty.channel.socket.nio.NioServerSocketChannel
java.util.concurrent.atomic.AtomicInteger)
(:use [clojure.stacktrace])
(:gen-class))
(def b (ServerBootstrap.))
(.group b (NioEventLoopGroup.))
(.channel b NioServerSocketChannel)
;(.option b ChannelOption/SO_BACKLOG 100)
(.childHandler b
(proxy [ChannelInitializer] []
(initChannel [ch] (println "new connection"))))
(.bind b 9999)
@shlomiv
Copy link
Author

shlomiv commented Mar 14, 2013

gistfile2.clj - this is a first working version

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