diff options
Diffstat (limited to 'same/src/main/java/com/orbekk/same/BroadcasterImpl.java')
-rw-r--r-- | same/src/main/java/com/orbekk/same/BroadcasterImpl.java | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/BroadcasterImpl.java b/same/src/main/java/com/orbekk/same/BroadcasterImpl.java new file mode 100644 index 0000000..688a388 --- /dev/null +++ b/same/src/main/java/com/orbekk/same/BroadcasterImpl.java @@ -0,0 +1,36 @@ +package com.orbekk.same; + +import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +public class BroadcasterImpl { + private ConnectionManager connections; + private Executor executor; + + /** + * Get a BroadcastRunner for ClientService using a thread pool of size 20. + */ + public static BroadcasterImpl getDefaultBroadcastRunner() { + return new BroadcasterImpl(Executors.newFixedThreadPool(20), + new ConnectionManagerImpl()); + } + + public BroadcasterImpl(Executor executor, + ConnectionManager connections) { + this.connections = connections; + this.executor = executor; + } + + public synchronized void broadcast(final List<String> targets, + final ServiceOperation operation) { + for (String t : targets) { + final ClientService client = connections.getClient(t); + executor.execute(new Runnable() { + @Override public void run() { + operation.run(client); + } + }); + } + } +} |