Created
May 6, 2014 07:51
-
-
Save normanmaurer/a2c58ed42b9a5c28d402 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| final NioEventLoopGroup group = new NioEventLoopGroup(); | |
| Bootstrap bs = new Bootstrap(); | |
| bs.channel(NioDatagramChannel.class); | |
| bs.group(group); | |
| bs.handler(new ChannelInitializer<Channel>() { | |
| @Override | |
| protected void initChannel(Channel ch) throws Exception { | |
| ChannelPipeline pipeline = ch.pipeline(); | |
| pipeline.addLast(new ChannelInboundHandlerAdapter() { | |
| @Override | |
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | |
| final DatagramPacket packet = (DatagramPacket) msg; | |
| ChannelFuture cf = connectRestServer(ctx.channel().eventLoop().parent()); | |
| cf.addListener(new ChannelFutureListener() { | |
| @Override | |
| public void operationComplete(ChannelFuture future) throws Exception { | |
| if (!future.isSuccess()) { | |
| // something went wrong | |
| packet.release(); | |
| } else { | |
| ByteBuf content = packet.content(); | |
| final Channel ch = future.channel(); | |
| FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "/path", content); | |
| HttpHeaders.setContentLength(request, content.readableBytes()); | |
| HttpHeaders.setKeepAlive(request, true); | |
| HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_ENCODING, "application/json;charset=utf-8"); | |
| ch.writeAndFlush(request).addListener(new ChannelFutureListener() { | |
| @Override | |
| public void operationComplete(ChannelFuture future) throws Exception { | |
| if (!future.isSuccess()) { | |
| // and remove from connection pool if you have one etc... | |
| ch.close(); | |
| } | |
| } | |
| }); | |
| } | |
| } | |
| }); | |
| } | |
| ChannelFuture connectRestServer(EventLoopGroup group) { | |
| // TODO: Either bootstrap or use existing connection | |
| return null; | |
| } | |
| }); | |
| } | |
| }); | |
| bs.bind(9999).syncUninterruptibly(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment