summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/BroadcastRunner.java
blob: 45935b6c93cf0cb6d459323ed08e7c0bbbb2b367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.orbekk.same;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class BroadcastRunner {
    private ConnectionManager connections;
    private Executor executor;

    /**
     * Get a BroadcastRunner for ClientService using a thread pool of size 20.
     */
    public static BroadcastRunner getDefaultBroadcastRunner() {
        return new BroadcastRunner(Executors.newFixedThreadPool(20));
    }
    
    public BroadcastRunner(Executor executor, ConnectionManager connections) {
        this.executor = executor;
    }

    public synchronized void broadcast(final List<String> targets,
            final ServiceOperation operation) {
        for (String t : targets) {
            final ClientService client = connections.getConnection(t);
            executor.execute(new Runnable() {
                @Override public void run() {
                    operation.run(client);
                }
            });
        }
    }
}