Using ThreadPoolExecutor and DiscardPolicy

I need to make a client queue with ThreadPoolExecutor and the ability to drop clients if it exceeds a certain number (e.g. 5). This is a kind of DDOS protection. When client # 6 requests my server, it crashes, etc. I got my server and client code, but I do not know how to implement ThreadPoolExecutor and DiscardPolicy. Ideas or examples?

Simple server:

   import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Semaphore;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Server {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        ServerSocket server = new ServerSocket (3000);

        ExecutorService es = Executors.newFixedThreadPool(2);

        Semaphore semaphore = new Semaphore (2);

        while(true){

        semaphore.acquire();

        Socket accept2 = server.accept();

        es.execute(()->{
            try (Socket accept = accept2) {
            serve(accept);
            } catch (Exception exception) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, exception);
            } 
            finally {
                semaphore.release();

            }
        });        

        }

    }

    private static void serve(final Socket accept) throws ClassNotFoundException, IOException {
        InputStream inputStream = accept.getInputStream();
        OutputStream outputStream = accept.getOutputStream();

        ObjectInputStream inputStream2 = new ObjectInputStream (inputStream);

        while (true){
            Object readObject = inputStream2.readObject();
            System.out.println(readObject);
        }

        }

    }

And a simple client:

  import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;

    public class Client {

    public static void main(String[] args) throws IOException, InterruptedException {
        Socket socket = new Socket ("localhost", 3000);
        ObjectOutputStream oos = new ObjectOutputStream (
                socket.getOutputStream());
        oos.writeObject("First!");
        Thread.sleep(10000);
        oos.writeObject("First again!");
        Thread.sleep(10000);
        oos.writeObject("First again again!");

        }

    }
+1
source share
1 answer

Use ThreadPoolExecutorwith DiscardPolicyas below:

  int poolSize=1;
  int maxPoolSize=2;
  int queueSize=5;
  long aliveTive=1000;
  ArrayBlockingQueue<Runnable> queue= new ArrayBlockingQueue<Runnable>(queueSize);
  ThreadPoolExecutor executor= new ThreadPoolExecutor(poolSize,maxPoolSize,aliveTive,
                    TimeUnit.MILLISECONDS,queue,new ThreadPoolExecutor.DiscardPolicy());
}

Rejected Tasks:

, (Runnable), , Executor , Executor , .

execute RejectedExecutionHandler.rejectedExecution(Runnable, ThreadPoolExecutor) RejectedExecutionHandler.

:

  • ThreadPoolExecutor.AbortPolicy ExjectExceptionException .
  • ThreadPoolExecutor.CallerRunsPolicy , , . , .
  • ThreadPoolExecutor.DiscardPolicy , , .
  • ThreadPoolExecutor.DiscardOldestPolicy, , , ( , .)

0

Source: https://habr.com/ru/post/1657863/


All Articles