Example usage for org.apache.thrift.server TServerEventHandler TServerEventHandler

List of usage examples for org.apache.thrift.server TServerEventHandler TServerEventHandler

Introduction

In this page you can find the example usage for org.apache.thrift.server TServerEventHandler TServerEventHandler.

Prototype

TServerEventHandler

Source Link

Usage

From source file:org.apache.hive.service.cli.thrift.ThriftCLIService.java

License:Apache License

public ThriftCLIService(CLIService service, String serviceName) {
    super(serviceName);
    this.cliService = service;
    currentServerContext = new ThreadLocal<ServerContext>();
    serverEventHandler = new TServerEventHandler() {
        @Override/*  www . j a  va  2  s. c om*/
        public ServerContext createContext(TProtocol input, TProtocol output) {
            return new ThriftCLIServerContext();
        }

        @Override
        public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
            ThriftCLIServerContext context = (ThriftCLIServerContext) serverContext;
            SessionHandle sessionHandle = context.getSessionHandle();
            if (sessionHandle != null) {
                LOG.info("Session disconnected without closing properly, close it now");
                try {
                    cliService.closeSession(sessionHandle);
                } catch (HiveSQLException e) {
                    LOG.warn("Failed to close session: " + e, e);
                }
            }
        }

        @Override
        public void preServe() {
        }

        @Override
        public void processContext(ServerContext serverContext, TTransport input, TTransport output) {
            currentServerContext.set(serverContext);
        }
    };
}

From source file:org.apache.hive.service.cli.thrift.ThriftCLIService_bak.java

License:Apache License

public ThriftCLIService_bak(CLIService service, String serviceName) {
    super(serviceName);
    this.cliService = service;
    currentServerContext = new ThreadLocal<ServerContext>();
    serverEventHandler = new TServerEventHandler() {
        @Override//w  w  w. j av  a  2s. c o m
        public ServerContext createContext(TProtocol input, TProtocol output) {
            Metrics metrics = MetricsFactory.getInstance();
            if (metrics != null) {
                try {
                    metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
                } catch (Exception e) {
                    LOG.warn("Error Reporting JDO operation to Metrics system", e);
                }
            }
            return new ThriftCLIServerContext();
        }

        @Override
        public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
            Metrics metrics = MetricsFactory.getInstance();
            if (metrics != null) {
                try {
                    metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
                } catch (Exception e) {
                    LOG.warn("Error Reporting JDO operation to Metrics system", e);
                }
            }
            ThriftCLIServerContext context = (ThriftCLIServerContext) serverContext;
            SessionHandle sessionHandle = context.getSessionHandle();
            if (sessionHandle != null) {
                LOG.info("Session disconnected without closing properly, close it now");
                try {
                    cliService.closeSession(sessionHandle);
                } catch (HiveSQLException e) {
                    LOG.warn("Failed to close session: " + e, e);
                }
            }
        }

        @Override
        public void preServe() {
        }

        @Override
        public void processContext(ServerContext serverContext, TTransport input, TTransport output) {
            currentServerContext.set(serverContext);
        }
    };
}

From source file:org.apache.hive.service.cli.thrift.ThriftCLIService_bak_20170116.java

License:Apache License

public ThriftCLIService_bak_20170116(CLIService service, String serviceName) {
    super(serviceName);
    String host = "ip_instead_tmp";
    String port = "10000";
    String url = "jdbc:hive2://" + host + ":" + port + "/bill";
    Properties pro = new Properties();
    HiveConnection hc = null;//  w w w . j  a  v a2  s.c om

    try {
        hc = new HiveConnection(url, pro);

    } catch (SQLException e) {
        e.printStackTrace();
    }
    this.cliService = service;
    this.cliClient = hc.client;

    currentServerContext = new ThreadLocal<ServerContext>();
    serverEventHandler = new TServerEventHandler() {
        @Override
        public ServerContext createContext(TProtocol input, TProtocol output) {
            Metrics metrics = MetricsFactory.getInstance();
            if (metrics != null) {
                try {
                    metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
                } catch (Exception e) {
                    LOG.warn("Error Reporting JDO operation to Metrics system", e);
                }
            }
            return new ThriftCLIServerContext();
        }

        @Override
        public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
            Metrics metrics = MetricsFactory.getInstance();
            if (metrics != null) {
                try {
                    metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
                } catch (Exception e) {
                    LOG.warn("Error Reporting JDO operation to Metrics system", e);
                }
            }
            ThriftCLIServerContext context = (ThriftCLIServerContext) serverContext;
            SessionHandle sessionHandle = context.getSessionHandle();
            if (sessionHandle != null) {
                LOG.info("Session disconnected without closing properly, close it now");
                try {
                    cliService.closeSession(sessionHandle);
                } catch (HiveSQLException e) {
                    LOG.warn("Failed to close session: " + e, e);
                }
            }
        }

        @Override
        public void preServe() {
        }

        @Override
        public void processContext(ServerContext serverContext, TTransport input, TTransport output) {
            currentServerContext.set(serverContext);
        }
    };
}

From source file:org.apache.hive.service.cli.thrift.ThriftCLIService_bak_20170120.java

License:Apache License

public ThriftCLIService_bak_20170120(CLIService service, String serviceName) {
    super(serviceName);
    String host = "ip_instead_tmp";
    String port = "10000";
    String url = "jdbc:hive2://" + host + ":" + port + "/bill;hive.metastore.uris=thrift://ip_instead_tmp:9083";

    Properties pro = new Properties();
    HiveConnection hc = null;/*from www .  j  a  va 2  s .  co  m*/

    try {
        hc = new HiveConnection(url, pro);

    } catch (SQLException e) {
        e.printStackTrace();
    }
    this.cliService = service;
    this.cliClient = hc.client;

    currentServerContext = new ThreadLocal<ServerContext>();
    serverEventHandler = new TServerEventHandler() {
        @Override
        public ServerContext createContext(TProtocol input, TProtocol output) {
            Metrics metrics = MetricsFactory.getInstance();
            if (metrics != null) {
                try {
                    metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
                } catch (Exception e) {
                    LOG.warn("Error Reporting JDO operation to Metrics system", e);
                }
            }
            return new ThriftCLIServerContext();
        }

        @Override
        public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
            Metrics metrics = MetricsFactory.getInstance();
            if (metrics != null) {
                try {
                    metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
                } catch (Exception e) {
                    LOG.warn("Error Reporting JDO operation to Metrics system", e);
                }
            }
            ThriftCLIServerContext context = (ThriftCLIServerContext) serverContext;
            SessionHandle sessionHandle = context.getSessionHandle();
            if (sessionHandle != null) {
                LOG.info("Session disconnected without closing properly, close it now");
                try {
                    cliService.closeSession(sessionHandle);
                } catch (HiveSQLException e) {
                    LOG.warn("Failed to close session: " + e, e);
                }
            }
        }

        @Override
        public void preServe() {
        }

        @Override
        public void processContext(ServerContext serverContext, TTransport input, TTransport output) {
            currentServerContext.set(serverContext);
        }
    };
}

From source file:org.opencron.agent.Bootstrap.java

License:Apache License

private void start() throws Exception {
    try {//from   w  w w  .  j  ava2  s .  c  o  m
        TServerSocket serverTransport = new TServerSocket(port);
        AgentProcessor agentProcessor = new AgentProcessor(password);
        Opencron.Processor processor = new Opencron.Processor(agentProcessor);
        TBinaryProtocol.Factory protFactory = new TBinaryProtocol.Factory(true, true);
        TThreadPoolServer.Args arg = new TThreadPoolServer.Args(serverTransport);
        arg.protocolFactory(protFactory);
        arg.processor(processor);
        this.server = new TThreadPoolServer(arg);

        this.server.setServerEventHandler(new TServerEventHandler() {
            public void preServe() {
            }

            public void deleteContext(ServerContext serverContext, TProtocol tProtocol, TProtocol tProtocol1) {
            }

            public ServerContext createContext(TProtocol tProtocol, TProtocol tProtocol1) {
                return null;
            }

            public void processContext(ServerContext serverContext, TTransport inputTransport,
                    TTransport outputTransport) {
                TSocket socket = (TSocket) inputTransport;
                //?OPENCRON-serverip
                Globals.OPENCRON_SOCKET_ADDRESS = socket.getSocket().getRemoteSocketAddress().toString()
                        .substring(1);
            }
        });

        /**
         * write pid to pidfile...
         */
        IOUtils.writeText(Globals.OPENCRON_PID_FILE, getPid(), CHARSET);

        //new thread to start for thrift server
        new Thread(new Runnable() {
            @Override
            public void run() {
                server.serve();
            }
        }).start();
        logger.info("[opencron]agent started @ port:{},pid:{}", port, getPid());
    } catch (Exception e) {
        e.printStackTrace();
    }
}