0

I am implementing a producer consumer scenario using a BlockingQueue.

This is my consumers run method:

@Override
    public void run() {
        try {
            Message msg;
            System.out.println("Consumer started");
            //consuming messages until exit message is received

            while(!(msg=queue.take()).getMsg().equalsIgnoreCase("exit")) {

                //




            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

and I intend to call the below method when I get something out of the queue, but here is a condition. I want to run ony one isntance of this method. I dont want to consume from the queue till I get a return from the below method.

I have been struggling as in how do I contain this in the while loop.

public boolean runStrategy(String msg) {

        ExecutionContext executionContext = new ExecutionContext();

        String[] filePathArray = msg.split("/");

        String campaignForType = filePathArray[6];// .equals("Profiles")/events etc etc

        if (campaignForType.equalsIgnoreCase("Profiles")) {

            executionContext.setExecutionStrategy(new ProfileUploadExecutionStrategy());
            return executionContext.executeCampaign(filePathArray, msg);

        } else if (campaignForType.equalsIgnoreCase("Events")) {

           /* executionContext.setExecutionStrategy(new EventExecutionStrategy());
            executionContext.executeCampaign(filePathArray, msg.toString());*/

            return true;
        }

        return true;
    }
User3
  • 2,465
  • 8
  • 41
  • 84

1 Answers1

1

a pretty easy solution: start only one consumer thread.

You want at most one instance of the method running. Or are there other cases?. If there is only one thread, only one instance of the method can run. As long as that thread is busy, there are no others who can consume from the queue.

markus
  • 511
  • 1
  • 4
  • 15