This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 3a7f436 CAMEL-12610 Fixed the issue of camel bean always invokes cached instance 3a7f436 is described below commit 3a7f436de13e07b0c964e3677974150b5c84ec04 Author: Willem Jiang <jiangni...@huawei.com> AuthorDate: Sun Jul 8 10:48:22 2018 +0800 CAMEL-12610 Fixed the issue of camel bean always invokes cached instance --- .../component/bean/AbstractBeanProcessor.java | 3 +- .../org/apache/camel/processor/BeanCachedTest.java | 104 +++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java b/camel-core/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java index 063ccf9..173f89d 100644 --- a/camel-core/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java @@ -92,7 +92,8 @@ public abstract class AbstractBeanProcessor implements AsyncProcessor { // do we have a custom adapter for this POJO to a Processor // but only do this if allowed - if (allowProcessor(explicitMethodName, beanInfo)) { + // we need to check beanHolder is Processor is support, to avoid the bean cached issue + if (allowProcessor(explicitMethodName, beanInfo) && beanHolder.supportProcessor()) { processor = getProcessor(); if (processor == null && !lookupProcessorDone) { // only attempt to lookup the processor once or nearly once diff --git a/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java b/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java new file mode 100644 index 0000000..f9cfcb4 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java @@ -0,0 +1,104 @@ +/** + * 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.processor; + +import java.util.concurrent.atomic.AtomicInteger; + +import javax.naming.Context; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.junit.Test; + +public class BeanCachedTest extends ContextTestSupport { + + private Context context; + + private JndiRegistry registry; + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:noCache") + .to("bean:something?cache=false"); + from("direct:cached") + .to("bean:something?cache=true"); + + } + }; + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + registry.bind("something", new MyBean()); + this.context = registry.getContext(); + this.registry = registry; + return registry; + } + + @Test + public void testFreshBeanInContext() throws Exception { + // Just make sure the bean processor doesn't work if the cached is false + MyBean originalInstance = registry.lookup("something", MyBean.class); + template.sendBody("direct:noCache", null); + context.unbind("something"); + context.bind("something", new MyBean()); + // Make sure we can get the object from the registry + assertNotSame(registry.lookup("something"), originalInstance); + template.sendBody("direct:noCache", null); + } + + @Test + public void testBeanWithCached() throws Exception { + // Just make sure the bean processor doesn't work if the cached is false + MyBean originalInstance = registry.lookup("something", MyBean.class); + template.sendBody("direct:cached", null); + context.unbind("something"); + context.bind("something", new MyBean()); + // Make sure we can get the object from the registry + assertNotSame(registry.lookup("something"), originalInstance); + try { + template.sendBody("direct:cached", null); + fail("The IllegalStateException is expected"); + } catch (CamelExecutionException ex) { + assertTrue("IllegalStateException is expected!", ex.getCause() instanceof IllegalStateException); + assertEquals("This bean is not supported to be invoked again!", ex.getCause().getMessage()); + } + } + + + public static class MyBean implements Processor { + private boolean invoked; + + public void process(Exchange exchange) throws Exception { + if (invoked) { + throw new IllegalStateException("This bean is not supported to be invoked again!"); + } else { + invoked = true; + } + + } + } + +}