@Override protected void serverStart() throws RemotingException { acceptor = new NioSocketAcceptor(); //TCP Acceptor // acceptor.getFilterChain().addFirst("logging", new MinaLoggingFilter()); acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(getCodec()))); acceptor.getFilterChain().addLast("mdc", new MdcInjectionFilter()); acceptor.setHandler(new MinaHandler(this)); IoSessionConfig cfg = acceptor.getSessionConfig(); cfg.setReaderIdleTime(remotingServerConfig.getReaderIdleTimeSeconds()); cfg.setWriterIdleTime(remotingServerConfig.getWriterIdleTimeSeconds()); cfg.setBothIdleTime(remotingServerConfig.getServerChannelMaxIdleTimeSeconds()); bindAddress = new InetSocketAddress(remotingServerConfig.getListenPort()); try { acceptor.bind(bindAddress); } catch (IOException e) { throw new RemotingException("Start Mina server error", e); } }
@Override protected void serverStart() throws RemotingException { acceptor = new NioSocketAcceptor(); //TCP Acceptor // acceptor.getFilterChain().addFirst("logging", new MinaLoggingFilter()); acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(getCodec()))); acceptor.getFilterChain().addLast("mdc", new MdcInjectionFilter()); acceptor.setHandler(new MinaHandler(this)); IoSessionConfig cfg = acceptor.getSessionConfig(); cfg.setReaderIdleTime(remotingServerConfig.getReaderIdleTimeSeconds()); cfg.setWriterIdleTime(remotingServerConfig.getWriterIdleTimeSeconds()); cfg.setBothIdleTime(remotingServerConfig.getServerChannelMaxIdleTimeSeconds()); bindAddress = new InetSocketAddress(remotingServerConfig.getListenPort()); try { acceptor.bind(bindAddress); } catch (IOException e) { throw new RemotingException("Start Mina server error", e); } }
public void start(int port) throws Exception { this.port = port; acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast("codec", new HttpServerCodec()); acceptor.setHandler(new HttpServerHandle()); acceptor.bind(new InetSocketAddress(port)); logger.error("---------> http server start at port:{}", port); }
public synchronized void open(InetSocketAddress address, MessageReceiverHandler handler, SystemEventListener systemEventListener) { if ( logger.isInfoEnabled() ){ logger.info("(" + Thread.currentThread().getId() + ")"+Thread.currentThread().getName() +" ### Binding a new SocketAcceptor to "+address.getHostName()+":"+address.getPort()); } if ( this.acceptor == null ) { acceptor = new NioSocketAcceptor( 16 ); acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new ObjectSerializationCodecFactory() ) ); acceptor.getSessionConfig().setReadBufferSize( 2048 ); acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 100 ); } this.handler = handler; acceptor.setHandler( new MinaIoHandler( systemEventListener, handler ) ); try { acceptor.bind( address ); } catch ( IOException e ) { throw new RuntimeException( "(" + Thread.currentThread().getId() + ")"+Thread.currentThread().getName() +"Unable to bind Mina Acceptor", e ); } }
acceptor.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset
public static void main(String[] args) throws IOException { IoAcceptor acceptor = new NioSocketAcceptor(); // acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, IDELTIMEOUT); acceptor.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.getFilterChain().addLast("myfliter", new MyFilter()); acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CommandCodecFactory("UTF-8"))); KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl(); KeepAliveFilter kaf = new KeepAliveFilter(kamfi, IdleStatus.BOTH_IDLE); /** 是否回发 */ kaf.setForwardEvent(true); acceptor.getFilterChain().addLast("heart", kaf); acceptor.setHandler(new CalculatorHandler()); acceptor.bind(new InetSocketAddress(PORT)); log.debug("socket通信服务端已启动,端口是" + PORT); }
public void start() throws Exception { acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast("codec", new HttpServerCodec()); acceptor.setHandler(new HttpServerHandle()); acceptor.bind(new InetSocketAddress(port)); }
this.acceptor.getFilterChain().addLast("ssl", sslFilter); textLine.setEncoderMaxLineLength(Integer.MAX_VALUE); ProtocolCodecFilter codecFilter = new ProtocolCodecFilter(textLine); this.acceptor.getFilterChain().addLast( "codec", codecFilter); this.acceptor.getFilterChain().addLast("threadPool", new ExecutorFilter()); this.acceptor.getFilterChain().addLast("payloadFilter", this.payloadFilter); this.acceptor.getFilterChain().addLast("requestFilter", this.requestFilter); this.acceptor.getFilterChain().addLast("transactionFilter", this.transactionFilter); this.acceptor.getFilterChain().addLast("authenticationFilter", this.authenticationFilter);
private void initializeAndBindPlatypusAcceptor(InetSocketAddress s) throws IOException, Exception { //final SslFilter sslFilter = new SslFilter(sslContext); commented out until MINA Sslfilter bugs will be fixed ThreadPoolExecutor connectionsPollerExecutor = new ThreadPoolExecutor(1, 1, 3L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new PlatypusThreadFactory("nio-polling-", false)); final IoAcceptor acceptor = new NioSocketAcceptor(connectionsPollerExecutor, new NioProcessor(executor)); //acceptor.getFilterChain().addLast("encryption", sslFilter); commented out until MINA Sslfilter bugs will be fixed acceptor.getFilterChain().addLast("platypusCodec", new ProtocolCodecFilter(new ResponseEncoder(), new RequestDecoder())); /* acceptor.getFilterChain().addLast("executor", new ExecutorFilter(executor, IoEventType.EXCEPTION_CAUGHT, IoEventType.MESSAGE_RECEIVED, IoEventType.MESSAGE_SENT, IoEventType.SESSION_CLOSED, IoEventType.SESSION_IDLE, IoEventType.CLOSE, IoEventType.WRITE)); */ PlatypusRequestsHandler handler = new PlatypusRequestsHandler(this); acceptor.setHandler(handler); Integer sessionIdleTime = portsSessionIdleTimeouts != null ? portsSessionIdleTimeouts.get(s.getPort()) : null; if (sessionIdleTime == null || sessionIdleTime == 0) { sessionIdleTime = PlatypusRequestsHandler.SESSION_TIME_OUT; } Integer sessionIdleCheckInterval = portsSessionIdleCheckIntervals != null ? portsSessionIdleCheckIntervals.get(s.getPort()) : null; if (sessionIdleCheckInterval == null || sessionIdleCheckInterval == 0) { sessionIdleCheckInterval = PlatypusRequestsHandler.IDLE_TIME_EVENT; } handler.setSessionIdleCheckInterval(sessionIdleCheckInterval); handler.setSessionIdleTime(sessionIdleTime); acceptor.bind(s); Logger.getLogger(ServerMain.class.getName()).log(Level.INFO, "Listening platypus protocol on {0}", s.toString()); }
@Override public void run() { acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); acceptor.setHandler(new MinaServerHandler()); acceptor.getSessionConfig().setReadBufferSize(mConfig.getReadBufferSize()); //10秒无操作释放Session acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10); try { acceptor.bind(new InetSocketAddress(mConfig.getPort())); } catch (IOException e) { e.printStackTrace(); } Log.d(TAG,acceptor.getLocalAddress().toString()); }
/** * Default constructor * * @param host * @param port */ public TcpIpServer(final String host, final int port) { this.host = host; this.port = port; // Initialize the acceptor this.acceptor.getFilterChain().addLast("logger", new LoggingFilter()); this.acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TcpIpTextLineEncoder(Charset.forName(ENCODING)), new TextLineDecoder(Charset.forName(ENCODING)) )); this.acceptor.getSessionConfig().setReadBufferSize(BUFFER_READER_SIZE); this.acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, IDLE_TIME); }
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
public void start() throws IOException { acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast( "logger", new LoggingFilter() ); acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new ObjectSerializationCodecFactory() ) ); acceptor.setHandler( handler ); acceptor.getSessionConfig().setReadBufferSize( 2048 ); acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 ); ((SocketSessionConfig) acceptor.getSessionConfig()).setSoLinger(0); acceptor.bind( new InetSocketAddress( localInterface, port ) ); running = true; }
/** * 初始化tcp服务器 * * @param ip * 监听ip 0.0.0.0最好 * @param port * 监听端口 * @param HandlerClass * 继承MinaHandler的类 * @param useTcpMonitor * 是否输出监控日志 * @param log * 日志可以为null * @throws Throwable */ public static void init(String ip, int port, Class<?> HandlerClass, boolean useTcpMonitor, ILog log) throws Exception { MinaConfig.USE_TCP_MONITOR = useTcpMonitor; MinaConfig.log = log; // 初始化 ioAcceptor = new NioSocketAcceptor(); // 设置编码解码 ioAcceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaEncoder(), new MinaDecoder())); // 设置处理函数实现IoHandler接口 MinaHandler minaHandler = (MinaHandler) HandlerClass.newInstance(); ioAcceptor.setHandler(minaHandler); // 绑定ip地址与端口 ioAcceptor.bind(new InetSocketAddress(ip, port)); } }
/** * <p>startServer</p> * * @throws java.lang.Exception if any. */ public void startServer() throws Exception { m_acceptor = new NioSocketAcceptor(); m_acceptor.getFilterChain().addLast("logger", new LoggingFilter()); m_acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(StandardCharsets.UTF_8))); m_acceptor.setHandler(getServerHandler()); m_acceptor.getSessionConfig().setReadBufferSize(getBufferSize()); m_acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, getIdleTime()); ((NioSocketAcceptor) m_acceptor).setReuseAddress(true); m_acceptor.bind(new InetSocketAddress(getPort())); }
@Override protected void serverStart() throws RemotingException { acceptor = new NioSocketAcceptor(); //TCP Acceptor // acceptor.getFilterChain().addFirst("logging", new MinaLoggingFilter()); acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(getCodec()))); acceptor.getFilterChain().addLast("mdc", new MdcInjectionFilter()); acceptor.setHandler(new MinaHandler(this)); IoSessionConfig cfg = acceptor.getSessionConfig(); cfg.setReaderIdleTime(remotingServerConfig.getReaderIdleTimeSeconds()); cfg.setWriterIdleTime(remotingServerConfig.getWriterIdleTimeSeconds()); cfg.setBothIdleTime(remotingServerConfig.getServerChannelMaxIdleTimeSeconds()); bindAddress = new InetSocketAddress(remotingServerConfig.getListenPort()); try { acceptor.bind(bindAddress); } catch (IOException e) { throw new RemotingException("Start Mina server error", e); } }