Skip to content

Instantly share code, notes, and snippets.

@vhutov
Created March 12, 2018 22:48
Show Gist options
  • Save vhutov/ec70838f8408d6c7db1c0d95c162658f to your computer and use it in GitHub Desktop.
Save vhutov/ec70838f8408d6c7db1c0d95c162658f to your computer and use it in GitHub Desktop.
class GenericActionBuilder[V <: Message](
attributes: Attributes[V],
requestActionBuilder: RequestActionBuilder[V],
responseActionBuilder: ResponseActionBuilder
) extends ActionBuilder with LazyLogging {
override def build(ctx: ScenarioContext, next: Action): Action = {
import ctx._
val requestProtocolComponents = protocolComponentsRegistry.components(requestActionBuilder.key)
val responseProtocolComponents = protocolComponentsRegistry.components(responseActionBuilder.key)
val publisher = requestActionBuilder.build(ctx, requestProtocolComponents)
logger.debug(s"Publisher ${publisher.name} has been instantiated")
val consumer = responseActionBuilder.build(ctx, responseProtocolComponents)
logger.debug(s"Consumer ${consumer.name} has been instantiated")
system.registerOnTermination {
safe(publisher.close())
safe(consumer.close())
}
new GenericAction[V](
coreComponents,
attributes,
publisher,
consumer,
throttled,
next
)
}
private def safe(f: => Unit): Unit = {
var r: Unit = () //to prevent JIT from skipping execution of f
try {
r = f
} catch {case NonFatal(e) => e.printStackTrace()}
}
}
case class Attributes[V](
requestName: Expression[String],
payload: Expression[V]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment