CAMEL-9879: Circuit Breaker EIP - That is using hystrix
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d041f703 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d041f703 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d041f703 Branch: refs/heads/master Commit: d041f703005df98111420079e5fb223f127e8a08 Parents: c89b23a Author: Claus Ibsen <[email protected]> Authored: Mon Apr 18 17:33:01 2016 +0200 Committer: Claus Ibsen <[email protected]> Committed: Tue Apr 19 18:16:45 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/impl/DefaultCamelContext.java | 2 +- .../camel/impl/DefaultProcessorFactory.java | 67 ++++++++++++++++++++ .../hystrix/HystrixDummyProcessor.java | 28 ++++++++ .../hystrix/HystrixProcessorFactory.java | 35 ++++++++++ .../camel/model/HystrixCircuitBreakerDefinition | 18 ++++++ .../hystrix/HystrixCircuitBreakerTest.java | 53 ++++++++++++++++ 6 files changed, 202 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d041f703/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index 654c7b9..d96d646 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -245,7 +245,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon private ServicePool<Endpoint, Producer> producerServicePool = new SharedProducerServicePool(100); private ServicePool<Endpoint, PollingConsumer> pollingConsumerServicePool = new SharedPollingConsumerServicePool(100); private NodeIdFactory nodeIdFactory = new DefaultNodeIdFactory(); - private ProcessorFactory processorFactory; + private ProcessorFactory processorFactory = new DefaultProcessorFactory(); private MessageHistoryFactory messageHistoryFactory = new DefaultMessageHistoryFactory(); private InterceptStrategy defaultTracer; private InterceptStrategy defaultBacklogTracer; http://git-wip-us.apache.org/repos/asf/camel/blob/d041f703/camel-core/src/main/java/org/apache/camel/impl/DefaultProcessorFactory.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultProcessorFactory.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultProcessorFactory.java new file mode 100644 index 0000000..078743d --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultProcessorFactory.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.impl; + +import org.apache.camel.NoFactoryAvailableException; +import org.apache.camel.Processor; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.spi.FactoryFinder; +import org.apache.camel.spi.ProcessorFactory; +import org.apache.camel.spi.RouteContext; + +public class DefaultProcessorFactory implements ProcessorFactory { + + public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/model/"; + + @Override + public Processor createChildProcessor(RouteContext routeContext, ProcessorDefinition<?> definition, boolean mandatory) throws Exception { + String name = definition.getClass().getSimpleName(); + FactoryFinder finder = routeContext.getCamelContext().getFactoryFinder(RESOURCE_PATH); + try { + if (finder != null) { + Object object = finder.newInstance(name); + if (object != null && object instanceof ProcessorFactory) { + ProcessorFactory pc = (ProcessorFactory) object; + return pc.createChildProcessor(routeContext, definition, mandatory); + } + } + } catch (NoFactoryAvailableException e) { + // ignore there is no custom factory + } + + return null; + } + + @Override + public Processor createProcessor(RouteContext routeContext, ProcessorDefinition<?> definition) throws Exception { + String name = definition.getClass().getSimpleName(); + FactoryFinder finder = routeContext.getCamelContext().getFactoryFinder(RESOURCE_PATH); + try { + if (finder != null) { + Object object = finder.newInstance(name); + if (object != null && object instanceof ProcessorFactory) { + ProcessorFactory pc = (ProcessorFactory) object; + return pc.createProcessor(routeContext, definition); + } + } + } catch (NoFactoryAvailableException e) { + // ignore there is no custom factory + } + + return null; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/d041f703/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixDummyProcessor.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixDummyProcessor.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixDummyProcessor.java new file mode 100644 index 0000000..48d52f5 --- /dev/null +++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixDummyProcessor.java @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.hystrix; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; + +public class HystrixDummyProcessor implements Processor { + + @Override + public void process(Exchange exchange) throws Exception { + System.out.println("Dummy processor"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/d041f703/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessorFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessorFactory.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessorFactory.java new file mode 100644 index 0000000..c0924b1 --- /dev/null +++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/HystrixProcessorFactory.java @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.hystrix; + +import org.apache.camel.Processor; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.spi.ProcessorFactory; +import org.apache.camel.spi.RouteContext; + +public class HystrixProcessorFactory implements ProcessorFactory { + + @Override + public Processor createChildProcessor(RouteContext routeContext, ProcessorDefinition<?> definition, boolean mandatory) throws Exception { + return null; + } + + @Override + public Processor createProcessor(RouteContext routeContext, ProcessorDefinition<?> definition) throws Exception { + return new HystrixDummyProcessor(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/d041f703/components/camel-hystrix/src/main/resources/META-INF/services/org/apache/camel/model/HystrixCircuitBreakerDefinition ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/main/resources/META-INF/services/org/apache/camel/model/HystrixCircuitBreakerDefinition b/components/camel-hystrix/src/main/resources/META-INF/services/org/apache/camel/model/HystrixCircuitBreakerDefinition new file mode 100644 index 0000000..0e00349 --- /dev/null +++ b/components/camel-hystrix/src/main/resources/META-INF/services/org/apache/camel/model/HystrixCircuitBreakerDefinition @@ -0,0 +1,18 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +class=org.apache.camel.component.hystrix.HystrixProcessorFactory http://git-wip-us.apache.org/repos/asf/camel/blob/d041f703/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerTest.java new file mode 100644 index 0000000..d714435 --- /dev/null +++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/HystrixCircuitBreakerTest.java @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.hystrix; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class HystrixCircuitBreakerTest extends CamelTestSupport { + + @Test + public void testHystrixCircuitBreaker() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .hystrixCircuitBreaker() + .to("direct:foo") + .fallback() + .transform().constant("Fallback message") + .end() + .to("mock:result"); + + from("direct:foo") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } + +}
