├── LICENSE
├── NOTICE
├── README.md
├── aggregating-messages
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ ├── AggregatingMessagesRoute.java
│ │ │ └── InvoiceTotalAggregator.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── aggregating-messages-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── AggregatingMessagesRouteSpringTest.java
│ └── AggregatingMessagesRouteTest.java
├── connecting-routes
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ └── ConnectingRoutes.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── connecting-routes-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── ConnectingRoutesSpringTest.java
│ └── ConnectingRoutesTest.java
├── creating-camel-project
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ ├── Main.java
│ │ │ ├── MoveFileRoute.java
│ │ │ └── SpringMain.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── move-file-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── MoveFileRouteSpringTest.java
│ └── MoveFileRouteTest.java
├── error-handling
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ └── DeadLetterChannelRoute.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── dead-letter-channel-route-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── DeadLetterChannelRouteSpringTest.java
│ └── DeadLetterChannelRouteTest.java
├── multicasting-messages
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ ├── HighestQuoteAggregator.java
│ │ │ └── MulticastingRoute.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── route-first-message-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── MulticastingRouteSpringTest.java
│ └── MulticastingRouteTest.java
├── pom.xml
├── removing-unwanted-messages
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ └── RemovingUnwantedMessagesRoute.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── removing-unwanted-messages-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── RemovingUnwantedMessagesRouteSpringTest.java
│ └── RemovingUnwantedMessagesRouteTest.java
├── reorganizing-messages
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ └── ReorganizingMessages.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── reorganizing-messages-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── ReorganizingMessagesSprintTest.java
│ └── ReorganizingMessagesTest.java
├── routing-different-destinations
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ └── RoutingToDifferentDestinationsRoute.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── routing-different-destinations-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── RoutingToDifferentDestinationsRouteSpringTest.java
│ └── RoutingToDifferentDestinationsRouteTest.java
├── splitting-message
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ └── SplittingMessageRoute.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── splitting-message-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── SplittingMessageRouteSpringTest.java
│ └── SplittingMessageRouteTest.java
├── testing-routes
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ └── SimpleChoiceRoute.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── simple-choice-route-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── SimpleChoiceRouteEnhancedTest.java
│ ├── SimpleChoiceRouteSpringTest.java
│ └── SimpleChoiceRouteTest.java
├── transforming-messages
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── org
│ │ │ └── apache
│ │ │ └── camel
│ │ │ └── howto
│ │ │ └── TransformingMessagesRoute.java
│ └── resources
│ │ ├── META-INF
│ │ └── spring
│ │ │ └── transforming-messages-context.xml
│ │ └── log4j.properties
│ └── test
│ └── java
│ └── org
│ └── apache
│ └── camel
│ └── howto
│ ├── TransformingMessagesRouteSpringTest.java
│ └── TransformingMessagesRouteTest.java
└── using-components
├── pom.xml
└── src
├── main
├── java
│ └── org
│ │ └── apache
│ │ └── camel
│ │ └── howto
│ │ └── UsingComponentsRoute.java
└── resources
│ ├── META-INF
│ └── spring
│ │ └── using-components-context.xml
│ └── log4j.properties
└── test
└── java
└── org
└── apache
└── camel
└── howto
├── UsingComponentsRouteSpringTest.java
└── UsingComponentsRouteTest.java
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 |
2 | Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Instant Apache Camel Message Routing Examples
2 |
3 | Creating a Camel project
4 | Routing messages to different destinations
5 | Using components
6 | Connecting routes
7 | Removing unwanted messages
8 | Transforming messages
9 | Splitting a message into many
10 | Aggregating multiple messages into one
11 | Reorganizing messages
12 | Multicasting messages
13 | Error handling and monitoring
14 | Testing messaging applications
15 |
16 | #### Run the examples with: mvn test
17 |
--------------------------------------------------------------------------------
/aggregating-messages/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | aggregating-messages
30 | Aggregating multiple messages into one
31 |
32 |
33 |
--------------------------------------------------------------------------------
/aggregating-messages/src/main/java/org/apache/camel/howto/AggregatingMessagesRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class AggregatingMessagesRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 |
26 | from("direct:start")
27 | .aggregate(header("invoiceId"), new InvoiceTotalAggregator())
28 | .completionTimeout(500)
29 | .to("mock:aggregated");
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/aggregating-messages/src/main/java/org/apache/camel/howto/InvoiceTotalAggregator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import java.math.BigDecimal;
20 |
21 | import org.apache.camel.Exchange;
22 | import org.apache.camel.processor.aggregate.AggregationStrategy;
23 |
24 | public class InvoiceTotalAggregator implements AggregationStrategy {
25 |
26 | public static final String ITEM_TOTAL_HEADER_NAME = "invoiceItemTotal";
27 |
28 | public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
29 | if (oldExchange == null) {
30 | return newExchange;
31 | }
32 | BigDecimal currentTotal = oldExchange.getIn().getHeader(ITEM_TOTAL_HEADER_NAME, BigDecimal.class);
33 | BigDecimal itemTotal = newExchange.getIn().getHeader(ITEM_TOTAL_HEADER_NAME, BigDecimal.class);
34 |
35 | oldExchange.getIn().setHeader(ITEM_TOTAL_HEADER_NAME, currentTotal.add(itemTotal));
36 | return oldExchange;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/aggregating-messages/src/main/resources/META-INF/spring/aggregating-messages-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 | header.invoiceId
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/aggregating-messages/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/aggregating-messages/src/test/java/org/apache/camel/howto/AggregatingMessagesRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import java.math.BigDecimal;
20 | import java.util.HashMap;
21 | import java.util.Map;
22 |
23 | import org.apache.camel.EndpointInject;
24 | import org.apache.camel.Produce;
25 | import org.apache.camel.ProducerTemplate;
26 | import org.apache.camel.component.mock.MockEndpoint;
27 | import org.apache.camel.test.spring.CamelSpringTestSupport;
28 | import org.junit.Test;
29 | import org.springframework.context.support.AbstractApplicationContext;
30 | import org.springframework.context.support.ClassPathXmlApplicationContext;
31 |
32 | public class AggregatingMessagesRouteSpringTest extends CamelSpringTestSupport {
33 |
34 | @Override
35 | protected AbstractApplicationContext createApplicationContext() {
36 | return new ClassPathXmlApplicationContext("META-INF/spring/aggregating-messages-context.xml");
37 | }
38 |
39 | @Produce(uri = "direct:start")
40 | protected ProducerTemplate start;
41 |
42 | @EndpointInject(uri = "mock:aggregated")
43 | private MockEndpoint mockAggregated;
44 |
45 | @Test
46 | public void aggregatesTwoMessagesIntoOne() throws Exception {
47 | mockAggregated.expectedHeaderReceived("invoiceItemTotal", BigDecimal.valueOf(5));
48 | mockAggregated.expectedMessageCount(1);
49 |
50 | start.sendBodyAndHeaders(null, toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(2)));
51 | start.sendBodyAndHeaders(null, toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(3)));
52 | assertMockEndpointsSatisfied();
53 | }
54 |
55 | @Test
56 | public void aggregatesMessagesByCorrelationKey() throws Exception {
57 | mockAggregated.expectedHeaderValuesReceivedInAnyOrder("invoiceItemTotal", BigDecimal.valueOf(5), BigDecimal.valueOf(4));
58 | mockAggregated.expectedMessageCount(2);
59 |
60 | start.sendBodyAndHeaders(null, toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(2)));
61 | start.sendBodyAndHeaders(null, toHeadersMap("invoiceId", "invoiceTwo", "invoiceItemTotal", BigDecimal.valueOf(4)));
62 | start.sendBodyAndHeaders(null, toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(3)));
63 | assertMockEndpointsSatisfied();
64 | }
65 |
66 | private Map toHeadersMap(Object... keyValuePairs) {
67 | Map result = new HashMap();
68 | for (int i = 0; i < keyValuePairs.length; i += 2) {
69 | result.put(keyValuePairs[i].toString(), keyValuePairs[i + 1]);
70 | }
71 | return result;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/aggregating-messages/src/test/java/org/apache/camel/howto/AggregatingMessagesRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import java.math.BigDecimal;
20 | import java.util.HashMap;
21 | import java.util.Map;
22 |
23 | import org.apache.camel.EndpointInject;
24 | import org.apache.camel.Produce;
25 | import org.apache.camel.ProducerTemplate;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.mock.MockEndpoint;
28 | import org.apache.camel.test.junit4.CamelTestSupport;
29 | import org.junit.Test;
30 |
31 | public class AggregatingMessagesRouteTest extends CamelTestSupport {
32 |
33 | @Override
34 | protected RouteBuilder createRouteBuilder() throws Exception {
35 | return new AggregatingMessagesRoute();
36 | }
37 |
38 | @Produce(uri = "direct:start")
39 | protected ProducerTemplate start;
40 |
41 | @EndpointInject(uri = "mock:aggregated")
42 | private MockEndpoint mockAggregated;
43 |
44 | @Test
45 | public void aggregatesTwoMessagesIntoOne() throws Exception {
46 | mockAggregated.expectedHeaderReceived("invoiceItemTotal", BigDecimal.valueOf(5));
47 | mockAggregated.expectedMessageCount(1);
48 |
49 | start.sendBodyAndHeaders(null, toHeaderMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(2)));
50 | start.sendBodyAndHeaders(null, toHeaderMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(3)));
51 | assertMockEndpointsSatisfied();
52 | }
53 |
54 | @Test
55 | public void aggregatesMessagesByCorrelationKey() throws Exception {
56 | mockAggregated.expectedHeaderValuesReceivedInAnyOrder("invoiceItemTotal", BigDecimal.valueOf(5), BigDecimal.valueOf(4));
57 | mockAggregated.expectedMessageCount(2);
58 |
59 | start.sendBodyAndHeaders(null, toHeaderMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(2)));
60 | start.sendBodyAndHeaders(null, toHeaderMap("invoiceId", "invoiceTwo", "invoiceItemTotal", BigDecimal.valueOf(4)));
61 | start.sendBodyAndHeaders(null, toHeaderMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(3)));
62 | assertMockEndpointsSatisfied();
63 | }
64 |
65 | private Map toHeaderMap(Object... keyValuePairs) {
66 | Map result = new HashMap();
67 | for (int i = 0; i < keyValuePairs.length; i += 2) {
68 | result.put(keyValuePairs[i].toString(), keyValuePairs[i + 1]);
69 | }
70 | return result;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/connecting-routes/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | connecting-routes
30 | Connecting routes together
31 |
32 |
33 |
--------------------------------------------------------------------------------
/connecting-routes/src/main/java/org/apache/camel/howto/ConnectingRoutes.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class ConnectingRoutes extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 | from("direct:logger")
26 | .id("loggerRoute")
27 | .to("log:org.apache.camel.howto.logger?level=INFO")
28 | .to("mock:result");
29 |
30 | from("timer://start?fixedRate=true&period=1000")
31 | .id("generatorRoute")
32 | .to("log:org.apache.camel.howto.generator?level=INFO")
33 | .to("direct:logger");
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/connecting-routes/src/main/resources/META-INF/spring/connecting-routes-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/connecting-routes/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/connecting-routes/src/test/java/org/apache/camel/howto/ConnectingRoutesSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.AdviceWithRouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.spring.CamelSpringTestSupport;
25 | import org.junit.Before;
26 | import org.junit.Test;
27 | import org.springframework.context.support.AbstractApplicationContext;
28 | import org.springframework.context.support.ClassPathXmlApplicationContext;
29 |
30 | public class ConnectingRoutesSpringTest extends CamelSpringTestSupport {
31 |
32 | @Override
33 | protected AbstractApplicationContext createApplicationContext() {
34 | return new ClassPathXmlApplicationContext("META-INF/spring/connecting-routes-context.xml");
35 | }
36 |
37 | @Produce(uri = "direct:start")
38 | protected ProducerTemplate start;
39 |
40 | @EndpointInject(uri = "mock:result")
41 | private MockEndpoint mockResult;
42 |
43 | @Before
44 | public void setUp() throws Exception {
45 | super.setUp();
46 | context.getRouteDefinition("generatorRoute").adviceWith(context, new AdviceWithRouteBuilder() {
47 | @Override
48 | public void configure() throws Exception {
49 | replaceFromWith("direct:start");
50 | }
51 | });
52 | }
53 |
54 | @Test
55 | public void deliversMessagesToNextRoute() throws Exception {
56 | mockResult.expectedBodiesReceived("Test message");
57 |
58 | start.sendBody("Test message");
59 | mockResult.assertIsSatisfied();
60 | }
61 | }
62 |
63 |
--------------------------------------------------------------------------------
/connecting-routes/src/test/java/org/apache/camel/howto/ConnectingRoutesTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.AdviceWithRouteBuilder;
23 | import org.apache.camel.builder.RouteBuilder;
24 | import org.apache.camel.component.mock.MockEndpoint;
25 | import org.apache.camel.test.junit4.CamelTestSupport;
26 | import org.junit.Before;
27 | import org.junit.Test;
28 |
29 | public class ConnectingRoutesTest extends CamelTestSupport {
30 |
31 | @Override
32 | protected RouteBuilder createRouteBuilder() throws Exception {
33 | return new ConnectingRoutes();
34 | }
35 |
36 | @Produce(uri = "direct:start")
37 | protected ProducerTemplate start;
38 |
39 | @EndpointInject(uri = "mock:result")
40 | private MockEndpoint mockResult;
41 |
42 | @Before
43 | public void setUp() throws Exception {
44 | super.setUp();
45 | context.getRouteDefinition("generatorRoute").adviceWith(context, new AdviceWithRouteBuilder() {
46 | @Override
47 | public void configure() throws Exception {
48 | replaceFromWith("direct:start");
49 | }
50 | });
51 | }
52 |
53 | @Test
54 | public void deliversMessagesToNextRoute() throws Exception {
55 | mockResult.expectedBodiesReceived("Test message");
56 |
57 | start.sendBody("Test message");
58 | mockResult.assertIsSatisfied();
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/creating-camel-project/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | creating-camel-project
30 | Creating our first Camel application
31 |
32 |
33 |
34 |
35 | org.codehaus.mojo
36 | exec-maven-plugin
37 | ${exec-maven-plugin.version}
38 |
39 | org.apache.camel.howto.Main
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/creating-camel-project/src/main/java/org/apache/camel/howto/Main.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.CamelContext;
20 | import org.apache.camel.impl.DefaultCamelContext;
21 |
22 | public class Main {
23 | public static void main(String[] args) throws Exception {
24 | CamelContext camelContext = new DefaultCamelContext();
25 | camelContext.addRoutes(new MoveFileRoute());
26 | camelContext.setTracing(true);
27 | camelContext.start();
28 | Thread.sleep(100000);
29 | camelContext.stop();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/creating-camel-project/src/main/java/org/apache/camel/howto/MoveFileRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class MoveFileRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 | from("file://source")
26 | .to("log://org.apache.camel.howto?showAll=true&multiline=true")
27 | .to("file://target");
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/creating-camel-project/src/main/java/org/apache/camel/howto/SpringMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.springframework.context.support.AbstractApplicationContext;
20 | import org.springframework.context.support.ClassPathXmlApplicationContext;
21 |
22 | public class SpringMain {
23 | public static void main(String[] args) throws Exception {
24 | AbstractApplicationContext springContext =
25 | new ClassPathXmlApplicationContext("META-INF/spring/move-file-context.xml");
26 |
27 | springContext.start();
28 | Thread.sleep(100000);
29 | springContext.stop();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/creating-camel-project/src/main/resources/META-INF/spring/move-file-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/creating-camel-project/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/creating-camel-project/src/test/java/org/apache/camel/howto/MoveFileRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.AdviceWithRouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.spring.CamelSpringTestSupport;
25 | import org.junit.Before;
26 | import org.junit.Test;
27 | import org.springframework.context.support.AbstractApplicationContext;
28 | import org.springframework.context.support.ClassPathXmlApplicationContext;
29 |
30 | public class MoveFileRouteSpringTest extends CamelSpringTestSupport {
31 |
32 | @Produce(uri = "direct:start")
33 | protected ProducerTemplate start;
34 |
35 | @EndpointInject(uri = "mock:file:target")
36 | private MockEndpoint mockResult;
37 |
38 | @Override
39 | protected AbstractApplicationContext createApplicationContext() {
40 | return new ClassPathXmlApplicationContext("META-INF/spring/move-file-context.xml");
41 | }
42 |
43 | @Before
44 | public void setUp() throws Exception {
45 | super.setUp();
46 | context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
47 | @Override
48 | public void configure() throws Exception {
49 | replaceFromWith("direct:start");
50 | mockEndpointsAndSkip("file:*");
51 | }
52 | });
53 | }
54 |
55 | @Test
56 | public void messageReachesTarted() throws Exception {
57 | mockResult.expectedBodiesReceived("Test message");
58 |
59 | start.sendBody("Test message");
60 | mockResult.assertIsSatisfied();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/creating-camel-project/src/test/java/org/apache/camel/howto/MoveFileRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.AdviceWithRouteBuilder;
23 | import org.apache.camel.builder.RouteBuilder;
24 | import org.apache.camel.component.mock.MockEndpoint;
25 | import org.apache.camel.test.junit4.CamelTestSupport;
26 | import org.junit.Before;
27 | import org.junit.Test;
28 |
29 | public class MoveFileRouteTest extends CamelTestSupport {
30 |
31 | @Override
32 | protected RouteBuilder createRouteBuilder() throws Exception {
33 | return new MoveFileRoute();
34 | }
35 |
36 | @EndpointInject(uri = "mock:file:target")
37 | private MockEndpoint mockResult;
38 |
39 | @Produce(uri = "direct:start")
40 | protected ProducerTemplate start;
41 |
42 | @Before
43 | public void setUp() throws Exception {
44 | super.setUp();
45 | context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
46 | @Override
47 | public void configure() throws Exception {
48 | replaceFromWith("direct:start");
49 | mockEndpointsAndSkip("file:*");
50 | }
51 | });
52 | }
53 |
54 | @Test
55 | public void messageReachesTarted() throws Exception {
56 | mockResult.expectedBodiesReceived("Test message");
57 |
58 | start.sendBody("Test message");
59 | mockResult.assertIsSatisfied();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/error-handling/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | error-handling
30 | Error handling and monitoring
31 |
32 |
33 |
--------------------------------------------------------------------------------
/error-handling/src/main/java/org/apache/camel/howto/DeadLetterChannelRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class DeadLetterChannelRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 | from("direct:start")
26 | .errorHandler(deadLetterChannel("mock:error")
27 | .maximumRedeliveries(3)
28 | .redeliveryDelay(1000)
29 | .backOffMultiplier(2)
30 | .useOriginalMessage()
31 | .useExponentialBackOff())
32 |
33 | .transform(body().append(" Modified data!"))
34 | .to("mock:result");
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/error-handling/src/main/resources/META-INF/spring/dead-letter-channel-route-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | ${in.body} Modified data!
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/error-handling/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/error-handling/src/test/java/org/apache/camel/howto/DeadLetterChannelRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Exchange;
21 | import org.apache.camel.Processor;
22 | import org.apache.camel.Produce;
23 | import org.apache.camel.ProducerTemplate;
24 | import org.apache.camel.component.mock.MockEndpoint;
25 | import org.apache.camel.test.spring.CamelSpringTestSupport;
26 | import org.junit.Before;
27 | import org.junit.Test;
28 | import org.springframework.context.support.AbstractApplicationContext;
29 | import org.springframework.context.support.ClassPathXmlApplicationContext;
30 |
31 | public class DeadLetterChannelRouteSpringTest extends CamelSpringTestSupport {
32 |
33 | @Override
34 | protected AbstractApplicationContext createApplicationContext() {
35 | return new ClassPathXmlApplicationContext("META-INF/spring/dead-letter-channel-route-context.xml");
36 | }
37 |
38 | @Produce(uri = "direct:start")
39 | protected ProducerTemplate start;
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mockResult;
43 |
44 | @EndpointInject(uri = "mock:error")
45 | private MockEndpoint mockError;
46 |
47 | private boolean failedOnce = false;
48 |
49 | @Before
50 | public void setUp() throws Exception {
51 | super.setUp();
52 |
53 | mockResult.whenAnyExchangeReceived(new Processor() {
54 | @Override
55 | public void process(Exchange exchange) throws Exception {
56 | String body = exchange.getIn().getBody(String.class);
57 | if (body.startsWith("ALWAYS FAILS")) {
58 | throw new Exception("Error publishing message");
59 | }
60 |
61 | if (body.startsWith("FAIL ONCE") && !failedOnce) {
62 | failedOnce = true;
63 | throw new Exception("Error publishing message");
64 | }
65 | }
66 | });
67 | }
68 |
69 | @Test
70 | public void retriesFourTime() throws Exception {
71 | mockResult.expectedMessageCount(4);
72 | mockError.expectedMessageCount(1);
73 | mockError.expectedBodiesReceived("ALWAYS FAILS");
74 |
75 | long startTime = System.currentTimeMillis();
76 | start.sendBody("ALWAYS FAILS");
77 | assertMockEndpointsSatisfied();
78 |
79 | long endTime = System.currentTimeMillis();
80 | assertTrue(endTime - startTime > 7000);
81 | }
82 |
83 | @Test
84 | public void failingMessageIsRetiedOnceBeforeSucceeding() throws Exception {
85 | mockResult.expectedMessageCount(2);
86 | mockError.expectedMessageCount(0);
87 |
88 | start.sendBody("FAIL ONCE");
89 | assertMockEndpointsSatisfied();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/error-handling/src/test/java/org/apache/camel/howto/DeadLetterChannelRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Exchange;
21 | import org.apache.camel.Processor;
22 | import org.apache.camel.Produce;
23 | import org.apache.camel.ProducerTemplate;
24 | import org.apache.camel.builder.RouteBuilder;
25 | import org.apache.camel.component.mock.MockEndpoint;
26 | import org.apache.camel.test.junit4.CamelTestSupport;
27 | import org.junit.Before;
28 | import org.junit.Test;
29 |
30 | public class DeadLetterChannelRouteTest extends CamelTestSupport {
31 |
32 | @Override
33 | protected RouteBuilder createRouteBuilder() throws Exception {
34 | return new DeadLetterChannelRoute();
35 | }
36 |
37 | @Produce(uri = "direct:start")
38 | protected ProducerTemplate start;
39 |
40 | @EndpointInject(uri = "mock:result")
41 | private MockEndpoint mockResult;
42 |
43 | @EndpointInject(uri = "mock:error")
44 | private MockEndpoint mockError;
45 |
46 | private boolean failedOnce = false;
47 |
48 | @Before
49 | public void setUp() throws Exception {
50 | super.setUp();
51 |
52 | mockResult.whenAnyExchangeReceived(new Processor() {
53 | @Override
54 | public void process(Exchange exchange) throws Exception {
55 | String body = exchange.getIn().getBody(String.class);
56 | if (body.startsWith("FAIL ALWAYS")) {
57 | throw new Exception("Error publishing message");
58 | }
59 |
60 | if (body.startsWith("FAIL ONCE") && !failedOnce) {
61 | failedOnce = true;
62 | throw new Exception("Error publishing message");
63 | }
64 | }
65 | });
66 | }
67 |
68 | @Test
69 | public void failingMessageIsRetied4TimesWithExponentialBackOffBeforeMovingToDLQ() throws Exception {
70 | mockResult.expectedMessageCount(4);
71 | mockError.expectedMessageCount(1);
72 | mockError.expectedBodiesReceived("FAIL ALWAYS");
73 |
74 | long startTime = System.currentTimeMillis();
75 | start.sendBody("FAIL ALWAYS");
76 | assertMockEndpointsSatisfied();
77 |
78 | long endTime = System.currentTimeMillis();
79 | assertTrue(endTime - startTime > 7000);
80 | }
81 |
82 | @Test
83 | public void failingMessageIsRetiedOnceBeforeSucceeding() throws Exception {
84 | mockResult.expectedMessageCount(2);
85 | mockError.expectedMessageCount(0);
86 |
87 | start.sendBody("FAIL ONCE");
88 | assertMockEndpointsSatisfied();
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/multicasting-messages/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | multicasting-messages
30 | Multicasting messages
31 |
32 |
33 |
--------------------------------------------------------------------------------
/multicasting-messages/src/main/java/org/apache/camel/howto/HighestQuoteAggregator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import java.math.BigDecimal;
20 |
21 | import org.apache.camel.Exchange;
22 | import org.apache.camel.processor.aggregate.AggregationStrategy;
23 |
24 | public class HighestQuoteAggregator implements AggregationStrategy {
25 | public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
26 | if (oldExchange == null) {
27 | return newExchange;
28 | }
29 |
30 | BigDecimal currentQuote = oldExchange.getIn().getBody(BigDecimal.class);
31 | BigDecimal newQuote = newExchange.getIn().getBody(BigDecimal.class);
32 | return currentQuote.compareTo(newQuote) > 0 ? oldExchange : newExchange;
33 | }
34 | }
--------------------------------------------------------------------------------
/multicasting-messages/src/main/java/org/apache/camel/howto/MulticastingRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class MulticastingRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 |
26 | from("direct:start")
27 | .multicast(new HighestQuoteAggregator())
28 | .parallelProcessing()
29 | .to("mock:a", "mock:b", "mock:c")
30 | .end()
31 | .to("mock:result");
32 | }
33 |
34 | }
35 |
36 |
--------------------------------------------------------------------------------
/multicasting-messages/src/main/resources/META-INF/spring/route-first-message-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/multicasting-messages/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/multicasting-messages/src/test/java/org/apache/camel/howto/MulticastingRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import java.math.BigDecimal;
20 |
21 | import org.apache.camel.EndpointInject;
22 | import org.apache.camel.Exchange;
23 | import org.apache.camel.Processor;
24 | import org.apache.camel.Produce;
25 | import org.apache.camel.ProducerTemplate;
26 | import org.apache.camel.component.mock.MockEndpoint;
27 | import org.apache.camel.test.spring.CamelSpringTestSupport;
28 | import org.junit.Before;
29 | import org.junit.Test;
30 | import org.springframework.context.support.AbstractApplicationContext;
31 | import org.springframework.context.support.ClassPathXmlApplicationContext;
32 |
33 | public class MulticastingRouteSpringTest extends CamelSpringTestSupport {
34 |
35 | @Override
36 | protected AbstractApplicationContext createApplicationContext() {
37 | return new ClassPathXmlApplicationContext("META-INF/spring/route-first-message-context.xml");
38 | }
39 |
40 | @Produce(uri = "direct:start")
41 | protected ProducerTemplate start;
42 |
43 | @EndpointInject(uri = "mock:result")
44 | private MockEndpoint mockResult;
45 |
46 | @EndpointInject(uri = "mock:a")
47 | private MockEndpoint mockA;
48 |
49 | @EndpointInject(uri = "mock:b")
50 | private MockEndpoint mockB;
51 |
52 | @EndpointInject(uri = "mock:c")
53 | private MockEndpoint mockC;
54 |
55 | @Before
56 | public void setUp() throws Exception {
57 | super.setUp();
58 |
59 | mockA.whenAnyExchangeReceived(new Processor() {
60 | @Override
61 | public void process(Exchange exchange) throws Exception {
62 | Thread.sleep(950);
63 | exchange.getIn().setBody(BigDecimal.valueOf(3));
64 | }
65 | });
66 | mockB.whenAnyExchangeReceived(new Processor() {
67 | @Override
68 | public void process(Exchange exchange) throws Exception {
69 | Thread.sleep(900);
70 | exchange.getIn().setBody(BigDecimal.valueOf(5));
71 | }
72 | });
73 | mockC.whenAnyExchangeReceived(new Processor() {
74 | @Override
75 | public void process(Exchange exchange) throws Exception {
76 | Thread.sleep(850);
77 | exchange.getIn().setBody(BigDecimal.valueOf(1));
78 | }
79 | });
80 | }
81 |
82 | @Test
83 | public void getsTheHighestQuoteProcessedInParallel() throws Exception {
84 | mockResult.expectedBodiesReceived(5);
85 |
86 | long start = System.currentTimeMillis();
87 | this.start.sendBody("Get Best Quote");
88 | mockResult.assertIsSatisfied();
89 | long endTime = System.currentTimeMillis();
90 |
91 | assertTrue(endTime - start < 2000);
92 |
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/multicasting-messages/src/test/java/org/apache/camel/howto/MulticastingRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import java.math.BigDecimal;
20 |
21 | import org.apache.camel.EndpointInject;
22 | import org.apache.camel.Exchange;
23 | import org.apache.camel.Processor;
24 | import org.apache.camel.Produce;
25 | import org.apache.camel.ProducerTemplate;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.mock.MockEndpoint;
28 | import org.apache.camel.test.junit4.CamelTestSupport;
29 | import org.junit.Before;
30 | import org.junit.Test;
31 |
32 | public class MulticastingRouteTest extends CamelTestSupport {
33 |
34 | @Override
35 | protected RouteBuilder createRouteBuilder() throws Exception {
36 | return new MulticastingRoute();
37 | }
38 |
39 | @Produce(uri = "direct:start")
40 | protected ProducerTemplate start;
41 |
42 | @EndpointInject(uri = "mock:result")
43 | private MockEndpoint mockResult;
44 |
45 | @EndpointInject(uri = "mock:a")
46 | private MockEndpoint mockA;
47 |
48 | @EndpointInject(uri = "mock:b")
49 | private MockEndpoint mockB;
50 |
51 | @EndpointInject(uri = "mock:c")
52 | private MockEndpoint mockC;
53 |
54 | @Before
55 | public void setUp() throws Exception {
56 | super.setUp();
57 |
58 | mockA.whenAnyExchangeReceived(new Processor() {
59 | @Override
60 | public void process(Exchange exchange) throws Exception {
61 | Thread.sleep(950);
62 | exchange.getIn().setBody(BigDecimal.valueOf(3));
63 | }
64 | });
65 | mockB.whenAnyExchangeReceived(new Processor() {
66 | @Override
67 | public void process(Exchange exchange) throws Exception {
68 | Thread.sleep(900);
69 | exchange.getIn().setBody(BigDecimal.valueOf(5));
70 | }
71 | });
72 | mockC.whenAnyExchangeReceived(new Processor() {
73 | @Override
74 | public void process(Exchange exchange) throws Exception {
75 | Thread.sleep(850);
76 | exchange.getIn().setBody(BigDecimal.valueOf(1));
77 | }
78 | });
79 | }
80 |
81 | @Test
82 | public void getsTheHighestQuoteProcessedInParallel() throws Exception {
83 | mockResult.expectedBodiesReceived(5);
84 |
85 | long start = System.currentTimeMillis();
86 | this.start.sendBody("Get Best Quote");
87 | mockResult.assertIsSatisfied();
88 | long endTime = System.currentTimeMillis();
89 |
90 | assertTrue(endTime - start < 2000);
91 |
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
20 | 4.0.0
21 |
22 | org.apache.camel.howto
23 | camel-message-routing
24 | pom
25 | 1.0.0
26 | Instant Apache Camel Message Routing How-to Examples
27 |
28 |
29 | 2.11.0
30 | 1.7.5
31 | 1.2.17
32 | 4.11
33 |
34 | 2.5.1
35 | 1.2.1
36 | UTF-8
37 |
38 |
39 |
40 | creating-camel-project
41 | routing-different-destinations
42 | using-components
43 | connecting-routes
44 | removing-unwanted-messages
45 | transforming-messages
46 | splitting-message
47 | aggregating-messages
48 | reorganizing-messages
49 | multicasting-messages
50 | error-handling
51 | testing-routes
52 |
53 |
54 |
55 |
56 |
57 | org.apache.camel
58 | camel-core
59 | ${camel-version}
60 |
61 |
62 | org.apache.camel
63 | camel-spring
64 | ${camel-version}
65 |
66 |
67 | org.apache.camel
68 | camel-xmljson
69 | ${camel-version}
70 |
71 |
72 | org.apache.camel
73 | camel-jetty
74 | ${camel-version}
75 |
76 |
77 |
78 |
79 | org.apache.camel
80 | camel-test
81 | ${camel-version}
82 | test
83 |
84 |
85 | org.apache.camel
86 | camel-test-spring
87 | ${camel-version}
88 | test
89 |
90 |
91 | junit
92 | junit
93 | ${junit-version}
94 | test
95 |
96 |
97 |
98 |
99 | org.slf4j
100 | slf4j-api
101 | ${slf4j-version}
102 |
103 |
104 | org.slf4j
105 | slf4j-log4j12
106 | ${slf4j-version}
107 |
108 |
109 | log4j
110 | log4j
111 | ${log4j-version}
112 |
113 |
114 |
115 |
116 |
117 |
118 | org.apache.camel
119 | camel-core
120 |
121 |
122 | org.apache.camel
123 | camel-spring
124 |
125 |
126 | org.apache.camel
127 | camel-test
128 |
129 |
130 | org.apache.camel
131 | camel-test-spring
132 |
133 |
134 | junit
135 | junit
136 |
137 |
138 | org.slf4j
139 | slf4j-api
140 |
141 |
142 | org.slf4j
143 | slf4j-log4j12
144 |
145 |
146 | log4j
147 | log4j
148 |
149 |
150 |
151 |
152 | test
153 |
154 |
155 | org.apache.maven.plugins
156 | maven-compiler-plugin
157 | ${maven-compiler-plugin-version}
158 |
159 | 1.6
160 | 1.6
161 |
162 |
163 |
164 |
165 |
--------------------------------------------------------------------------------
/removing-unwanted-messages/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | removing-unwanted-messages
30 | Removing unwanted messages
31 |
32 |
33 |
--------------------------------------------------------------------------------
/removing-unwanted-messages/src/main/java/org/apache/camel/howto/RemovingUnwantedMessagesRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class RemovingUnwantedMessagesRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 | from("direct:start")
26 | .filter(header("userStatus").isEqualTo("valid"))
27 | .to("mock:valid")
28 | .end()
29 | .to("mock:all");
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/removing-unwanted-messages/src/main/resources/META-INF/spring/removing-unwanted-messages-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 | ${in.header.userStatus} == 'valid'
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/removing-unwanted-messages/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/removing-unwanted-messages/src/test/java/org/apache/camel/howto/RemovingUnwantedMessagesRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.component.mock.MockEndpoint;
23 | import org.apache.camel.test.spring.CamelSpringTestSupport;
24 | import org.junit.Test;
25 | import org.springframework.context.support.AbstractApplicationContext;
26 | import org.springframework.context.support.ClassPathXmlApplicationContext;
27 |
28 | public class RemovingUnwantedMessagesRouteSpringTest extends CamelSpringTestSupport {
29 |
30 | @Override
31 | protected AbstractApplicationContext createApplicationContext() {
32 | return new ClassPathXmlApplicationContext("META-INF/spring/removing-unwanted-messages-context.xml");
33 | }
34 |
35 | @EndpointInject(uri = "mock:valid")
36 | private MockEndpoint mockValid;
37 |
38 | @EndpointInject(uri = "mock:all")
39 | private MockEndpoint mockAll;
40 |
41 | @Produce(uri = "direct:start")
42 | protected ProducerTemplate start;
43 |
44 | @Test
45 | public void validMessagesArePassed() throws Exception {
46 | mockValid.expectedBodiesReceived("Test message");
47 | mockAll.expectedBodiesReceived("Test message");
48 |
49 | start.sendBodyAndHeader("Test message", "userStatus", "valid");
50 | assertMockEndpointsSatisfied();
51 | }
52 |
53 | @Test
54 | public void invalidMessagesAreFilteredOut() throws Exception {
55 | mockValid.expectedMessageCount(0);
56 | mockAll.expectedBodiesReceived("Test message");
57 |
58 | start.sendBodyAndHeader("Test message", "userStatus", "invalid");
59 | assertMockEndpointsSatisfied();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/removing-unwanted-messages/src/test/java/org/apache/camel/howto/RemovingUnwantedMessagesRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.RouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.junit4.CamelTestSupport;
25 | import org.junit.Test;
26 |
27 | public class RemovingUnwantedMessagesRouteTest extends CamelTestSupport {
28 |
29 | @Override
30 | protected RouteBuilder createRouteBuilder() throws Exception {
31 | return new RemovingUnwantedMessagesRoute();
32 | }
33 |
34 | @EndpointInject(uri = "mock:valid")
35 | private MockEndpoint mockValid;
36 |
37 | @EndpointInject(uri = "mock:all")
38 | private MockEndpoint mockAll;
39 |
40 | @Produce(uri = "direct:start")
41 | protected ProducerTemplate start;
42 |
43 | @Test
44 | public void validMessagesArePassed() throws Exception {
45 | mockValid.expectedBodiesReceived("Test message");
46 | mockAll.expectedBodiesReceived("Test message");
47 |
48 | start.sendBodyAndHeader("Test message", "userStatus", "valid");
49 | assertMockEndpointsSatisfied();
50 | }
51 |
52 | @Test
53 | public void invalidMessagesAreFilteredOut() throws Exception {
54 | mockValid.expectedMessageCount(0);
55 | mockAll.expectedBodiesReceived("Test message");
56 |
57 | start.sendBodyAndHeader("Test message", "userStatus", "invalid");
58 | assertMockEndpointsSatisfied();
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/reorganizing-messages/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | reorganizing-messages
30 | Reorganizing messages
31 |
32 |
33 |
--------------------------------------------------------------------------------
/reorganizing-messages/src/main/java/org/apache/camel/howto/ReorganizingMessages.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class ReorganizingMessages extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 |
26 | from("direct:start")
27 | .resequence(header("message_index"))
28 | .batch().size(100).timeout(1000L)
29 | .to("mock:result");
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/reorganizing-messages/src/main/resources/META-INF/spring/reorganizing-messages-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/reorganizing-messages/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/reorganizing-messages/src/test/java/org/apache/camel/howto/ReorganizingMessagesSprintTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.component.mock.MockEndpoint;
23 | import org.apache.camel.test.spring.CamelSpringTestSupport;
24 | import org.junit.Test;
25 | import org.springframework.context.support.AbstractApplicationContext;
26 | import org.springframework.context.support.ClassPathXmlApplicationContext;
27 |
28 | public class ReorganizingMessagesSprintTest extends CamelSpringTestSupport {
29 |
30 | @Override
31 | protected AbstractApplicationContext createApplicationContext() {
32 | return new ClassPathXmlApplicationContext("META-INF/spring/reorganizing-messages-context.xml");
33 | }
34 |
35 | @Produce(uri = "direct:start")
36 | protected ProducerTemplate start;
37 |
38 | @EndpointInject(uri = "mock:result")
39 | private MockEndpoint mockResult;
40 |
41 | @Test
42 | public void ordersOutOfOrderMessages() throws Exception {
43 | mockResult.expectedMessageCount(3);
44 | mockResult.expectsAscending(header("message_index").convertTo(Number.class));
45 |
46 | start.sendBodyAndHeader(null, "message_index", 3);
47 | start.sendBodyAndHeader(null, "message_index", 1);
48 | start.sendBodyAndHeader(null, "message_index", 2);
49 |
50 | assertMockEndpointsSatisfied();
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/reorganizing-messages/src/test/java/org/apache/camel/howto/ReorganizingMessagesTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.RouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.junit4.CamelTestSupport;
25 | import org.junit.Test;
26 |
27 | public class ReorganizingMessagesTest extends CamelTestSupport {
28 |
29 | @Override
30 | protected RouteBuilder createRouteBuilder() throws Exception {
31 | return new ReorganizingMessages();
32 | }
33 |
34 | @Produce(uri = "direct:start")
35 | protected ProducerTemplate start;
36 |
37 | @EndpointInject(uri = "mock:result")
38 | private MockEndpoint mockResult;
39 |
40 | @Test
41 | public void ordersOutOfOrderMessages() throws Exception {
42 | mockResult.expectedMessageCount(3);
43 | mockResult.expectsAscending(header("message_index").convertTo(Number.class));
44 |
45 | start.sendBodyAndHeader(null, "message_index", 3);
46 | start.sendBodyAndHeader(null, "message_index", 1);
47 | start.sendBodyAndHeader(null, "message_index", 2);
48 |
49 | assertMockEndpointsSatisfied();
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/routing-different-destinations/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | routing-different-destinations
30 | Routing messages to different destinations
31 |
32 |
33 |
--------------------------------------------------------------------------------
/routing-different-destinations/src/main/java/org/apache/camel/howto/RoutingToDifferentDestinationsRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class RoutingToDifferentDestinationsRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 | from("file://source")
26 | .choice()
27 | .when(simple("${in.header.CamelFileName} contains 'widget.txt'"))
28 | .to("file://widget")
29 | .when(simple("${in.header.CamelFileName} contains 'gadget.txt'"))
30 | .to("file://gadget")
31 | .otherwise()
32 | .to("log://org.apache.camel.howto");
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/routing-different-destinations/src/main/resources/META-INF/spring/routing-different-destinations-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | ${in.header.CamelFileName} contains 'widget.txt'
30 |
31 |
32 |
33 | ${in.header.CamelFileName} contains 'gadget.txt'
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/routing-different-destinations/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/routing-different-destinations/src/test/java/org/apache/camel/howto/RoutingToDifferentDestinationsRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.AdviceWithRouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.spring.CamelSpringTestSupport;
25 | import org.junit.Before;
26 | import org.junit.Test;
27 | import org.springframework.context.support.AbstractApplicationContext;
28 | import org.springframework.context.support.ClassPathXmlApplicationContext;
29 |
30 | public class RoutingToDifferentDestinationsRouteSpringTest extends CamelSpringTestSupport {
31 |
32 | @Produce(uri = "direct:start")
33 | protected ProducerTemplate start;
34 |
35 | @EndpointInject(uri = "mock:file:widget")
36 | private MockEndpoint mockWidget;
37 |
38 | @EndpointInject(uri = "mock:file:gadget")
39 | private MockEndpoint mockGadget;
40 |
41 | @EndpointInject(uri = "mock:log:org.apache.camel.howto")
42 | private MockEndpoint mockOther;
43 |
44 | @Override
45 | protected AbstractApplicationContext createApplicationContext() {
46 | return new ClassPathXmlApplicationContext("META-INF/spring/routing-different-destinations-context.xml");
47 | }
48 |
49 | @Before
50 | public void setUp() throws Exception {
51 | super.setUp();
52 | context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
53 | @Override
54 | public void configure() throws Exception {
55 | replaceFromWith("direct:start");
56 | mockEndpointsAndSkip("*");
57 | }
58 | });
59 | }
60 |
61 | @Test
62 | public void sendsAGadgetMessage() throws Exception {
63 | mockWidget.expectedBodiesReceived("Test message");
64 | mockGadget.expectedMessageCount(0);
65 | mockOther.expectedMessageCount(0);
66 |
67 | start.sendBodyAndHeader("Test message", "CamelFileName", "widget.txt");
68 | assertMockEndpointsSatisfied();
69 | }
70 |
71 | @Test
72 | public void sendsAWidgetAndAGadgetMessages() throws Exception {
73 | mockWidget.expectedBodiesReceived("widget message");
74 | mockGadget.expectedBodiesReceived("gadget message");
75 | mockOther.expectedMessageCount(0);
76 |
77 | start.sendBodyAndHeader("widget message", "CamelFileName", "widget.txt");
78 | start.sendBodyAndHeader("gadget message", "CamelFileName", "gadget.txt");
79 | assertMockEndpointsSatisfied();
80 | }
81 |
82 | @Test
83 | public void sendsAnotherMessage() throws Exception {
84 | mockWidget.expectedMessageCount(0);
85 | mockGadget.expectedMessageCount(0);
86 | mockOther.expectedMessageCount(1);
87 |
88 | start.sendBodyAndHeader("Test message", "CamelFileName", "other.txt");
89 | assertMockEndpointsSatisfied();
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/routing-different-destinations/src/test/java/org/apache/camel/howto/RoutingToDifferentDestinationsRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.AdviceWithRouteBuilder;
23 | import org.apache.camel.builder.RouteBuilder;
24 | import org.apache.camel.component.mock.MockEndpoint;
25 | import org.apache.camel.test.junit4.CamelTestSupport;
26 | import org.junit.Before;
27 | import org.junit.Test;
28 |
29 | public class RoutingToDifferentDestinationsRouteTest extends CamelTestSupport {
30 |
31 | @Override
32 | protected RouteBuilder createRouteBuilder() throws Exception {
33 | return new RoutingToDifferentDestinationsRoute();
34 | }
35 |
36 | @Produce(uri = "direct:start")
37 | protected ProducerTemplate start;
38 |
39 | @EndpointInject(uri = "mock:file:widget")
40 | private MockEndpoint mockWidget;
41 |
42 | @EndpointInject(uri = "mock:file:gadget")
43 | private MockEndpoint mockGadget;
44 |
45 | @EndpointInject(uri = "mock:log:org.apache.camel.howto")
46 | private MockEndpoint mockOther;
47 |
48 | @Before
49 | public void setUp() throws Exception {
50 | super.setUp();
51 | context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
52 | @Override
53 | public void configure() throws Exception {
54 | replaceFromWith("direct:start");
55 | mockEndpointsAndSkip("*");
56 | }
57 | });
58 | }
59 |
60 | @Test
61 | public void sendsAGadgetMessage() throws Exception {
62 | mockWidget.expectedBodiesReceived("Test message");
63 | mockGadget.expectedMessageCount(0);
64 | mockOther.expectedMessageCount(0);
65 |
66 | start.sendBodyAndHeader("Test message", "CamelFileName", "widget.txt");
67 | assertMockEndpointsSatisfied();
68 | }
69 |
70 | @Test
71 | public void sendsAWidgetAndAGadgetMessages() throws Exception {
72 | mockWidget.expectedBodiesReceived("widget message");
73 | mockGadget.expectedBodiesReceived("gadget message");
74 | mockOther.expectedMessageCount(0);
75 |
76 | start.sendBodyAndHeader("widget message", "CamelFileName", "widget.txt");
77 | start.sendBodyAndHeader("gadget message", "CamelFileName", "gadget.txt");
78 | assertMockEndpointsSatisfied();
79 | }
80 |
81 | @Test
82 | public void sendsAnotherMessage() throws Exception {
83 | mockWidget.expectedMessageCount(0);
84 | mockGadget.expectedMessageCount(0);
85 | mockOther.expectedMessageCount(1);
86 |
87 | start.sendBodyAndHeader("Test message", "CamelFileName", "other.txt");
88 | assertMockEndpointsSatisfied();
89 | }
90 | }
91 |
92 |
93 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/splitting-message/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | splitting-message
30 | Splitting a message into many
31 |
32 |
33 |
--------------------------------------------------------------------------------
/splitting-message/src/main/java/org/apache/camel/howto/SplittingMessageRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class SplittingMessageRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 |
26 | from("direct:start")
27 | .split(xpath("//invoice/item/product"))
28 | .convertBodyTo(String.class)
29 | .to("mock:products")
30 | .end()
31 | .to("mock:result");
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/splitting-message/src/main/resources/META-INF/spring/splitting-message-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 | //invoice/item/product
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/splitting-message/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/splitting-message/src/test/java/org/apache/camel/howto/SplittingMessageRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.component.mock.MockEndpoint;
23 | import org.apache.camel.test.spring.CamelSpringTestSupport;
24 | import org.junit.Test;
25 | import org.springframework.context.support.AbstractApplicationContext;
26 | import org.springframework.context.support.ClassPathXmlApplicationContext;
27 |
28 | public class SplittingMessageRouteSpringTest extends CamelSpringTestSupport {
29 |
30 | @Override
31 | protected AbstractApplicationContext createApplicationContext() {
32 | return new ClassPathXmlApplicationContext("META-INF/spring/splitting-message-context.xml");
33 | }
34 |
35 | @EndpointInject(uri = "mock:result")
36 | private MockEndpoint mockResult;
37 |
38 | @EndpointInject(uri = "mock:products")
39 | private MockEndpoint mockProducts;
40 |
41 | @Produce(uri = "direct:start")
42 | protected ProducerTemplate start;
43 |
44 | private static final String XML_INPUT =
45 | "\n" +
46 | "\n" +
47 | " - \n" +
48 | " \n" +
49 | "
\n" +
50 | " - \n" +
51 | " \n" +
52 | "
\n" +
53 | "";
54 |
55 | @Test
56 | public void splitsInputMessageIntoTwoMessages() throws Exception {
57 | mockResult.expectedBodiesReceived(XML_INPUT);
58 | mockProducts.expectedMessageCount(2);
59 | mockProducts.expectedBodiesReceived("", "");
60 |
61 | start.sendBody(XML_INPUT);
62 | assertMockEndpointsSatisfied();
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/splitting-message/src/test/java/org/apache/camel/howto/SplittingMessageRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.RouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.junit4.CamelTestSupport;
25 | import org.junit.Test;
26 |
27 | public class SplittingMessageRouteTest extends CamelTestSupport {
28 |
29 | @Override
30 | protected RouteBuilder createRouteBuilder() throws Exception {
31 | return new SplittingMessageRoute();
32 | }
33 |
34 | @EndpointInject(uri = "mock:result")
35 | private MockEndpoint mockResult;
36 |
37 | @EndpointInject(uri = "mock:products")
38 | private MockEndpoint mockProducts;
39 |
40 | @Produce(uri = "direct:start")
41 | protected ProducerTemplate start;
42 |
43 | private static final String XML_INPUT =
44 | "\n" +
45 | "\n" +
46 | " - \n" +
47 | " \n" +
48 | "
\n" +
49 | " - \n" +
50 | " \n" +
51 | "
\n" +
52 | "";
53 |
54 | @Test
55 | public void splitsInputMessageIntoTwoMessages() throws Exception {
56 | mockResult.expectedBodiesReceived(XML_INPUT);
57 | mockProducts.expectedMessageCount(2);
58 | mockProducts.expectedBodiesReceived("", "");
59 |
60 | start.sendBody(XML_INPUT);
61 | assertMockEndpointsSatisfied();
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/testing-routes/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | testing-routes
30 | Testing messaging applications
31 |
32 |
33 |
--------------------------------------------------------------------------------
/testing-routes/src/main/java/org/apache/camel/howto/SimpleChoiceRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class SimpleChoiceRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 | from("direct:start")
26 | .choice()
27 | .when(body().isEqualTo("orange"))
28 | .to("mock:oranges")
29 | .when(body().isEqualTo("apple"))
30 | .to("mock:apples");
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/testing-routes/src/main/resources/META-INF/spring/simple-choice-route-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | ${in.body} == 'orange'
30 |
31 |
32 |
33 | ${in.body} == 'apple'
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/testing-routes/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/testing-routes/src/test/java/org/apache/camel/howto/SimpleChoiceRouteEnhancedTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.CamelContext;
20 | import org.apache.camel.EndpointInject;
21 | import org.apache.camel.Produce;
22 | import org.apache.camel.ProducerTemplate;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner;
25 | import org.junit.Test;
26 | import org.junit.runner.RunWith;
27 | import org.springframework.beans.factory.annotation.Autowired;
28 | import org.springframework.test.annotation.DirtiesContext;
29 | import org.springframework.test.context.ContextConfiguration;
30 |
31 | @RunWith(CamelSpringJUnit4ClassRunner.class)
32 | @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
33 | @ContextConfiguration(locations = {"classpath:/META-INF/spring/simple-choice-route-context.xml"})
34 | public class SimpleChoiceRouteEnhancedTest {
35 |
36 | @Autowired
37 | private CamelContext camelContext;
38 |
39 | @Produce(uri = "direct:start")
40 | protected ProducerTemplate start;
41 |
42 | @EndpointInject(uri = "mock:oranges")
43 | private MockEndpoint mockOranges;
44 |
45 | @EndpointInject(uri = "mock:apples")
46 | private MockEndpoint mockApples;
47 |
48 | @Test
49 | public void routesMessagesToSeparateEndpoints() throws Exception {
50 | mockOranges.expectedBodiesReceived("orange");
51 | mockApples.expectedBodiesReceived("apple");
52 |
53 | start.sendBody("orange");
54 | start.sendBody("apple");
55 | MockEndpoint.assertIsSatisfied(camelContext);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/testing-routes/src/test/java/org/apache/camel/howto/SimpleChoiceRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.component.mock.MockEndpoint;
23 | import org.apache.camel.test.spring.CamelSpringTestSupport;
24 | import org.junit.Test;
25 | import org.springframework.context.support.AbstractApplicationContext;
26 | import org.springframework.context.support.ClassPathXmlApplicationContext;
27 |
28 | public class SimpleChoiceRouteSpringTest extends CamelSpringTestSupport {
29 |
30 | @Override
31 | protected AbstractApplicationContext createApplicationContext() {
32 | return new ClassPathXmlApplicationContext("META-INF/spring/simple-choice-route-context.xml");
33 | }
34 |
35 | @Produce(uri = "direct:start")
36 | protected ProducerTemplate start;
37 |
38 | @EndpointInject(uri = "mock:oranges")
39 | private MockEndpoint mockOranges;
40 |
41 | @EndpointInject(uri = "mock:apples")
42 | private MockEndpoint mockApples;
43 |
44 | @Test
45 | public void routesMessagesToSeparateEndpoints() throws Exception {
46 | mockOranges.expectedBodiesReceived("orange");
47 | mockApples.expectedBodiesReceived("apple");
48 |
49 | start.sendBody("orange");
50 | start.sendBody("apple");
51 | assertMockEndpointsSatisfied();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/testing-routes/src/test/java/org/apache/camel/howto/SimpleChoiceRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.RouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.junit4.CamelTestSupport;
25 | import org.junit.Test;
26 |
27 | public class SimpleChoiceRouteTest extends CamelTestSupport {
28 |
29 | @Override
30 | protected RouteBuilder createRouteBuilder() throws Exception {
31 | return new SimpleChoiceRoute();
32 | }
33 |
34 | @Produce(uri = "direct:start")
35 | protected ProducerTemplate start;
36 |
37 | @EndpointInject(uri = "mock:oranges")
38 | private MockEndpoint mockOranges;
39 |
40 | @EndpointInject(uri = "mock:apples")
41 | private MockEndpoint mockApples;
42 |
43 | @Test
44 | public void routesMessagesToSeparateEndpoints() throws Exception {
45 | mockOranges.expectedBodiesReceived("orange");
46 | mockApples.expectedBodiesReceived("apple");
47 |
48 | start.sendBody("orange");
49 | start.sendBody("apple");
50 | assertMockEndpointsSatisfied();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/transforming-messages/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | transforming-messages
30 | Transforming messages
31 |
32 |
33 |
34 | org.apache.camel
35 | camel-xmljson
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/transforming-messages/src/main/java/org/apache/camel/howto/TransformingMessagesRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 | import org.apache.camel.model.dataformat.XmlJsonDataFormat;
21 |
22 | public class TransformingMessagesRoute extends RouteBuilder {
23 |
24 | private final XmlJsonDataFormat xmlJsonFormat;
25 |
26 | public TransformingMessagesRoute() {
27 | xmlJsonFormat = new XmlJsonDataFormat();
28 | xmlJsonFormat.setForceTopLevelObject(true);
29 | xmlJsonFormat.setTrimSpaces(true);
30 | xmlJsonFormat.setSkipNamespaces(true);
31 | xmlJsonFormat.setRemoveNamespacePrefixes(true);
32 | }
33 |
34 | @Override
35 | public void configure() throws Exception {
36 | from("direct:start")
37 | .marshal(xmlJsonFormat)
38 | .to("mock:result");
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/transforming-messages/src/main/resources/META-INF/spring/transforming-messages-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/transforming-messages/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/transforming-messages/src/test/java/org/apache/camel/howto/TransformingMessagesRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.component.mock.MockEndpoint;
23 | import org.apache.camel.test.spring.CamelSpringTestSupport;
24 | import org.junit.Test;
25 | import org.springframework.context.support.AbstractApplicationContext;
26 | import org.springframework.context.support.ClassPathXmlApplicationContext;
27 |
28 | public class TransformingMessagesRouteSpringTest extends CamelSpringTestSupport {
29 |
30 | @Override
31 | protected AbstractApplicationContext createApplicationContext() {
32 | return new ClassPathXmlApplicationContext("META-INF/spring/transforming-messages-context.xml");
33 | }
34 | @EndpointInject(uri = "mock:result")
35 | private MockEndpoint mockResult;
36 |
37 | @Produce(uri = "direct:start")
38 | protected ProducerTemplate start;
39 |
40 | private static final String XML_INPUT =
41 | "\n" +
42 | " 1\n" +
43 | " 2\n" +
44 | " \n" +
45 | " 3\n" +
46 | " 4\n" +
47 | " \n" +
48 | "";
49 |
50 | private static final String EXPECTED_JSON =
51 | "{\"root\":{" +
52 | "\"a\":\"1\"," +
53 | "\"b\":\"2\"," +
54 | "\"c\":{" +
55 | "\"ca\":\"3\"," +
56 | "\"cb\":\"4\"}" +
57 | "}" +
58 | "}";
59 |
60 | @Test
61 | public void transformsXmlIntoJson() throws Exception {
62 | mockResult.expectedBodiesReceived(EXPECTED_JSON);
63 |
64 | start.sendBody(XML_INPUT);
65 | assertMockEndpointsSatisfied();
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/transforming-messages/src/test/java/org/apache/camel/howto/TransformingMessagesRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.RouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.junit4.CamelTestSupport;
25 | import org.junit.Test;
26 |
27 | public class TransformingMessagesRouteTest extends CamelTestSupport {
28 |
29 | @Override
30 | protected RouteBuilder createRouteBuilder() throws Exception {
31 | return new TransformingMessagesRoute();
32 | }
33 |
34 | @EndpointInject(uri = "mock:result")
35 | private MockEndpoint mockResult;
36 |
37 | @Produce(uri = "direct:start")
38 | protected ProducerTemplate start;
39 |
40 | private static final String XML_INPUT =
41 | "\n" +
42 | " 1\n" +
43 | " 2\n" +
44 | " \n" +
45 | " 3\n" +
46 | " 4\n" +
47 | " \n" +
48 | "";
49 |
50 | private static final String EXPECTED_JSON =
51 | "{\"root\":{" +
52 | "\"a\":\"1\"," +
53 | "\"b\":\"2\"," +
54 | "\"c\":{" +
55 | "\"ca\":\"3\"," +
56 | "\"cb\":\"4\"}" +
57 | "}" +
58 | "}";
59 |
60 | @Test
61 | public void transformsXmlIntoJson() throws Exception {
62 | mockResult.expectedBodiesReceived(EXPECTED_JSON);
63 |
64 | start.sendBody(XML_INPUT);
65 | assertMockEndpointsSatisfied();
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/using-components/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel.howto
25 | camel-message-routing
26 | 1.0.0
27 |
28 |
29 | using-components
30 | Using Camel components
31 |
32 |
33 |
34 | org.apache.camel
35 | camel-jetty
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/using-components/src/main/java/org/apache/camel/howto/UsingComponentsRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.builder.RouteBuilder;
20 |
21 | public class UsingComponentsRoute extends RouteBuilder {
22 |
23 | @Override
24 | public void configure() throws Exception {
25 |
26 | from("jetty:http://localhost:8181?matchOnUriPrefix=true")
27 | .to("log:org.apache.camel.howto?showAll=true&multiline=true")
28 | .to("jetty:http://www.google.co.uk?bridgeEndpoint=true&throwExceptionOnFailure=false");
29 | }
30 |
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/using-components/src/main/resources/META-INF/spring/using-components-context.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/using-components/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | ##
4 | ## Licensed under the Apache License, Version 2.0 (the "License");
5 | ## you may not use this file except in compliance with the License.
6 | ## You may obtain a copy of the License at
7 | ##
8 | ## http://www.apache.org/licenses/LICENSE-2.0
9 | ##
10 | ## Unless required by applicable law or agreed to in writing, software
11 | ## distributed under the License is distributed on an "AS IS" BASIS,
12 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | ## See the License for the specific language governing permissions and
14 | ## limitations under the License.
15 | ## ------------------------------------------------------------------------
16 |
17 | log4j.rootLogger=INFO, out
18 |
19 | # uncomment the following line to turn on Camel debugging
20 | #log4j.logger.org.apache.camel.component.mock=DEBUG
21 |
22 | # CONSOLE appender not used by default
23 | log4j.appender.out=org.apache.log4j.ConsoleAppender
24 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
25 | log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
26 |
--------------------------------------------------------------------------------
/using-components/src/test/java/org/apache/camel/howto/UsingComponentsRouteSpringTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.AdviceWithRouteBuilder;
23 | import org.apache.camel.component.mock.MockEndpoint;
24 | import org.apache.camel.test.spring.CamelSpringTestSupport;
25 | import org.junit.Before;
26 | import org.junit.Test;
27 | import org.springframework.context.support.AbstractApplicationContext;
28 | import org.springframework.context.support.ClassPathXmlApplicationContext;
29 |
30 | import static org.apache.camel.builder.Builder.constant;
31 | import static org.hamcrest.CoreMatchers.is;
32 |
33 | public class UsingComponentsRouteSpringTest extends CamelSpringTestSupport {
34 |
35 | @Override
36 | protected AbstractApplicationContext createApplicationContext() {
37 | return new ClassPathXmlApplicationContext("META-INF/spring/using-components-context.xml");
38 | }
39 |
40 | @EndpointInject(uri = "mock://http:www.google.co.uk")
41 | private MockEndpoint mockResult;
42 |
43 | @Produce(uri = "direct:start")
44 | protected ProducerTemplate start;
45 |
46 | @Before
47 | public void setUp() throws Exception {
48 | super.setUp();
49 | context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
50 | @Override
51 | public void configure() throws Exception {
52 | replaceFromWith("direct:start");
53 | mockEndpointsAndSkip("*");
54 | }
55 | });
56 |
57 | mockResult.returnReplyBody(constant("Result From Target Endpoint"));
58 | }
59 |
60 | @Test
61 | public void returnsResultFromTargetEndpoint() throws Exception {
62 |
63 | String result = start.requestBody((Object) "Some query", String.class);
64 | assertThat(result, is("Result From Target Endpoint"));
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/using-components/src/test/java/org/apache/camel/howto/UsingComponentsRouteTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) Bilgin Ibryam http://www.ofbizian.com
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.apache.camel.howto;
18 |
19 | import org.apache.camel.EndpointInject;
20 | import org.apache.camel.Produce;
21 | import org.apache.camel.ProducerTemplate;
22 | import org.apache.camel.builder.AdviceWithRouteBuilder;
23 | import org.apache.camel.builder.RouteBuilder;
24 | import org.apache.camel.component.mock.MockEndpoint;
25 | import org.apache.camel.test.junit4.CamelTestSupport;
26 | import org.junit.Before;
27 | import org.junit.Test;
28 |
29 | import static org.apache.camel.builder.Builder.constant;
30 | import static org.hamcrest.CoreMatchers.is;
31 |
32 | public class UsingComponentsRouteTest extends CamelTestSupport {
33 | @Override
34 | protected RouteBuilder createRouteBuilder() throws Exception {
35 | return new UsingComponentsRoute();
36 | }
37 |
38 | @EndpointInject(uri = "mock://http:www.google.co.uk")
39 | private MockEndpoint mockResult;
40 |
41 | @Produce(uri = "direct:start")
42 | protected ProducerTemplate start;
43 |
44 | @Before
45 | public void setUp() throws Exception {
46 | super.setUp();
47 | context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
48 | @Override
49 | public void configure() throws Exception {
50 | replaceFromWith("direct:start");
51 | mockEndpointsAndSkip("*");
52 | }
53 | });
54 |
55 | mockResult.returnReplyBody(constant("Result From Target Endpoint"));
56 | }
57 |
58 | @Test
59 | public void returnsResultFromTargetEndpoint() throws Exception {
60 |
61 | String result = start.requestBody((Object) "Some query", String.class);
62 | assertThat(result, is("Result From Target Endpoint"));
63 | }
64 | }
65 |
--------------------------------------------------------------------------------