Guest User

Untitled

a guest
Jul 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. import org.scalatest.matchers.MustMatchers
  2.  
  3. import scalaz._
  4. import scalaz.camel._
  5.  
  6. object RouterConfig {
  7. import org.apache.camel.impl.DefaultCamelContext
  8.  
  9. val context = new DefaultCamelContext
  10. val template = context.createProducerTemplate
  11.  
  12. implicit val router = new Router(context)
  13. }
  14.  
  15. object RecipientListDemo extends Application with MustMatchers {
  16. import concurrent.Strategy
  17. import Scalaz._
  18. import Camel._
  19. import RouterConfig._
  20.  
  21. router.start
  22.  
  23. // some message processors
  24. def append(s: String) = (msg: Message) => msg.appendBody(s)
  25. def delay(ms: Long) = (msg: Message) => { Thread sleep ms; msg }
  26.  
  27. // function to combine results from independent computations
  28. val aggregator = (m1: Message, m2: Message) => m1.appendBody(" - %s" format m2.body)
  29.  
  30. // creates an example route consuming from endpoint defined by uri
  31. def route(uri: String)(implicit s: Strategy) =
  32. from(uri) route {
  33. append("-begin") >=> multicast (
  34. delay(3000) >=> append("-1"),
  35. delay(3000) >=> append("-2"),
  36. delay(3000) >=> append("-3")
  37. ) (aggregator) >=> append("-end")
  38. }
  39.  
  40. {
  41. // strategy causing a parallel multicast where each
  42. // computation is run in a separate, newly created
  43. // thread
  44. import concurrent.Strategy.Naive
  45.  
  46. // create route consuming from direct:test-1
  47. route("direct:test-1")
  48.  
  49. // takes about 3 seconds to complete (parallel multicast)
  50. template.requestBody("direct:test-1", "test") must
  51. equal("test-begin-1 - test-begin-2 - test-begin-3-end")
  52. }
  53.  
  54. {
  55. // strategy causing a sequential multicast where
  56. // each computation is run in the main thread
  57. import concurrent.Strategy.Sequential
  58.  
  59. // create route consuming from direct:test-2
  60. route("direct:test-2")
  61.  
  62. // takes about 9 (3x3) seconds to complete (sequential multicast)
  63. template.requestBody("direct:test-2", "test") must
  64. equal("test-begin-1 - test-begin-2 - test-begin-3-end")
  65. }
  66.  
  67. router.stop
  68. }
Add Comment
Please, Sign In to add comment