Richie
Richie

Reputation: 5199

Executor Service invokeAll

I am fairly new to the callable interface. I have some code which I can't get to compile at the moment and just need some help on why....

public List<String> getNonPingableRegisters (Collection<RegisterReplicationSynchTime> nonReplicatingRegisters) throws IOException {

    int nThreads = 15;
    final ExecutorService es = Executors.newFixedThreadPool(nThreads);

    Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());
    for (RegisterReplicationSynchTime nonReplicatingRegister : nonReplicatingRegisters) {
        pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));
    }

    List<Future<String>> taskResults = es.invokeAll(pingTasks);
    List<String> results = new ArrayList<String>();

    for (Future<String> taskResult : taskResults) {
        try {
            String output = taskResult.get();
            if (StringUtils.isNotEmpty(output) ) {
                results.add(output);
            }
        } catch (InterruptedException e) {
            // handle accordingly
        } catch (ExecutionException e) {
            // handle accordingly          
        }
    }
    return results;
}

Where PingTask is ...

public class PingTask implements Callable<String> {

    private String hostname = null;

    public PingTask(String hostname) {
        this.hostname = hostname;
    }

        public String call() {
            Socket socket = null;
            boolean reachable = false;  
            try {                           
                socket = new Socket();
                socket.connect(new InetSocketAddress(hostname, 139), 1000); //1 sec timeout                     
                reachable = true;
                socket.close();
            } catch (IOException e) {

            }
            finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {

                    }
                }
            }       
            return (reachable ? "" : hostname);
        }

    }

The compile error is at ...

List<Future<String>> taskResults = es.invokeAll(pingTasks);

The method add(Callable) in the type Collection> is not applicable for the arguments (PingTask) StoreReplicationSynchtimeManagerImpl.java

Not sure what I need to do here to make the call to invokeAll. Would appreciate some help.

thanks

Upvotes: 0

Views: 183

Answers (3)

Ravindra babu
Ravindra babu

Reputation: 38910

There is a type mis-match.

 Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>

But PingTask is declared as

public class PingTask implements Callable<String>

Change collection as Collection<PingTask>

pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));

will cause compile time error due to Callable<String> type addition

Upvotes: 0

Mateusz Mrozewski
Mateusz Mrozewski

Reputation: 2191

Error is not at that line. It's at:

pingTasks.add(new PingTask(nonReplicatingRegister.getRegisterName()));

Your collection is of Callable where as your PingTask class implements Callable. Change the collection to:

Collection<Callable<String>>

Upvotes: 1

shmosel
shmosel

Reputation: 50716

Here's your mistake:

Collection<Callable<PingTask>> pingTasks = new ArrayList<Callable<PingTask>>(nonReplicatingRegisters.size());

PingTask implements Callable<String>, not Callable<PingTask>. You need to declare your list as Collection<PingTask> or Collection<Callable<String>>.

Upvotes: 0

Related Questions