Skip to content

Commit c577e57

Browse files
Merge pull request #1719 from sgeorgakis/master
[SCALA-499] IOApp in Cats Effect
2 parents 2cb6bd5 + 8958055 commit c577e57

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.scala.ioapp
2+
3+
import cats.effect.{ExitCode, IO, IOApp}
4+
5+
object ArgumentIOApp extends IOApp {
6+
7+
def run(args: List[String]): IO[ExitCode] = {
8+
if (args.nonEmpty) {
9+
IO.println(s"Running with args: ${args.mkString(",")}")
10+
.as(ExitCode.Success)
11+
} else {
12+
IO.println("No args provided. Aborting").as(ExitCode(2))
13+
}
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.scala.ioapp
2+
3+
import cats.effect._
4+
import cats.effect.unsafe.IORuntimeConfig
5+
6+
import java.util.concurrent.Executors
7+
import scala.concurrent.ExecutionContext
8+
9+
object ContextIOApp extends IOApp {
10+
11+
private val customExecutionContext: ExecutionContext =
12+
ExecutionContext.fromExecutor(Executors.newWorkStealingPool(1))
13+
14+
override def run(args: List[String]): IO[ExitCode] = {
15+
val computation = IO {
16+
println(s"Running on thread: ${Thread.currentThread().getName}")
17+
}
18+
19+
computation.evalOn(customExecutionContext).as(ExitCode.Success)
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.scala.ioapp
2+
3+
import cats.effect.{ExitCode, IO, IOApp, Resource}
4+
5+
import java.util.concurrent.Executors
6+
import scala.concurrent.ExecutionContext
7+
8+
object ResourceIOApp extends IOApp {
9+
10+
val customExecutionContext: Resource[IO, ExecutionContext] =
11+
Resource.make(
12+
IO(ExecutionContext.fromExecutorService(Executors.newWorkStealingPool(1)))
13+
) { ec =>
14+
IO.println("Shutting down execution contest").as(ec.shutdown())
15+
}
16+
17+
def run(args: List[String]): IO[ExitCode] = customExecutionContext
18+
.use { ec =>
19+
IO.println(s"Running on thread: ${Thread.currentThread().getName}")
20+
}
21+
.as(ExitCode.Success)
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.scala.ioapp
2+
3+
import cats.effect.{IO, IOApp}
4+
5+
object SimpleIOApp extends IOApp.Simple {
6+
val run: IO[Unit] = {
7+
IO.println("Running with simple app")
8+
}
9+
}

0 commit comments

Comments
 (0)