Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created August 28, 2011 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislewis/1176662 to your computer and use it in GitHub Desktop.
Save chrislewis/1176662 to your computer and use it in GitHub Desktop.
/* Real details omitted. */
val ssh = Ssh(Host("localhost"), UsernameAndPassword("xx", "xx"), Some("/xx/.ssh/known_hosts"))
/* Replicate some block as a set of Callable[A]s */
object Callable {
def replicate[A](count: Int)(block: => A) =
(1 to count).map(_ => new java.util.concurrent.Callable[A] {
def call() = block
})
}
import Callable.replicate
/* Nasty, but these will do. */
import scala.collection.JavaConversions._
def grepFoo = ssh("foo" |: "grep foo" :| identity)
def grepBaz = ssh("foo\nbar\nbaz" |: "grep bar" :| identity)
def echo = ssh("echo \"hello\" $RANDOM" :| identity)
/* After this line we'll have 9 callables using the same ssh instance. */
val sessionTasks = replicate(3)(grepFoo) ++ replicate(3)(grepBaz) ++ replicate(3)(echo)
val pool = java.util.concurrent.Executors.newFixedThreadPool(20)
pool.invokeAll(sessionTasks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment