Advertisement
Guest User

Untitled

a guest
Mar 24th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.47 KB | None | 0 0
  1.  
  2. import akka.actor._
  3. import akka.pattern.{AskTimeoutException, ask}
  4. import akka.util.Timeout
  5. import com.ctilogic.cvd.common.aux.StackTraceHelper
  6. import com.ctilogic.cvd.common.messaging.{ErrorResponse, InternalRequest}
  7. import com.github.levkhomich.akka.tracing.{ActorTracing, TracingSupport}
  8. import scala.concurrent.{Future, ExecutionContext}
  9. import scala.util.control.ControlThrowable
  10.  
  11.  
  12.  
  13. /**
  14.  * This trait provides inheritor ability to communicate with services in distributed by cluster using `askService`
  15.  * and `tellService` methods.
  16.  */
  17. trait DistributedServicesSupport { this: Actor with ActorTracing ⇒
  18.     val apiRouter = CommonActorSelections.apiRouterSelection(context)
  19.  
  20.     /**
  21.      * This method sends the `request` using the ask pattern to the service on one of nodes with `role`.
  22.      *
  23.      * This method provides request tracing.
  24.      *
  25.      * @param role the role of the node that should process the request.
  26.      * @param request the request message.
  27.      * @param parentRequest parent request should be provided, if the `request` generated by other request, for example
  28.      *                      by REST request.
  29.      * @param t ask timeout
  30.      * @param ec ask executionContext
  31.      * @return future with response.
  32.      */
  33.     def askService(role: NodeRole.Value, request: InternalRequest, parentRequest: TracingSupport)
  34.             (implicit t: Timeout, ec: ExecutionContext): Future[Any] = {
  35.         val future = apiRouter ? request.asChildOf(parentRequest)
  36.         future map {
  37.             case msg =>
  38.                 trace.record(request, "Ask service response: " + msg)
  39.                 msg
  40.         } recover {
  41.             case e: ControlThrowable =>
  42.                 throw e
  43.  
  44.             case e: AskTimeoutException =>
  45.                 trace.record(request, StackTraceHelper.format(e))
  46.                 ErrorResponse.ServiceUnavailable
  47.  
  48.             case e: Throwable =>
  49.                 trace.record(request, StackTraceHelper.format(e))
  50.                 ErrorResponse.InternalServerError("error asking " + role + " for " + request)
  51.         }
  52.     }
  53.  
  54.  
  55.     /**
  56.      * This method sends the `request` using tell to the appropriate service on one of nodes with `role`.
  57.      *
  58.      * This method provides request tracing.
  59.      *
  60.      * @param role the role of the node that should process the request.
  61.      * @param request the request message.
  62.      * @param parentRequest parent request should be provided, if the `request` generated by other request, for example
  63.      *                      by REST request.
  64.      */
  65.     def tellService(role: NodeRole.Value, request: InternalRequest, parentRequest: TracingSupport): Unit = {
  66.         apiRouter ! request.asChildOf(parentRequest)
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement