Menu

Invoke operation timeout

Help
2016-12-07
2017-01-20
  • Pedro Grilo Barroca

    Hi,
    I'm devolping an application that pretends to monitoring the network and pull some data from all devices, with a constant frequency.
    Due to do that I'm trying to create a "pulling client" that extends Thread class, and in the run method I'm iterating a list with all devices. This list is stored in a DefaultClient.
    I can get to the devices and the operations. I'm using the invoke method to call the operation and it works.
    This invoke method is bloking, and I need to configure a timeout to skip the non responding devices.
    Can anyone help me to understand which is the best way to do that?
    Maybe using the clienttimeout?

    BR,
    Pedro

     
  • Chris

    Chris - 2016-12-15

    Hi Pedro,
    a better way to monitor the network is by listening for hello and BYE messages. Also, you should use eventing to get the information from the devices.

    Regards,
    Chris

     
  • Pedro Grilo Barroca

    Hi Chris,

    Thanks for your help.

    I need to implement a network monitoring entity with several different behaviors.
    I'm already using the messages of hello and bye to update the list. In fact I already use the events to monitor the network, and wich devices are working or not.
    But I need to implement an interaction of request-reply with the devices. I can make the connection and if they are active and responsive the interaction works well, otherwise the method of invoke, blocks the entire process.
    I thought about using the java Executer or the ThreadPoolExecuter classes to perform this interaction.
    I just want to see if there is any easier or direct way to do so. For example, by using a timeout of the client or something like that.

    BR,
    Pedro

     
  • Chris

    Chris - 2017-01-03

    Hi Pedro,
    at the moment, we do not provide an easier way to set a timeout to an invoke operation. But we will consider adding one in the future. Thanks a lot for this suggestion!

    BR,
    Chris

     
  • Pedro Grilo Barroca

    Hi Cris,

    I came with a solution some time ago, and I'll let it here to anyone who want it.

    I'm using the Executers and Timer classes to manage the solution.

    package testingproject;
    
    import static java.lang.Math.random;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.LinkedList;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    class parameters{
        static int numberOfDevices = 50;
        static int frequency = 10000; //milliseconds
        static double failurePercentage = 50;
    }
    
    /**
     *
     * @author pedro
     */
    public class TestingProject {
    
        public static void main(String[] args) {
    
            TestingProjectTimer tpt = new TestingProjectTimer(parameters.numberOfDevices, parameters.frequency); //time in milliseconds
            tpt.startRequesting();
        }
    }
    
    class TestingProjectTimer {
    
        public LinkedList<String> results;
        ExecutorService executor;
        int numberOfThreads;
        long granularity;
        long initialTime;
        DateFormat df = new SimpleDateFormat("mm:ss:SSS");
        Timer timer;
    
        public TestingProjectTimer(int number, int granularity) {
    
            this.numberOfThreads = number;
            this.granularity = granularity;
            this.results = new LinkedList<>();
            this.timer = new Timer();    
        }
    
        void startRequesting(){
    
            ExecuterTimerTask mtt = new ExecuterTimerTask(this);
    
            timer.scheduleAtFixedRate(
                    mtt
                    , 0L
                    , granularity);
        }
    
        void submitTasks(){
    
            this.results.clear();
    
            this.executor = Executors.newFixedThreadPool(this.numberOfThreads);
    
            this.initialTime = System.currentTimeMillis();        
            System.out.println("Start Time : "+ df.format(new Date(initialTime)));
    
            for (int i = 0; i < this.numberOfThreads; i++) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                    Logger.getLogger(TestingProjectTimer.class.getName()).log(Level.SEVERE, null, ex);
                }
    
                this.executor.submit(new runTask(this.results));
            }
        }
    
        void shutdownThreadPullExecuter(){
    
            this.executor.shutdownNow();
            System.out.println("Size: "+ this.results.size());
            System.out.println();
        }
    
    }
    
    class ExecuterTimerTask extends TimerTask{
    
        TestingProjectTimer tpt;
    
        ExecuterTimerTask(TestingProjectTimer tpt){
    
            this.tpt = tpt;
        }
    
        @Override
        public void run() {
            if(tpt.executor!= null)
                if(!tpt.executor.isShutdown()){
                    tpt.shutdownThreadPullExecuter();
                }
    
            tpt.submitTasks();
        }
    
    }
    
    class runTask implements Runnable {
    
        private LinkedList<String> result;
    
        public runTask(LinkedList<String> result) {
            this.result = result;
        }
    
        @Override
        public void run() {
    
            //operation.invoke();
    
            //the invoke operation is here instead of the code below
            //######################################################################################################
            //this code is only to test if the block threads were blocking all the program        
            //######################################################################################################
            long Tid = Thread.currentThread().getId();
    
            if (Tid % 10 == 5) {
                try {
                    long time28 = (long) (random() * parameters.frequency) + (long)((parameters.failurePercentage/100)*parameters.frequency);
                    System.out.println("Wait: " + time28 + (time28>parameters.frequency?" -> Failure":""));
                    Thread.sleep(time28);
                    this.result.add(Long.toString(Tid));
    
                } catch (InterruptedException ex) {
                    //the method blocked more than it should (not responding device)
                    return;
                }
            } else {
                this.result.add(Long.toString(Tid));
            }
            //######################################################################################################
        }
    }
    

    Best Regards
    Pedro

     

Log in to post a comment.