Java tutorial
/* * Tencent is pleased to support the open source community by making Mars available. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. * * Licensed under the MIT License (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://opensource.org/licenses/MIT * * Unless required by applicable law or agreed to in writing, software distributed under the License is * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language governing permissions and * limitations under the License. */ package com.tencent.mars.proxy; import com.tencent.mars.datacenter.CacheData; import com.tencent.mars.logicserver.ProxySession; import org.apache.log4j.PropertyConfigurator; import java.util.Properties; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; /** * * Simple proxy server for mars * * Created by zhaoyuan on 16/2/2. */ public class ProxyServer { static { Properties pro = new Properties(); pro.put("log4j.rootLogger", "DEBUG,stdout,R"); pro.put("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender"); pro.put("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout"); pro.put("log4j.appender.stdout.layout.ConversionPattern", "%5p [%t] (%F:%L) - %m%n"); pro.put("log4j.appender.R", "org.apache.log4j.DailyRollingFileAppender"); pro.put("log4j.appender.R.Threshold", "INFO"); pro.put("log4j.appender.R.File", "${user.home}/logs/mars/info.log"); pro.put("log4j.appender.R.DatePattern", ".yyyy-MM-dd"); pro.put("log4j.appender.R.layout", "org.apache.log4j.PatternLayout"); pro.put("log4j.appender.R.layout.ConversionPattern", "[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"); PropertyConfigurator.configure(pro); } private int port; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; private ServerBootstrap serverBootstrap; private ChannelHandler channelHandler; /** * * @param port */ public ProxyServer(int port) { this.port = port; bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); channelHandler = new ChannelInitializerImpl<SocketChannel>(); } public void start() throws Exception { try { serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(channelHandler).option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture channelFuture = serverBootstrap.bind(port).sync(); channelFuture.channel().closeFuture().sync(); } catch (Exception e) { } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } /** * * @param <T> */ public class ChannelInitializerImpl<T extends Channel> extends ChannelInitializer<T> { @Override protected void initChannel(T channel) throws Exception { channel.pipeline().addLast(new NetMsgHeaderHandler()); } } public static void main(String[] args) throws Exception { // init non-persistent database CacheData.connect(); ProxySession.Manager.connect(); int port; if (args.length > 0) { port = Integer.parseInt(args[0]); } else { port = 8081; } new ProxyServer(port).start(); ProxySession.Manager.disconnect(); CacheData.disconnect(); } }