You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/08/22 14:15:52 UTC

[camel] branch main updated: (chores) camel-bean: break the process method (#11171)

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 83ed8676293 (chores) camel-bean: break the process method (#11171)
83ed8676293 is described below

commit 83ed8676293f072259b7c1b8255f6447a388d71c
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Tue Aug 22 16:15:06 2023 +0200

    (chores) camel-bean: break the process method (#11171)
    
    This should provide better reports in profilers
---
 .../component/bean/AbstractBeanProcessor.java      | 103 ++++++++++++---------
 1 file changed, 61 insertions(+), 42 deletions(-)

diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
index f681f96c960..ff287aeba53 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
@@ -59,18 +59,14 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
     @Override
     public boolean process(Exchange exchange, AsyncCallback callback) {
         // do we have an explicit method name we always should invoke (either configured on endpoint or as a header)
-        String explicitMethodName = exchange.getIn().getHeader(BeanConstants.BEAN_METHOD_NAME, method, String.class);
+        final String explicitMethodName = exchange.getIn().getHeader(BeanConstants.BEAN_METHOD_NAME, method, String.class);
 
-        Object bean;
-        BeanInfo beanInfo;
+        final Object beanInstance;
+        final BeanInfo beanInfo;
         try {
-            bean = beanHolder.getBean(exchange);
+            beanInstance = beanHolder.getBean(exchange);
             // get bean info for this bean instance (to avoid thread issue)
-            beanInfo = beanHolder.getBeanInfo(bean);
-            if (beanInfo == null) {
-                // fallback and use old way
-                beanInfo = beanHolder.getBeanInfo();
-            }
+            beanInfo = doGetBeanInfo(beanInstance);
         } catch (Exception e) {
             exchange.setException(e);
             callback.done(true);
@@ -81,51 +77,29 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
         // but only do this if allowed
         // we need to check beanHolder is Processor is support, to avoid the bean cached issue
         if (allowProcessor(explicitMethodName, beanInfo)) {
-            Processor target = getProcessor();
-            if (target == null) {
-                // only attempt to lookup the processor once or nearly once
-                // allow cache by default or if the scope is singleton
-                boolean allowCache = scope == null || scope == BeanScope.Singleton;
-                if (allowCache) {
-                    if (!lookupProcessorDone) {
-                        synchronized (lock) {
-                            lookupProcessorDone = true;
-                            // so if there is a custom type converter for the bean to processor
-                            target = exchange.getContext().getTypeConverter().tryConvertTo(Processor.class, exchange, bean);
-                            processor = target;
-                        }
-                    }
-                } else {
-                    // so if there is a custom type converter for the bean to processor
-                    target = exchange.getContext().getTypeConverter().tryConvertTo(Processor.class, exchange, bean);
-                }
-            }
+            final Processor target = getCustomAdapter(exchange, beanInstance);
             if (target != null) {
-                if (LOG.isTraceEnabled()) {
-                    LOG.trace("Using a custom adapter as bean invocation: {}", target);
-                }
-                try {
-                    target.process(exchange);
-                } catch (AssertionError | Exception e) {
-                    exchange.setException(e);
-                } finally {
-                    callback.done(true);
-                }
+                useCustomAdapter(exchange, callback, target);
 
                 return true;
             }
         }
 
-        Message in = exchange.getIn();
+        return useMethodInvocation(exchange, callback, explicitMethodName, beanInfo, beanInstance);
+    }
+
+    private static boolean useMethodInvocation(
+            Exchange exchange, AsyncCallback callback, String explicitMethodName, BeanInfo beanInfo, Object beanInstance) {
+        final Message in = exchange.getIn();
 
         // set explicit method name to invoke as a header, which is how BeanInfo can detect it
         if (explicitMethodName != null) {
             in.setHeader(BeanConstants.BEAN_METHOD_NAME, explicitMethodName);
         }
 
-        MethodInvocation invocation;
+        final MethodInvocation invocation;
         try {
-            invocation = beanInfo.createInvocation(bean, exchange);
+            invocation = beanInfo.createInvocation(beanInstance, exchange);
         } catch (Exception e) {
             exchange.setException(e);
             callback.done(true);
@@ -139,7 +113,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
 
         if (invocation == null) {
             exchange.setException(new IllegalStateException(
-                    "No method invocation could be created, no matching method could be found on: " + bean));
+                    "No method invocation could be created, no matching method could be found on: " + beanInstance));
             callback.done(true);
             return true;
         }
@@ -148,6 +122,51 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
         return invocation.proceed(callback);
     }
 
+    private Processor getCustomAdapter(Exchange exchange, Object beanTmp) {
+        Processor target = getProcessor();
+        if (target == null) {
+            // only attempt to lookup the processor once or nearly once
+            // allow cache by default or if the scope is singleton
+            boolean allowCache = scope == null || scope == BeanScope.Singleton;
+            if (allowCache) {
+                if (!lookupProcessorDone) {
+                    synchronized (lock) {
+                        lookupProcessorDone = true;
+                        // so if there is a custom type converter for the bean to processor
+                        target = exchange.getContext().getTypeConverter().tryConvertTo(Processor.class, exchange, beanTmp);
+                        processor = target;
+                    }
+                }
+            } else {
+                // so if there is a custom type converter for the bean to processor
+                target = exchange.getContext().getTypeConverter().tryConvertTo(Processor.class, exchange, beanTmp);
+            }
+        }
+        return target;
+    }
+
+    private static void useCustomAdapter(Exchange exchange, AsyncCallback callback, Processor target) {
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Using a custom adapter as bean invocation: {}", target);
+        }
+        try {
+            target.process(exchange);
+        } catch (AssertionError | Exception e) {
+            exchange.setException(e);
+        } finally {
+            callback.done(true);
+        }
+    }
+
+    private BeanInfo doGetBeanInfo(Object beanTmp) {
+        BeanInfo beanInfo = beanHolder.getBeanInfo(beanTmp);
+        if (beanInfo == null) {
+            // fallback and use old way
+            beanInfo = beanHolder.getBeanInfo();
+        }
+        return beanInfo;
+    }
+
     protected Processor getProcessor() {
         return processor;
     }