1

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.

FiokoSoft
  • 67
  • 1
  • 10
  • You could make your client asynchronous and solve your actual problem. But if you insist on having a two-way service (e.g. the request is queued and/or the response takes a really long time), publish notification services in the client to receive the result of the server's operations, and implement a way to correlate response notifications to the previously sent requests. In this scenario, both ends are technically clients and servers, but we keep calling client to the initiator. – acelent Mar 16 '17 at 13:20
  • Thank you for your attention to my problem. In terms of the two solutions that you are proposing to me, I have implemented the first one. The second will be the subject of another iteration of the project, this iteration will constitute the research and the development – FiokoSoft Mar 21 '17 at 15:31

1 Answers1

0

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
Adonis
  • 4,670
  • 3
  • 37
  • 57
  • @FiokoSoft No problem, please remember to tag an answer as being the answer to mark the issue as solved. – Adonis Mar 21 '17 at 15:20