Skip to content

Instantly share code, notes, and snippets.

View vhutov's full-sized avatar

Vladyslav Hutov vhutov

View GitHub Profile
@vhutov
vhutov / Parent.scala
Last active June 23, 2020 22:27
First implementation
import io.circe.{Encoder, Decoder}
import cats.effect.Concurrent
import cats.syntax.either._
case class JmsMessage[T](value: T, metadata: Metadata)
class JmsParentController[
F[_] : Concurrent,
Request : Encoder,
SuccessResponse : Decoder,
@vhutov
vhutov / Parent.scala
Created June 21, 2020 14:17
ParentService definition
trait ParentService[F[_], Request, SuccessResponse, FailureResponse] { ... }
javaOptions in run ++= {
getWeaver.value.toSeq.map("-javaagent:" + _)
}
fork in run := true
def getWeaver = Def.task {
update.value.matching {
moduleFilter(organization = "org.aspectj", name = "aspectjweaver") &&
artifactFilter(`type` = "jar")
}.headOption
}
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<include within="com.github..*"/>
<include within="scala.concurrent..*"/>
<include within="cats.effect..*"/>
</weaver>
<aspects>
@Aspect
class ExecutorAspect extends ContextPopulationWrapper {
@Pointcut(value = "call( * java.util.concurrent.Executor+.execute(Runnable))")
def executePointcut(): Unit = ()
@Around(value = "executePointcut()", argNames = "jp")
def executeHandle(jp: ProceedingJoinPoint): Object = {
handleRunnableArgument(jp)
}
object ConcurrentNoAccessToExecutor extends IOApp {
def part1(text: String, id: String): IO[String] = {
for {
_ <- log.info(s"start part1 [static id = $id]")
result <- IO(text)
_ <- log.info(s"end part1 [static id = $id]")
} yield result
}
class ThirdParty(executor: Executor) {
def access(id: String, cb: Either[Throwable, String] => Unit): Unit = {
executor.execute { () =>
executor.execute { () =>
cb(Right(id))
}
}
}
}
object Concurrent extends IOApp {
override protected implicit def contextShift: ContextShift[IO] = IO.contextShift(
ExecutionContext.global.wrapLogging
)
override protected implicit def timer: Timer[IO] = IO.timer(
ExecutionContext.global.wrapLogging,
Executors.newScheduledThreadPool(2).wrapLogging
)
class LoggingExecutionContext(ec: ExecutionContext) extends ExecutionContext {
override def execute(runnable: Runnable): Unit = {
val context = LoggingContext.localContext.value
ec.execute(new WrappedRunnable(runnable, context))
}
override def reportFailure(cause: Throwable): Unit = ec.reportFailure(cause)
}