Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 2nd, 2012  |  syntax: None  |  size: 2.65 KB  |  views: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package org.apache.camel.processor;
  2.  
  3. import org.apache.camel.AsyncCallback;
  4. import org.apache.camel.ContextTestSupport;
  5. import org.apache.camel.Exchange;
  6. import org.apache.camel.builder.RouteBuilder;
  7. import org.apache.camel.component.mock.MockEndpoint;
  8. import org.apache.camel.processor.loadbalancer.LoadBalancerSupport;
  9.  
  10. public class CustomLoadBalanceTest extends ContextTestSupport {
  11.     protected MockEndpoint x;
  12.     protected MockEndpoint y;
  13.     protected MockEndpoint z;
  14.  
  15.     @Override
  16.     protected void setUp() throws Exception {
  17.         super.setUp();
  18.  
  19.         x = getMockEndpoint("mock:x");
  20.         y = getMockEndpoint("mock:y");
  21.         z = getMockEndpoint("mock:z");
  22.     }
  23.  
  24.     protected RouteBuilder createRouteBuilder() {
  25.         return new RouteBuilder() {
  26.             public void configure() {
  27.                 // START SNIPPET: e1
  28.                 from("direct:start")
  29.                     // using our custom load balancer
  30.                     .loadBalance(new MyLoadBalancer())
  31.                     .to("mock:x", "mock:y", "mock:z");
  32.                 // END SNIPPET: e1
  33.             }
  34.         };
  35.     }
  36.  
  37.     public void testCustomLoadBalancer() throws Exception {
  38.         x.expectedBodiesReceived("x", "x", "x");
  39.         y.expectedBodiesReceived("y", "y");
  40.         z.expectedBodiesReceived("foo", "bar", "baz");
  41.  
  42.         template.sendBody("direct:start", "x");
  43.         template.sendBody("direct:start", "y");
  44.         template.sendBody("direct:start", "foo");
  45.         template.sendBody("direct:start", "bar");
  46.         template.sendBody("direct:start", "y");
  47.         template.sendBody("direct:start", "x");
  48.         template.sendBody("direct:start", "x");
  49.         template.sendBody("direct:start", "baz");
  50.  
  51.         assertMockEndpointsSatisfied();
  52.     }
  53.  
  54.     // START SNIPPET: e2
  55.     public static class MyLoadBalancer extends LoadBalancerSupport {
  56.  
  57.         public boolean process(Exchange exchange, AsyncCallback callback) {
  58.             String body = exchange.getIn().getBody(String.class);
  59.             try {
  60.                 if ("x".equals(body)) {
  61.                     getProcessors().get(0).process(exchange);
  62.                 } else if ("y".equals(body)) {
  63.                     getProcessors().get(1).process(exchange);
  64.                 } else {
  65.                     getProcessors().get(2).process(exchange);
  66.                 }
  67.             } catch (Throwable e) {
  68.                 exchange.setException(e);
  69.             }
  70.             callback.done(true);
  71.             return true;
  72.         }
  73.     }
  74.     // END SNIPPET: e2
  75.  
  76. }
  77.        
  78. from("jetty://http://0.0.0.0:8080/test")
  79.   .loadBalance(new MyLoadBalancer())
  80.   .to("http://tcserver1:8080/hello","http://tcpserver2:8080/hello","http://tcpserver3:8080/hello");
clone this paste RAW Paste Data