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 2023/09/27 07:34:18 UTC
[camel] branch init-bean created (now c0cc5d2a45f)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a change to branch init-bean
in repository https://gitbox.apache.org/repos/asf/camel.git
at c0cc5d2a45f CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
This branch includes the following new commits:
new f08bb1dd236 CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
new a81f7cf4095 CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
new 82f10bc608b CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
new c0cc5d2a45f CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[camel] 02/04: CAMEL-19808: camel-core-model - Add init and destroy method to
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch init-bean
in repository https://gitbox.apache.org/repos/asf/camel.git
commit a81f7cf409537155e90ffd30f3a5237507276f12
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Sep 27 08:15:08 2023 +0200
CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
---
.../org/apache/camel/support/ObjectHelper.java | 32 ++++++++++
.../camel/dsl/xml/io/XmlRoutesBuilderLoader.java | 71 ++++++++++++++++++----
.../apache/camel/dsl/xml/io/XmlLoadAppTest.java | 37 +++++++++++
.../camel/dsl/xml/io/beans/MyDestroyBean.java | 63 +++++++++++++++++++
.../org/apache/camel/dsl/xml/io/camel-app8.xml | 39 ++++++++++++
5 files changed, 229 insertions(+), 13 deletions(-)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
index 1add25b7d88..decca85f8b6 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java
@@ -39,6 +39,7 @@ import org.apache.camel.Message;
import org.apache.camel.Ordered;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.TypeConverter;
+import org.apache.camel.util.ReflectionHelper;
import org.apache.camel.util.Scanner;
import org.apache.camel.util.StringHelper;
@@ -386,6 +387,37 @@ public final class ObjectHelper {
return answer;
}
+ /**
+ * A helper method to invoke a method via reflection in a safe way by allowing to invoke methods that are not
+ * accessible by default and wrap any exceptions as {@link RuntimeCamelException} instances
+ *
+ * @param name the method name
+ * @param instance the object instance (or null for static methods)
+ * @param parameters the parameters to the method
+ * @return the result of the method invocation
+ */
+ public static Object invokeMethodSafe(String name, Object instance, Object... parameters)
+ throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
+
+ // find method first
+ Class<?>[] arr = null;
+ if (parameters != null) {
+ arr = new Class[parameters.length];
+ for (int i = 0; i < parameters.length; i++) {
+ Object p = parameters[i];
+ if (p != null) {
+ arr[i] = p.getClass();
+ }
+ }
+ }
+ Method m = ReflectionHelper.findMethod(instance.getClass(), name, arr);
+ if (m != null) {
+ return invokeMethodSafe(m, instance, parameters);
+ } else {
+ throw new NoSuchMethodException(name);
+ }
+ }
+
/**
* A helper method to create a new instance of a type using the default constructor arguments.
*/
diff --git a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
index 604378a65ba..d44a6941260 100644
--- a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
+++ b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
@@ -18,6 +18,7 @@ package org.apache.camel.dsl.xml.io;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -50,8 +51,10 @@ import org.apache.camel.model.rest.RestsDefinition;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.annotations.RoutesLoader;
import org.apache.camel.support.CachedResource;
+import org.apache.camel.support.ObjectHelper;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.scan.PackageScanHelper;
+import org.apache.camel.util.KeyValueHolder;
import org.apache.camel.util.StringHelper;
import org.apache.camel.xml.io.util.XmlStreamDetector;
import org.apache.camel.xml.io.util.XmlStreamInfo;
@@ -71,6 +74,7 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport {
private final Map<String, XmlStreamInfo> xmlInfoCache = new ConcurrentHashMap<>();
private final Map<String, BeansDefinition> camelAppCache = new ConcurrentHashMap<>();
private final List<RegistryBeanDefinition> delayedRegistrations = new ArrayList<>();
+ private final Map<String, KeyValueHolder<Object, String>> beansToDestroy = new LinkedHashMap<>();
private final AtomicInteger counter = new AtomicInteger(0);
@@ -137,14 +141,12 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport {
new XmlModelParser(resource, xmlInfo.getRootElementNamespace())
.parseTemplatedRoutesDefinition()
.ifPresent(this::setTemplatedRouteCollection);
- case "rests", "rest" ->
- new XmlModelParser(resource, xmlInfo.getRootElementNamespace())
- .parseRestsDefinition()
- .ifPresent(this::setRestCollection);
- case "routes", "route" ->
- new XmlModelParser(resource, xmlInfo.getRootElementNamespace())
- .parseRoutesDefinition()
- .ifPresent(this::addRoutes);
+ case "rests", "rest" -> new XmlModelParser(resource, xmlInfo.getRootElementNamespace())
+ .parseRestsDefinition()
+ .ifPresent(this::setRestCollection);
+ case "routes", "route" -> new XmlModelParser(resource, xmlInfo.getRootElementNamespace())
+ .parseRoutesDefinition()
+ .ifPresent(this::addRoutes);
default -> {
}
}
@@ -353,12 +355,8 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport {
if (def.getProperties() != null && !def.getProperties().isEmpty()) {
PropertyBindingSupport.setPropertiesOnTarget(getCamelContext(), target, def.getProperties());
}
- getCamelContext().getRegistry().unbind(name);
- getCamelContext().getRegistry().bind(name, target);
- // register bean in model
- Model model = getCamelContext().getCamelContextExtension().getContextPlugin(Model.class);
- model.addRegistryBean(def);
+ bindBean(def, name, target);
} catch (Exception e) {
if (delayIfFailed) {
@@ -370,4 +368,51 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport {
}
}
+ protected void bindBean(RegistryBeanDefinition def, String name, Object target) throws Exception {
+ // destroy and unbind any existing bean
+ destroyBean(name, true);
+ getCamelContext().getRegistry().unbind(name);
+
+ // invoke init method and register bean
+ String initMethod = def.getInitMethod();
+ if (initMethod != null) {
+ ObjectHelper.invokeMethodSafe(initMethod, target);
+ }
+ getCamelContext().getRegistry().bind(name, target);
+
+ // remember to destroy bean on shutdown
+ if (def.getDestroyMethod() != null) {
+ beansToDestroy.put(name, new KeyValueHolder<>(target, def.getDestroyMethod()));
+ }
+
+ // register bean in model
+ Model model = getCamelContext().getCamelContextExtension().getContextPlugin(Model.class);
+ model.addRegistryBean(def);
+ }
+
+ protected void destroyBean(String name, boolean remove) {
+ var holder = remove ? beansToDestroy.remove(name) : beansToDestroy.get(name);
+ if (holder != null) {
+ String destroyMethod = holder.getValue();
+ Object target = holder.getKey();
+ try {
+ ObjectHelper.invokeMethodSafe(destroyMethod, target);
+ } catch (Exception e) {
+ LOG.warn("Error invoking destroy method: {} on bean: {} due to: {}. This exception is ignored.",
+ destroyMethod, target, e.getMessage(), e);
+ }
+ }
+ }
+
+ @Override
+ protected void doStop() throws Exception {
+ super.doStop();
+
+ // beans should trigger destroy methods on shutdown
+ for (String name : beansToDestroy.keySet()) {
+ destroyBean(name, false);
+ }
+ beansToDestroy.clear();
+ }
+
}
diff --git a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java
index 3f031dccd43..d5984129b8d 100644
--- a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java
+++ b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.dsl.xml.io;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.dsl.xml.io.beans.GreeterMessage;
+import org.apache.camel.dsl.xml.io.beans.MyDestroyBean;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesLoader;
@@ -25,7 +26,9 @@ import org.apache.camel.support.PluginHelper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
public class XmlLoadAppTest {
@@ -190,4 +193,38 @@ public class XmlLoadAppTest {
}
}
+ @Test
+ public void testLoadCamelAppWithBeanInitDestroy() throws Exception {
+ try (DefaultCamelContext context = new DefaultCamelContext()) {
+ context.start();
+
+ assertFalse(MyDestroyBean.initCalled.get());
+ assertFalse(MyDestroyBean.destroyCalled.get());
+
+ Resource resource = PluginHelper.getResourceLoader(context).resolveResource(
+ "/org/apache/camel/dsl/xml/io/camel-app8.xml");
+
+ RoutesLoader routesLoader = PluginHelper.getRoutesLoader(context);
+ routesLoader.preParseRoute(resource, false);
+ routesLoader.loadRoutes(resource);
+
+ assertNotNull(context.getRoute("r8"), "Loaded r8 route should be there");
+ assertEquals(1, context.getRoutes().size());
+
+ // test that loaded route works
+ MockEndpoint y8 = context.getEndpoint("mock:y8", MockEndpoint.class);
+ y8.expectedBodiesReceived("Hello Neptun. I am Camel and 45 years old!");
+ context.createProducerTemplate().sendBody("direct:x8", "Neptun");
+ y8.assertIsSatisfied();
+
+ assertTrue(MyDestroyBean.initCalled.get());
+ assertFalse(MyDestroyBean.destroyCalled.get());
+
+ context.stop();
+
+ assertTrue(MyDestroyBean.initCalled.get());
+ assertTrue(MyDestroyBean.destroyCalled.get());
+ }
+ }
+
}
diff --git a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/beans/MyDestroyBean.java b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/beans/MyDestroyBean.java
new file mode 100644
index 00000000000..e92c8bf6207
--- /dev/null
+++ b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/beans/MyDestroyBean.java
@@ -0,0 +1,63 @@
+/*
+ * 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.dsl.xml.io.beans;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public class MyDestroyBean {
+
+ public static final AtomicBoolean initCalled = new AtomicBoolean();
+ public static final AtomicBoolean destroyCalled = new AtomicBoolean();
+
+ private String field1;
+ private String field2;
+ private int age;
+
+ public MyDestroyBean(String field1, String field2) {
+ this.field1 = field1;
+ this.field2 = field2;
+ }
+
+ public String getField1() {
+ return field1;
+ }
+
+ public String getField2() {
+ return field2;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String hello(String body) {
+ return field1 + " " + body + ". I am " + field2 + " and " + age + " years old!";
+ }
+
+ public void initMe() {
+ initCalled.set(true);
+ }
+
+ public void destroyMe() {
+ destroyCalled.set(true);
+ }
+
+}
diff --git a/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app8.xml b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app8.xml
new file mode 100644
index 00000000000..35de6096a77
--- /dev/null
+++ b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app8.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<camel xmlns="http://camel.apache.org/schema/spring" xmlns:s="http://www.springframework.org/schema/beans">
+
+ <bean name="xml-bean-from-registry" type="org.apache.camel.dsl.xml.io.beans.MyDestroyBean"
+ initMethod="initMe" destroyMethod="destroyMe">
+ <constructors>
+ <constructor value="Hello"/>
+ <constructor value="Camel"/>
+ </constructors>
+ <properties>
+ <property key="age" value="45"/>
+ </properties>
+ </bean>
+
+ <route id="r8">
+ <from uri="direct:x8"/>
+ <bean ref="xml-bean-from-registry" method="hello"/>
+ <to uri="mock:y8"/>
+ </route>
+
+</camel>
[camel] 04/04: CAMEL-19808: camel-core-model - Add init and destroy method to
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch init-bean
in repository https://gitbox.apache.org/repos/asf/camel.git
commit c0cc5d2a45f26726da26d88bdb6122593f2294f0
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Sep 27 09:28:23 2023 +0200
CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
---
.../apache/camel/catalog/schemas/camel-spring.xsd | 2 +
.../src/main/docs/java-xml-io-dsl.adoc | 38 +++++++
.../camel-yaml-dsl/src/main/docs/yaml-dsl.adoc | 111 ++++++++-------------
3 files changed, 80 insertions(+), 71 deletions(-)
diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/schemas/camel-spring.xsd b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/schemas/camel-spring.xsd
index 66af7a0e9f2..602d7c3ba08 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/schemas/camel-spring.xsd
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/schemas/camel-spring.xsd
@@ -13603,6 +13603,8 @@ org.apache.camel.builder.RouteBuilder.
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="type" type="xs:string" use="required"/>
+ <xs:attribute name="initMethod" type="xs:string"/>
+ <xs:attribute name="destroyMethod" type="xs:string"/>
<xs:attribute name="factoryMethod" type="xs:string"/>
<xs:attribute name="factoryBean" type="xs:string"/>
</xs:complexType>
diff --git a/dsl/camel-xml-io-dsl/src/main/docs/java-xml-io-dsl.adoc b/dsl/camel-xml-io-dsl/src/main/docs/java-xml-io-dsl.adoc
index f96371e8a0f..b985c67c4df 100644
--- a/dsl/camel-xml-io-dsl/src/main/docs/java-xml-io-dsl.adoc
+++ b/dsl/camel-xml-io-dsl/src/main/docs/java-xml-io-dsl.adoc
@@ -222,6 +222,44 @@ public class MyHelper {
NOTE: The factory method must be `public static`.
+=== Using init and destroy methods on beans
+
+Sometimes beans need to do some initialization and cleanup work before a bean is ready to be used.
+For this you can use `initMethod` and `destroyMethod` that Camel triggers accordingly.
+
+Those methods must be public void and have no arguments, as shown below:
+
+[source,java]
+----
+public class MyBean {
+
+ public void initMe() {
+ // do init work here
+ }
+
+ public void destroyMe() {
+ // do cleanup work here
+ }
+
+}
+----
+
+You then have to declare those methods in XML DSL as follows:
+
+[source,xml]
+----
+ <bean name="myBean" type="com.acme.MyBean"
+ initMethod="initMe" destroyMethod="destroyMe">
+ <constructors>
+ <constructor index="0" value="true"/>
+ <constructor index="1" value="Hello World"/>
+ </constructors>
+ </bean>
+----
+
+The init and destroy methods are optional, so a bean does not have to have both,
+for example you may only have destroy methods.
+
== See Also
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/docs/yaml-dsl.adoc b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/docs/yaml-dsl.adoc
index 71fd6f385e6..f52b67e8016 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/docs/yaml-dsl.adoc
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/docs/yaml-dsl.adoc
@@ -267,6 +267,45 @@ public class MyHelper {
NOTE: The factory method must be `public static`.
+=== Using init and destroy methods on beans
+
+Sometimes beans need to do some initialization and cleanup work before a bean is ready to be used.
+For this you can use `initMethod` and `destroyMethod` that Camel triggers accordingly.
+
+Those methods must be public void and have no arguments, as shown below:
+
+[source,java]
+----
+public class MyBean {
+
+ public void initMe() {
+ // do init work here
+ }
+
+ public void destroyMe() {
+ // do cleanup work here
+ }
+
+}
+----
+
+You then have to declare those methods in YAML DSL as follows:
+
+[source,yaml]
+----
+- beans:
+ - name: myBean
+ type: com.acme.MyBean
+ initMethod: initMe
+ destroyMethod: destroyMe
+ constructors:
+ 0: true
+ 1: "Hello World"
+----
+
+The init and destroy methods are optional, so a bean does not have to have both,
+for example you may only have destroy methods.
+
== Configuring options on languages
Some xref:components:languages:index.adoc[Languages] have additional configurations
@@ -325,74 +364,4 @@ which demonstrate creating Camel Routes with YAML.
Another way to find examples of YAML DSL is to look in https://github.com/apache/camel-kamelets[Camel Kamelets]
where each Kamelet is defined using YAML.
-== Supported EIPs
-
-The following set of high level EIPs are supported in the yaml DSL language. For full details on their expected configuration, please refer to the https://github.com/apache/camel/blob/main/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json[Camel YAML DSL specification].
-
-- Aggregate
-- Bean
-- Do Catch
-- Do Try
-- Do Finally
-- Choice
-- Circuit Breaker
-- Claim Check
-- Convert Body To
-- Delay
-- Dynamic Router
-- Enrich
-- Error Handler
-- Filter
-- From
-- Idempotent Consumer
-- Intercept
-- Intercept From
-- Intercept Send To Endpoint
-- Load Balance
-- Log
-- Loop
-- Marshal
-- Multicast
-- On Completion
-- On Exception
-- On Fallback
-- Otherwise
-- Pausable
-- Pipeline
-- Policy
-- Poll Enrich
-- Process
-- Recipient List
-- Remove Header
-- Remove Headers
-- Remove Property
-- Remove Properties
-- Resequence
-- Resumable
-- REST DSL
-- Rollback
-- Routing Slip
-- Saga
-- Sample
-- Script
-- Service Call
-- Set Body
-- Set Exchange Pattern
-- Set Header
-- Set Property
-- Sort
-- Split
-- Step
-- Stop
-- Threads
-- Throttle
-- Throw Exception
-- To
-- To Dynamic
-- Transacted
-- Transform
-- Unmarshal
-- Validate
-- When
-- When Skip Send To Endpoint
-- Wire Tap
+
[camel] 03/04: CAMEL-19808: camel-core-model - Add init and destroy method to
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch init-bean
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 82f10bc608b95f9395b43d21a2401e82e87ede02
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Sep 27 09:16:47 2023 +0200
CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
---
.../camel/dsl/xml/io/XmlRoutesBuilderLoader.java | 81 ++++++++++++----------
.../dsl/yaml/deserializers/BeansDeserializer.java | 74 +++++++++++++++++---
.../dsl/yaml/YamlRoutesBuilderLoaderSupport.java | 4 ++
.../org/apache/camel/dsl/yaml/BeansTest.groovy | 32 +++++++++
.../dsl/yaml/support/model/MyDestroyBean.groovy | 62 +++++++++++++++++
5 files changed, 205 insertions(+), 48 deletions(-)
diff --git a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
index d44a6941260..9fa17215c3a 100644
--- a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
+++ b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java
@@ -30,6 +30,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.w3c.dom.Document;
+import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.builder.RouteBuilder;
@@ -321,51 +322,57 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport {
* {@link #doLoadRouteBuilder}), a failure may lead to delayed registration.
*/
private void registerBeanDefinition(RegistryBeanDefinition def, boolean delayIfFailed) {
- String type = def.getType();
String name = def.getName();
- if (name == null || name.isBlank()) {
- name = type;
+ String type = def.getType();
+ try {
+ Object target = newInstance(def, getCamelContext());
+ bindBean(def, name, target);
+ } catch (Exception e) {
+ if (delayIfFailed) {
+ delayedRegistrations.add(def);
+ } else {
+ String msg
+ = name != null ? "Error creating bean: " + name + " of type: " + type : "Error creating bean: " + type;
+ throw new RuntimeException(msg, e);
+ }
}
- if (type != null && !type.startsWith("#")) {
- type = "#class:" + type;
- try {
- // factory bean/method
- if (def.getFactoryBean() != null && def.getFactoryMethod() != null) {
- type = type + "#" + def.getFactoryBean() + ":" + def.getFactoryMethod();
- } else if (def.getFactoryMethod() != null) {
- type = type + "#" + def.getFactoryMethod();
- }
- // property binding support has constructor arguments as part of the type
- StringJoiner ctr = new StringJoiner(", ");
- if (def.getConstructors() != null && !def.getConstructors().isEmpty()) {
- // need to sort constructor args based on index position
- Map<Integer, Object> sorted = new TreeMap<>(def.getConstructors());
- for (Object val : sorted.values()) {
- String text = val.toString();
- if (!StringHelper.isQuoted(text)) {
- text = "\"" + text + "\"";
- }
- ctr.add(text);
- }
- type = type + "(" + ctr + ")";
- }
+ }
- final Object target = PropertyBindingSupport.resolveBean(getCamelContext(), type);
+ public Object newInstance(RegistryBeanDefinition def, CamelContext context) throws Exception {
- if (def.getProperties() != null && !def.getProperties().isEmpty()) {
- PropertyBindingSupport.setPropertiesOnTarget(getCamelContext(), target, def.getProperties());
- }
-
- bindBean(def, name, target);
+ String type = def.getType();
+ if (!type.startsWith("#")) {
+ type = "#class:" + type;
+ }
- } catch (Exception e) {
- if (delayIfFailed) {
- delayedRegistrations.add(def);
- } else {
- LOG.warn("Error creating bean: {} due to: {}. This exception is ignored.", type, e.getMessage(), e);
+ // factory bean/method
+ if (def.getFactoryBean() != null && def.getFactoryMethod() != null) {
+ type = type + "#" + def.getFactoryBean() + ":" + def.getFactoryMethod();
+ } else if (def.getFactoryMethod() != null) {
+ type = type + "#" + def.getFactoryMethod();
+ }
+ // property binding support has constructor arguments as part of the type
+ StringJoiner ctr = new StringJoiner(", ");
+ if (def.getConstructors() != null && !def.getConstructors().isEmpty()) {
+ // need to sort constructor args based on index position
+ Map<Integer, Object> sorted = new TreeMap<>(def.getConstructors());
+ for (Object val : sorted.values()) {
+ String text = val.toString();
+ if (!StringHelper.isQuoted(text)) {
+ text = "\"" + text + "\"";
}
+ ctr.add(text);
}
+ type = type + "(" + ctr + ")";
}
+
+ final Object target = PropertyBindingSupport.resolveBean(context, type);
+
+ if (def.getProperties() != null && !def.getProperties().isEmpty()) {
+ PropertyBindingSupport.setPropertiesOnTarget(context, target, def.getProperties());
+ }
+
+ return target;
}
protected void bindBean(RegistryBeanDefinition def, String name, Object target) throws Exception {
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/main/java/org/apache/camel/dsl/yaml/deserializers/BeansDeserializer.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/main/java/org/apache/camel/dsl/yaml/deserializers/BeansDeserializer.java
index 905d5a4ce21..4224881e130 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/main/java/org/apache/camel/dsl/yaml/deserializers/BeansDeserializer.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/main/java/org/apache/camel/dsl/yaml/deserializers/BeansDeserializer.java
@@ -18,6 +18,7 @@ package org.apache.camel.dsl.yaml.deserializers;
import java.util.ArrayList;
import java.util.HashSet;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -35,8 +36,11 @@ import org.apache.camel.spi.annotations.YamlIn;
import org.apache.camel.spi.annotations.YamlProperty;
import org.apache.camel.spi.annotations.YamlType;
import org.apache.camel.support.PropertyBindingSupport;
+import org.apache.camel.util.KeyValueHolder;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.snakeyaml.engine.v2.api.ConstructNode;
import org.snakeyaml.engine.v2.nodes.Node;
import org.snakeyaml.engine.v2.nodes.SequenceNode;
@@ -51,7 +55,10 @@ import org.snakeyaml.engine.v2.nodes.SequenceNode;
})
public class BeansDeserializer extends YamlDeserializerSupport implements ConstructNode {
+ public static final Logger LOG = LoggerFactory.getLogger(BeansDeserializer.class);
+
private final Set<String> beanCache = new HashSet<>();
+ private final Map<String, KeyValueHolder<Object, String>> beansToDestroy = new LinkedHashMap<>();
@Override
public Object construct(Node node) {
@@ -129,22 +136,19 @@ public class BeansDeserializer extends YamlDeserializerSupport implements Constr
CamelContext camelContext,
List<RegistryBeanDefinition> delayedRegistrations,
RegistryBeanDefinition def, boolean delayIfFailed) {
- try {
- // to support hot reloading of beans then we need to unbind old existing first
- String name = def.getName();
- Object bean = newInstance(def, camelContext);
- camelContext.getRegistry().unbind(name);
- camelContext.getRegistry().bind(name, bean);
-
- // register bean in model
- Model model = camelContext.getCamelContextExtension().getContextPlugin(Model.class);
- model.addRegistryBean(def);
+ String name = def.getName();
+ String type = def.getType();
+ try {
+ Object target = newInstance(def, camelContext);
+ bindBean(camelContext, def, name, target);
} catch (Exception e) {
if (delayIfFailed) {
delayedRegistrations.add(def);
} else {
- throw new RuntimeException(e);
+ String msg
+ = name != null ? "Error creating bean: " + name + " of type: " + type : "Error creating bean: " + type;
+ throw new RuntimeException(msg, e);
}
}
}
@@ -173,4 +177,52 @@ public class BeansDeserializer extends YamlDeserializerSupport implements Constr
}
}
+ protected void bindBean(
+ CamelContext camelContext, RegistryBeanDefinition def,
+ String name, Object target)
+ throws Exception {
+
+ // destroy and unbind any existing bean
+ destroyBean(name, true);
+ camelContext.getRegistry().unbind(name);
+
+ // invoke init method and register bean
+ String initMethod = def.getInitMethod();
+ if (initMethod != null) {
+ org.apache.camel.support.ObjectHelper.invokeMethodSafe(initMethod, target);
+ }
+ camelContext.getRegistry().bind(name, target);
+
+ // remember to destroy bean on shutdown
+ if (def.getDestroyMethod() != null) {
+ beansToDestroy.put(name, new KeyValueHolder<>(target, def.getDestroyMethod()));
+ }
+
+ // register bean in model
+ Model model = camelContext.getCamelContextExtension().getContextPlugin(Model.class);
+ model.addRegistryBean(def);
+ }
+
+ protected void destroyBean(String name, boolean remove) {
+ var holder = remove ? beansToDestroy.remove(name) : beansToDestroy.get(name);
+ if (holder != null) {
+ String destroyMethod = holder.getValue();
+ Object target = holder.getKey();
+ try {
+ org.apache.camel.support.ObjectHelper.invokeMethodSafe(destroyMethod, target);
+ } catch (Exception e) {
+ LOG.warn("Error invoking destroy method: {} on bean: {} due to: {}. This exception is ignored.",
+ destroyMethod, target, e.getMessage(), e);
+ }
+ }
+ }
+
+ public void stop() throws Exception {
+ // beans should trigger destroy methods on shutdown
+ for (String name : beansToDestroy.keySet()) {
+ destroyBean(name, false);
+ }
+ beansToDestroy.clear();
+ }
+
}
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoaderSupport.java b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoaderSupport.java
index bfd11e5212f..d7e77b54cb9 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoaderSupport.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoaderSupport.java
@@ -113,4 +113,8 @@ public abstract class YamlRoutesBuilderLoaderSupport extends RouteBuilderLoaderS
return null;
}
+ @Override
+ protected void doStop() throws Exception {
+ beansDeserializer.stop();
+ }
}
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/BeansTest.groovy b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/BeansTest.groovy
index 1d3209c6627..e7e2185ddef 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/BeansTest.groovy
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/BeansTest.groovy
@@ -19,6 +19,7 @@ package org.apache.camel.dsl.yaml
import org.apache.camel.dsl.yaml.support.YamlTestSupport
import org.apache.camel.dsl.yaml.support.model.MyBean
import org.apache.camel.dsl.yaml.support.model.MyCtrBean
+import org.apache.camel.dsl.yaml.support.model.MyDestroyBean
import org.apache.camel.dsl.yaml.support.model.MyFacBean
import org.apache.camel.dsl.yaml.support.model.MyFacHelper
@@ -178,5 +179,36 @@ class BeansTest extends YamlTestSupport {
}
}
+ def "beans with init destroy"() {
+ when:
+ loadRoutes """
+ - beans:
+ - name: myBean
+ type: ${MyDestroyBean.class.name}
+ initMethod: initMe
+ destroyMethod: destroyMe
+ constructors:
+ 0: 'fac1'
+ 1: 'fac2'
+ properties:
+ age: 43
+ """
+
+ then:
+
+ MyDestroyBean.initCalled.get() == true
+ MyDestroyBean.destroyCalled.get() == false
+
+ with(context.registry.lookupByName('myBean'), MyDestroyBean) {
+ it.field1 == 'fac1'
+ it.field2 == 'fac2'
+ it.age == 43
+ }
+
+ context.stop()
+
+ MyDestroyBean.initCalled.get() == true
+ MyDestroyBean.destroyCalled.get() == true
+ }
}
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/support/model/MyDestroyBean.groovy b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/support/model/MyDestroyBean.groovy
new file mode 100644
index 00000000000..f1a563c058f
--- /dev/null
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/support/model/MyDestroyBean.groovy
@@ -0,0 +1,62 @@
+/*
+ * 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.dsl.yaml.support.model
+
+import java.util.concurrent.atomic.AtomicBoolean
+
+class MyDestroyBean {
+
+ public static final AtomicBoolean initCalled = new AtomicBoolean();
+ public static final AtomicBoolean destroyCalled = new AtomicBoolean();
+
+ String field1
+ String field2
+ int age;
+
+ public MyDestroyBean(String field1, String field2) {
+ this.field1 = field1
+ this.field2 = field2
+ }
+
+ String getField1() {
+ return field1
+ }
+
+ String getField2() {
+ return field2
+ }
+
+ int getAge() {
+ return age
+ }
+
+ void setAge(int age) {
+ this.age = age
+ }
+
+ String hello(String body) {
+ return field1 + " " + body + ". I am " + field2 + " and " + age + " years old!";
+ }
+
+ void initMe() {
+ initCalled.set(true);
+ }
+
+ void destroyMe() {
+ destroyCalled.set(true);
+ }
+}
[camel] 01/04: CAMEL-19808: camel-core-model - Add init and destroy method to
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch init-bean
in repository https://gitbox.apache.org/repos/asf/camel.git
commit f08bb1dd23647d608ba6a2a83868e63005121eff
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Sep 26 14:25:54 2023 +0200
CAMEL-19808: camel-core-model - Add init and destroy method to <bean>
---
.../camel/model/app/RegistryBeanDefinition.java | 20 ++++++++++++++++++++
.../java/org/apache/camel/xml/in/ModelParser.java | 2 ++
.../java/org/apache/camel/xml/out/ModelWriter.java | 2 ++
.../org/apache/camel/xml/LwModelToXMLDumper.java | 6 ++++++
.../apache/camel/xml/jaxb/JaxbModelToXMLDumper.java | 7 ++++++-
.../java/org/apache/camel/yaml/out/ModelWriter.java | 2 ++
.../org/apache/camel/yaml/LwModelToYAMLDumper.java | 6 ++++++
.../main/xml/blueprint/BlueprintXmlBeansHandler.java | 8 ++++++++
.../camel/main/xml/spring/SpringXmlBeansHandler.java | 6 ++++++
.../dsl/yaml/deserializers/ModelDeserializers.java | 12 ++++++++++++
.../src/generated/resources/schema/camelYamlDsl.json | 6 ++++++
11 files changed, 76 insertions(+), 1 deletion(-)
diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/app/RegistryBeanDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/app/RegistryBeanDefinition.java
index 70100e769cd..ad764a8aa71 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/app/RegistryBeanDefinition.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/app/RegistryBeanDefinition.java
@@ -47,6 +47,10 @@ public class RegistryBeanDefinition implements ResourceAware {
@XmlAttribute(required = true)
private String type;
@XmlAttribute
+ private String initMethod;
+ @XmlAttribute
+ private String destroyMethod;
+ @XmlAttribute
private String factoryMethod;
@XmlAttribute
private String factoryBean;
@@ -73,6 +77,22 @@ public class RegistryBeanDefinition implements ResourceAware {
this.type = type;
}
+ public String getInitMethod() {
+ return initMethod;
+ }
+
+ public void setInitMethod(String initMethod) {
+ this.initMethod = initMethod;
+ }
+
+ public String getDestroyMethod() {
+ return destroyMethod;
+ }
+
+ public void setDestroyMethod(String destroyMethod) {
+ this.destroyMethod = destroyMethod;
+ }
+
public String getFactoryMethod() {
return factoryMethod;
}
diff --git a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java
index 7dcff72a36b..3e06002b2b6 100644
--- a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java
+++ b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java
@@ -1634,8 +1634,10 @@ public class ModelParser extends BaseParser {
protected RegistryBeanDefinition doParseRegistryBeanDefinition() throws IOException, XmlPullParserException {
return doParse(new RegistryBeanDefinition(), (def, key, val) -> {
switch (key) {
+ case "destroyMethod": def.setDestroyMethod(val); break;
case "factoryBean": def.setFactoryBean(val); break;
case "factoryMethod": def.setFactoryMethod(val); break;
+ case "initMethod": def.setInitMethod(val); break;
case "name": def.setName(val); break;
case "type": def.setType(val); break;
default: return false;
diff --git a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java
index 4e228274c16..275afafab9c 100644
--- a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java
+++ b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java
@@ -2567,7 +2567,9 @@ public class ModelWriter extends BaseWriter {
throws IOException {
startElement(name);
doWriteAttribute("factoryMethod", def.getFactoryMethod());
+ doWriteAttribute("initMethod", def.getInitMethod());
doWriteAttribute("name", def.getName());
+ doWriteAttribute("destroyMethod", def.getDestroyMethod());
doWriteAttribute("type", def.getType());
doWriteAttribute("factoryBean", def.getFactoryBean());
doWriteElement("constructors", new BeanConstructorsAdapter().marshal(def.getConstructors()), this::doWriteBeanConstructorsDefinition);
diff --git a/core/camel-xml-io/src/main/java/org/apache/camel/xml/LwModelToXMLDumper.java b/core/camel-xml-io/src/main/java/org/apache/camel/xml/LwModelToXMLDumper.java
index 7cec1ce562b..9ad46e95510 100644
--- a/core/camel-xml-io/src/main/java/org/apache/camel/xml/LwModelToXMLDumper.java
+++ b/core/camel-xml-io/src/main/java/org/apache/camel/xml/LwModelToXMLDumper.java
@@ -331,6 +331,12 @@ public class LwModelToXMLDumper implements ModelToXMLDumper {
if (b.getFactoryMethod() != null) {
buffer.write(String.format(" factory-method=\"%s\"", b.getFactoryMethod()));
}
+ if (b.getInitMethod() != null) {
+ buffer.write(String.format(" init-method=\"%s\"", b.getInitMethod()));
+ }
+ if (b.getDestroyMethod() != null) {
+ buffer.write(String.format(" destroy-method=\"%s\"", b.getDestroyMethod()));
+ }
buffer.write(">\n");
if (b.getConstructors() != null && !b.getConstructors().isEmpty()) {
buffer.write(String.format(" <constructors>%n"));
diff --git a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
index 02e3078c6b1..6e6a1abe149 100644
--- a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
+++ b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
@@ -338,13 +338,18 @@ public class JaxbModelToXMLDumper implements ModelToXMLDumper {
type = type.substring(7);
}
buffer.write(String.format(" <bean name=\"%s\" type=\"%s\"", b.getName(), type));
- String factoryMethod = b.getFactoryMethod();
if (b.getFactoryBean() != null) {
buffer.write(String.format(" factory-bean=\"%s\"", b.getFactoryBean()));
}
if (b.getFactoryMethod() != null) {
buffer.write(String.format(" factory-method=\"%s\"", b.getFactoryMethod()));
}
+ if (b.getInitMethod() != null) {
+ buffer.write(String.format(" init-method=\"%s\"", b.getInitMethod()));
+ }
+ if (b.getDestroyMethod() != null) {
+ buffer.write(String.format(" destroy-method=\"%s\"", b.getDestroyMethod()));
+ }
buffer.write(">\n");
if (b.getConstructors() != null && !b.getConstructors().isEmpty()) {
buffer.write(String.format(" <constructors>%n"));
diff --git a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java
index 08c098e8cf0..cd2bdb66105 100644
--- a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java
+++ b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java
@@ -2567,7 +2567,9 @@ public class ModelWriter extends BaseWriter {
throws IOException {
startElement(name);
doWriteAttribute("factoryMethod", def.getFactoryMethod());
+ doWriteAttribute("initMethod", def.getInitMethod());
doWriteAttribute("name", def.getName());
+ doWriteAttribute("destroyMethod", def.getDestroyMethod());
doWriteAttribute("type", def.getType());
doWriteAttribute("factoryBean", def.getFactoryBean());
doWriteElement("constructors", new BeanConstructorsAdapter().marshal(def.getConstructors()), this::doWriteBeanConstructorsDefinition);
diff --git a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
index 2507043724a..46decad3e35 100644
--- a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
+++ b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
@@ -320,6 +320,12 @@ public class LwModelToYAMLDumper implements ModelToYAMLDumper {
if (b.getFactoryMethod() != null) {
buffer.write(String.format(" factoryMethod: \"%s\"%n", b.getFactoryMethod()));
}
+ if (b.getInitMethod() != null) {
+ buffer.write(String.format(" initMethod: \"%s\"%n", b.getInitMethod()));
+ }
+ if (b.getDestroyMethod() != null) {
+ buffer.write(String.format(" destroyMethod: \"%s\"%n", b.getDestroyMethod()));
+ }
if (b.getConstructors() != null && !b.getConstructors().isEmpty()) {
buffer.write(String.format(" constructors:%n"));
int counter = 0;
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/xml/blueprint/BlueprintXmlBeansHandler.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/xml/blueprint/BlueprintXmlBeansHandler.java
index cba1329dd65..c5037814ebb 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/xml/blueprint/BlueprintXmlBeansHandler.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/xml/blueprint/BlueprintXmlBeansHandler.java
@@ -116,6 +116,14 @@ public class BlueprintXmlBeansHandler {
if (fm != null) {
rrd.setFactoryMethod(fm);
}
+ String im = XmlHelper.getAttribute(node, "init-method");
+ if (im != null) {
+ rrd.setInitMethod(fm);
+ }
+ String dm = XmlHelper.getAttribute(node, "destroy-method");
+ if (dm != null) {
+ rrd.setDestroyMethod(dm);
+ }
// constructor arguments
Map<Integer, Object> constructors = new LinkedHashMap<>();
rrd.setConstructors(constructors);
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/xml/spring/SpringXmlBeansHandler.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/xml/spring/SpringXmlBeansHandler.java
index b371c5a2ac0..7fc231df481 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/xml/spring/SpringXmlBeansHandler.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/xml/spring/SpringXmlBeansHandler.java
@@ -243,6 +243,12 @@ public class SpringXmlBeansHandler {
if (def.getFactoryMethodName() != null) {
rrd.setFactoryMethod(def.getFactoryMethodName());
}
+ if (def.getInitMethodName() != null) {
+ rrd.setInitMethod(def.getInitMethodName());
+ }
+ if (def.getDestroyMethodName() != null) {
+ rrd.setDestroyMethod(def.getDestroyMethodName());
+ }
// constructor arguments
if (def.hasConstructorArgumentValues()) {
Map<Integer, Object> constructors = new LinkedHashMap<>();
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
index 45c0068500c..05674840b1e 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
@@ -12349,8 +12349,10 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
order = org.apache.camel.dsl.yaml.common.YamlDeserializerResolver.ORDER_LOWEST - 1,
properties = {
@YamlProperty(name = "constructors", type = "object"),
+ @YamlProperty(name = "destroy-method", type = "string"),
@YamlProperty(name = "factory-bean", type = "string"),
@YamlProperty(name = "factory-method", type = "string"),
+ @YamlProperty(name = "init-method", type = "string"),
@YamlProperty(name = "name", type = "string", required = true),
@YamlProperty(name = "properties", type = "object"),
@YamlProperty(name = "type", type = "string", required = true)
@@ -12375,6 +12377,11 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
target.setConstructors(val);
break;
}
+ case "destroy-method": {
+ String val = asText(node);
+ target.setDestroyMethod(val);
+ break;
+ }
case "factory-bean": {
String val = asText(node);
target.setFactoryBean(val);
@@ -12385,6 +12392,11 @@ public final class ModelDeserializers extends YamlDeserializerSupport {
target.setFactoryMethod(val);
break;
}
+ case "init-method": {
+ String val = asText(node);
+ target.setInitMethod(val);
+ break;
+ }
case "name": {
String val = asText(node);
target.setName(val);
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json
index 4c298bdbda4..cc9133aad35 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json
@@ -8026,12 +8026,18 @@
"constructors" : {
"type" : "object"
},
+ "destroyMethod" : {
+ "type" : "string"
+ },
"factoryBean" : {
"type" : "string"
},
"factoryMethod" : {
"type" : "string"
},
+ "initMethod" : {
+ "type" : "string"
+ },
"name" : {
"type" : "string"
},