You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/06/21 08:21:19 UTC

(camel) 01/01: CAMEL-20894: EventNotifier: Not all step EIPs are triggered as events

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

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

commit 37394e821d90ae42885e9f4c9c7a29712cacafee
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Jun 21 10:21:01 2024 +0200

    CAMEL-20894: EventNotifier: Not all step EIPs are triggered as events
---
 .../org/apache/camel/processor/StepProcessor.java  |  2 -
 .../org/apache/camel/reifier/ProcessorReifier.java |  6 +-
 .../java/org/apache/camel/reifier/StepReifier.java |  7 ++
 .../processor/StepEventNotifierOneOutputTest.java  | 85 ++++++++++++++++++++++
 4 files changed, 97 insertions(+), 3 deletions(-)

diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/StepProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/StepProcessor.java
index a8f725b1f0b..cce18f6639d 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/StepProcessor.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/StepProcessor.java
@@ -42,8 +42,6 @@ public class StepProcessor extends Pipeline {
     public static Processor newInstance(CamelContext camelContext, List<Processor> processors, String stepId) {
         if (processors.isEmpty()) {
             return null;
-        } else if (processors.size() == 1) {
-            return processors.get(0);
         }
         return new StepProcessor(camelContext, processors, stepId);
     }
diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ProcessorReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ProcessorReifier.java
index 0ce9d2397a3..7849a6d8dc0 100644
--- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ProcessorReifier.java
+++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ProcessorReifier.java
@@ -803,6 +803,10 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> extends
     }
 
     protected Processor createOutputsProcessor(Collection<ProcessorDefinition<?>> outputs) throws Exception {
+        return createOutputsProcessor(outputs, true);
+    }
+
+    protected Processor createOutputsProcessor(Collection<ProcessorDefinition<?>> outputs, boolean optimize) throws Exception {
         List<Processor> list = new ArrayList<>();
         for (ProcessorDefinition<?> output : outputs) {
 
@@ -832,7 +836,7 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> extends
         // keep it as is
         Processor processor = null;
         if (!list.isEmpty()) {
-            if (list.size() == 1) {
+            if (optimize && list.size() == 1) {
                 processor = list.get(0);
             } else {
                 processor = createCompositeProcessor(list);
diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/StepReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/StepReifier.java
index 4b7dbd351db..dfedcaaca4a 100644
--- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/StepReifier.java
+++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/StepReifier.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.reifier;
 
+import java.util.Collection;
 import java.util.List;
 
 import org.apache.camel.Processor;
@@ -35,6 +36,12 @@ public class StepReifier extends ProcessorReifier<StepDefinition> {
         return this.createChildProcessor(true);
     }
 
+    @Override
+    protected Processor createOutputsProcessor(Collection<ProcessorDefinition<?>> outputs) throws Exception {
+        // do not optimize to force always wrapping in step processor
+        return super.createOutputsProcessor(outputs, false);
+    }
+
     @Override
     protected Processor createCompositeProcessor(List<Processor> list) throws Exception {
         String stepId = getId(definition);
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/StepEventNotifierOneOutputTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/StepEventNotifierOneOutputTest.java
new file mode 100644
index 00000000000..93e096fc87b
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/StepEventNotifierOneOutputTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.CamelEvent;
+import org.apache.camel.support.EventNotifierSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class StepEventNotifierOneOutputTest extends ContextTestSupport {
+
+    private final MyEventNotifier notifier = new MyEventNotifier();
+
+    @Test
+    public void testStepEventNotifier() throws Exception {
+        context.addService(notifier);
+        context.getManagementStrategy().addEventNotifier(notifier);
+
+        assertEquals(0, notifier.getEvents().size());
+
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals(2, notifier.getEvents().size());
+        assertIsInstanceOf(CamelEvent.StepStartedEvent.class, notifier.getEvents().get(0));
+        assertIsInstanceOf(CamelEvent.StepCompletedEvent.class, notifier.getEvents().get(1));
+        assertEquals("foo", ((CamelEvent.StepEvent) notifier.getEvents().get(0)).getStepId());
+        assertEquals("foo", ((CamelEvent.StepEvent) notifier.getEvents().get(1)).getStepId());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").step("foo").to("mock:foo").end();
+            }
+        };
+    }
+
+    private static class MyEventNotifier extends EventNotifierSupport {
+
+        private final List<CamelEvent> events = new ArrayList<>();
+
+        public MyEventNotifier() {
+            setIgnoreCamelContextEvents(true);
+            setIgnoreExchangeEvents(true);
+            setIgnoreRouteEvents(true);
+            setIgnoreServiceEvents(true);
+            setIgnoreStepEvents(false);
+        }
+
+        @Override
+        public void notify(CamelEvent event) {
+            events.add(event);
+        }
+
+        public List<CamelEvent> getEvents() {
+            return events;
+        }
+    }
+}