javamultithreadingrestreactive-programmingsendasynchronousrequest

Technologies and design for asynchronous processes in java


I am currently developing an application under a Restfull architecture with Java. I encountered a very big problem during the interactions between my web rest service, another web service soap and the android client.

After several tests, I realized that the client sometimes returned the failure of an operation while it was still being processed and the same for the consumption of service between the two services mentioned above, simply because That case calls are blocking.

To solve this problem, I would to proceed like this: whan a client sends an operation to a server, the server immediately responds that the request is taken about. Then the customer will have to test the status of his operation each time until that it is good it notifies the user thus.

Now my concern is that I would create a thread only for that; But I can not find a good design and technology for that ... Thank you in advance for your help.


Solution

  • You would to listen to calls from the client polling the status (on one of the server's endpoint), and have your consumer threads implement a getStatus method, while updating the status inside the run method. About the consumer threads, a crude implementation would look like that:

    public class ConsumerThread implements Runnable{
        private int status = 0;
        private Random rand = new Random();
        private final CountDownLatch startSignal;
    
        public ConsumerThread(CountDownLatch latch){
            this.startSignal = latch;
        }
    
        public int getStatus() {
            return status;
        }
    
        private void setStatus(int status) {
            this.status = status;
        }
    
        public void run() {
            try {
                this.startSignal.await();
                while (true){
                    this.setStatus(rand.nextInt(10));
                }
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }       
        }
    }
    

    Then trying a simple main method (I implemented a CountDownLatch to have all my threads starting at the same time, it's not mandatory):

    public class ThreadMain{
        private static List<ConsumerThread> consumers = new ArrayList<ConsumerThread>();
    
        public static void main(String[] args) {
            int NUM_THREAD = 15;
            ExecutorService executor = Executors.newFixedThreadPool(NUM_THREAD);
    
            CountDownLatch latch = new CountDownLatch(NUM_THREAD);
            ConsumerThread buffer = new ConsumerThread(latch);
            for (int i = 0; i < NUM_THREAD; i++){
                consumers.add(buffer);
                executor.execute(buffer);
                latch.countDown();
                buffer = new ConsumerThread(latch);
            }
    
            for (int i = 0; i < 100; i++){
                System.out.println("Status for Thread 0: " + getStatusId(0));
                System.out.println("Status for Thread 14: " + getStatusId(14));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static int getStatusId(int index){
            return consumers.get(index).getStatus();
        }
    }
    

    Sample output:

    Status for Thread 0: 5
    Status for Thread 14: 0
    Status for Thread 0: 7
    Status for Thread 14: 2
    Status for Thread 0: 7
    Status for Thread 14: 4
    Status for Thread 0: 6
    Status for Thread 14: 3