3

I was trying to find a solution to run different task in different threads (depends/independents)

I have scenario where I need to run one task (which internally runs a server) in different thread before running another task (test, depends on above server) in gradle, after 2nd task completed I need to kill first task.

Again, same as above scenario, run another set of server/test/kill tasks.

task exp{
   doFirst{
      run1stServerTask.execute()
   }

   def pool = Executors.newFixedThreadPool(5)
   try {
      def defer = { closure -> pool.submit(closure as Callable) }

      defer {
        run1stTest.execute()
        // After tests are finished, kill 1st server tasks
      }
      defer {
        run2ndServerTask.execute()
      }
      defer {
        run2ndTest.execute()
       // After tests are finished, kill 2nd server tasks
      }

    }
    finally {
      pool.shutdown()
     }
}

Hope, All above make sense... I am open for another approach if its possible in build.gradle.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
Chetan
  • 172
  • 1
  • 12
  • what kind of server are you starting with run1stServerTask ? usually servers have a daemon mode you can start them with. then they are started but not blocking. that's how you can do it at least with the jetty plugin. In general you should avoid executing tasks directly by calling `Task.execute()` instead use dependsOn / finalizedBy – Rene Groeschke Jan 09 '16 at 00:06
  • Yeah, I read that we should not use execute, I am too trying to avoid it. I am running js server, let me see if it has daemon mode. I tried running using dependsOn but it blocked other tasks. I need to see what does finalizedBy offer. – Chetan Jan 09 '16 at 00:59
  • Is the problem solved finally? Do you have an example that shows the problem? – Opal Jan 10 '16 at 15:11
  • No @Opal, not yet... Still looking for answer. – Chetan Jan 11 '16 at 23:05
  • Just a rough sketch: `run1stServerTask` could _spawn_ the server by starting some `main` class in a new, globally defined `Thread`. Then all tasks `runTestX` depend on `run1stServerTask`. Whatever your combination of test tasks would be - gradle would start the server first. If you then defined your `Thread` with `setDaemon(true)` your server would be killed when gradle has finished all tests. – Michael Schaefers Jan 30 '16 at 21:12
  • Have you looked at `Thread.start` method in Gradle? – IgorGanapolsky Sep 22 '16 at 14:18

0 Answers0