package com.hazelcast.remotecontroller;

import org.apache.logging.log4j.LogManager;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.ServerContext;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServerEventHandler;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransport;

public class Main {

    public static org.apache.logging.log4j.Logger LOG = LogManager.getLogger(Main.class);

    public static int PORT = 9701;

    public static RemoteControllerHandler handler;

    public static RemoteController.Processor processor;

    public static void main(String[] args) {
        try {
            handler = new RemoteControllerHandler();
            processor = new RemoteController.Processor(handler);

            Runnable simple = () -> {
                try {
                    TServerTransport serverTransport = new TServerSocket(PORT);
                    TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));

                    LOG.info("Starting Remote Controller Server on port:" + PORT);

                    //Set server event handler
                    server.setServerEventHandler(new ServerEventHandler(handler));
                    server.serve();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };

            new Thread(simple).start();
        } catch (Exception x) {
            x.printStackTrace();
        }
    }

    private static class ServerEventHandler implements TServerEventHandler {

        private RemoteControllerHandler handler;

        private Object mutex = new Object();

        public ServerEventHandler(RemoteControllerHandler handler) {

            this.handler = handler;
        }

        public void preServe() {
            LOG.info("TServerEventHandler.preServe - server starts accepting connections");
        }

        public ServerContext createContext(TProtocol input, TProtocol output) {
            synchronized (mutex){
                LOG.info("TServerEventHandler.createContext - new client context created");
                return null;
            }
        }

        public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
            synchronized (mutex) {
                try {
                    this.handler.clean();
                } catch (TException e) {
                    LOG.error(e.getMessage());
                }
                LOG.info("TServerEventHandler.deleteContext client");
            }
        }

        public void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport) {
        }

    }

}

@@ -297,24 +297,71 @@
 entHandler;%0A
+import org.apache.thrift.server.TSimpleServer;%0A
 import org.a
@@ -1163,34 +1163,30 @@
 rver = new T
-ThreadPool
+Simple
 Server(new T
@@ -1185,26 +1185,22 @@
 er(new T
-ThreadPool
+Simple
 Server.A
@@ -1863,54 +1863,8 @@
 r;%0A%0A
-        private Object mutex = new Object();%0A%0A
     
@@ -2196,46 +2196,8 @@
 ) %7B%0A
-            synchronized (mutex)%7B%0A    
     
@@ -2292,20 +2292,16 @@
         
-    
 return n
@@ -2305,30 +2305,16 @@
 n null;%0A
-            %7D%0A
         
@@ -2420,47 +2420,8 @@
 ) %7B%0A
-            synchronized (mutex) %7B%0A    
     
@@ -2442,36 +2442,32 @@
                 
-  
-  
 this.handler.cle
@@ -2468,28 +2468,24 @@
 er.clean();%0A
-    
             
@@ -2521,28 +2521,24 @@
             
-  
-  
 LOG.error(e.
@@ -2560,32 +2560,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -2570,34 +2570,32 @@
   %7D%0A            
-  
 LOG.info(%22TServe
@@ -2632,30 +2632,16 @@
 ient%22);%0A
-            %7D%0A
         

use TSimpleServer instead of thread pool