Advertisement
Guest User

Untitled

a guest
Dec 11th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 KB | None | 0 0
  1. package testIntercept;
  2.  
  3. import org.apache.camel.*;
  4. import org.apache.camel.builder.RouteBuilder;
  5. import org.apache.camel.component.mock.MockComponent;
  6. import org.apache.camel.component.mock.MockEndpoint;
  7. import org.apache.camel.component.seda.SedaComponent;
  8. import org.apache.camel.component.seda.SedaEndpoint;
  9. import org.apache.camel.processor.aggregate.AggregationStrategy;
  10. import org.apache.camel.test.junit4.CamelTestSupport;
  11. import org.junit.Ignore;
  12. import org.junit.Test;
  13.  
  14. import java.util.ArrayList;
  15. import java.util.Map;
  16. import java.util.concurrent.LinkedBlockingQueue;
  17.  
  18. public class TestRemoveInvalidRecipient3 extends CamelTestSupport {
  19.  
  20. String directEndPointURI = "direct:myDirect";
  21. String finalEndpointURI = "mock:finalEndpoint";
  22. RecipientBean recipientBean = new RecipientBean();
  23.  
  24. @Override
  25. public boolean isUseRouteBuilder() {
  26. return false;
  27. }
  28.  
  29. @Test
  30. public void testIntegrationDynamicRecipient() throws Exception {
  31.  
  32. context.addRoutes(new RouteBuilder() {
  33. @Override
  34. public void configure() throws Exception {
  35.  
  36. from(directEndPointURI)
  37. .doTry()
  38. .recipientList(method(recipientBean, "getRecipientList"))
  39. .parallelProcessing()
  40. .ignoreInvalidEndpoints()
  41. .aggregationStrategy(new AggregationStrategy() {
  42. @Override
  43. public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
  44. System.out.println("EEEEEEEEEEEEEEEEEEEEEE");
  45. if (oldExchange == null) {
  46. newExchange.getIn().setHeader("count", 1);
  47. return newExchange;
  48. } else {
  49. int count = oldExchange.getIn().getHeader("count", Integer.class);
  50. oldExchange.getIn().setHeader("count", count + 1);
  51. return oldExchange;
  52. }
  53. }
  54. })
  55. .end()
  56. .doCatch(Exception.class)
  57. .process(new Processor() {
  58. @Override
  59. public void process(Exchange exchange) throws Exception {
  60. int a1 = exchange.getIn().getHeader("count", Integer.class);
  61. System.out.println("WE GOT : " + a1);
  62. }
  63. })
  64. .end().to(finalEndpointURI);
  65. }
  66. });
  67.  
  68. context.start();
  69.  
  70. MockEndpoint mockEndpoint = new MockEndpoint("mock:abc", new MockComponent());
  71. Endpoint sedaEndpoint = new SedaEndpoint("seda:mySeda", new SedaComponent(), new LinkedBlockingQueue<Exchange>());
  72. sedaEndpoint.setCamelContext(context);
  73.  
  74. recipientBean.addRecipient(mockEndpoint);
  75. recipientBean.addRecipient(sedaEndpoint);
  76.  
  77. // Do a simple go through the route
  78. getMockEndpoint(finalEndpointURI).expectedMessageCount(1);
  79. template.sendBody(directEndPointURI, "MY_MESSAGE");
  80. getMockEndpoint(finalEndpointURI).assertIsSatisfied();
  81.  
  82. // Make sure we have the 2 recipients.
  83. assertEquals(2, recipientBean.getRecipientList().length);
  84.  
  85. // Add the bad endpoint
  86. Endpoint fake = new MyFakeEndpoint();
  87. recipientBean.addRecipient(fake);
  88.  
  89. // Run the second test
  90. getMockEndpoint(finalEndpointURI).reset();
  91. getMockEndpoint(finalEndpointURI).expectedMessageCount(1);
  92. template.sendBody(directEndPointURI, "MY_MESSAGE2");
  93. getMockEndpoint(finalEndpointURI).assertIsSatisfied();
  94.  
  95. // The fake endpoint should have been deleted by the time finalEndpointURI is satisfied.
  96. assertEquals(2, recipientBean.getRecipientList().length);
  97. }
  98.  
  99. public class RecipientBean {
  100.  
  101. private ArrayList<Endpoint> recipientList = new ArrayList<Endpoint>();
  102.  
  103. public void addRecipient(Endpoint endpoint) {
  104. recipientList.add(endpoint);
  105. }
  106.  
  107. public void removeRecipient(Endpoint endpoint) {
  108. recipientList.remove(endpoint);
  109. }
  110.  
  111. public void removeRecipient(String uri) {
  112. Endpoint toDeleteEndpoint = null;
  113.  
  114. for (Endpoint endpoint : recipientList) {
  115. if (endpoint.getEndpointUri().equals(uri))
  116. toDeleteEndpoint = endpoint;
  117. }
  118. if (toDeleteEndpoint != null)
  119. recipientList.remove(toDeleteEndpoint);
  120. }
  121.  
  122. public Endpoint[] getRecipientList() {
  123. return recipientList.toArray(new Endpoint[0]);
  124. }
  125. }
  126.  
  127. public class MyFakeEndpoint implements Endpoint {
  128.  
  129. @Override
  130. public String getEndpointUri() {
  131. return "myEvilURI";
  132. }
  133.  
  134. @Override
  135. public EndpointConfiguration getEndpointConfiguration() {
  136. return null; //To change body of implemented methods use File | Settings | File Templates.
  137. }
  138.  
  139. @Override
  140. public String getEndpointKey() {
  141. return null; //To change body of implemented methods use File | Settings | File Templates.
  142. }
  143.  
  144. @Override
  145. public Exchange createExchange() {
  146. return null; //To change body of implemented methods use File | Settings | File Templates.
  147. }
  148.  
  149. @Override
  150. public Exchange createExchange(ExchangePattern pattern) {
  151. return null; //To change body of implemented methods use File | Settings | File Templates.
  152. }
  153.  
  154. @Override
  155. public Exchange createExchange(Exchange exchange) {
  156. return null; //To change body of implemented methods use File | Settings | File Templates.
  157. }
  158.  
  159. @Override
  160. public CamelContext getCamelContext() {
  161. return null; //To change body of implemented methods use File | Settings | File Templates.
  162. }
  163.  
  164. @Override
  165. public Producer createProducer() throws Exception {
  166. return null; //To change body of implemented methods use File | Settings | File Templates.
  167. }
  168.  
  169. @Override
  170. public Consumer createConsumer(Processor processor) throws Exception {
  171. return null; //To change body of implemented methods use File | Settings | File Templates.
  172. }
  173.  
  174. @Override
  175. public PollingConsumer createPollingConsumer() throws Exception {
  176. return null; //To change body of implemented methods use File | Settings | File Templates.
  177. }
  178.  
  179. @Override
  180. public void configureProperties(Map<String, Object> options) {
  181. //To change body of implemented methods use File | Settings | File Templates.
  182. }
  183.  
  184. @Override
  185. public void setCamelContext(CamelContext context) {
  186. //To change body of implemented methods use File | Settings | File Templates.
  187. }
  188.  
  189. @Override
  190. public boolean isLenientProperties() {
  191. return false; //To change body of implemented methods use File | Settings | File Templates.
  192. }
  193.  
  194. @Override
  195. public boolean isSingleton() {
  196. return false; //To change body of implemented methods use File | Settings | File Templates.
  197. }
  198.  
  199. @Override
  200. public void start() throws Exception {
  201. //To change body of implemented methods use File | Settings | File Templates.
  202. }
  203.  
  204. @Override
  205. public void stop() throws Exception {
  206. //To change body of implemented methods use File | Settings | File Templates.
  207. }
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement