将xml骆驼上下文重新加载到已经进行过注册的骆驼上下文

时间:2020-11-07 04:39:24

标签: spring spring-boot apache-camel spring-camel apache-camel-3

要求是将表中可用的spring xml camel上下文加载到应用程序中(单个XML既包含上下文又包含路由)。应用程序启动时,需要从表加载到应用程序中读取此xml。

例如,假设表中有以下xml,并且需要从表中读取以下xml并在sprint引导应用程序启动时加载到应用程序中。

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://camel.apache.org/schema/spring
                                http://camel.apache.org/schema/spring/camel-spring.xsd">                  
    <camelContext id="myContext">       
        <route id="FileReadRoute">
            <from uri="file:///Users/Data?noop=false&amp;idempotent=false&amp;delay=30s" />
            <split streaming="true">
                <tokenize token="\n"/>
                <log message="print the body message: ${body}" />
            </split>
        </route>
    </camelContext>
</beans>

我能够从表中读取xml并使用下面的代码加载到应用程序中,并使用“ myContext”名称加载上下文,因为这是我从xml传递的名称。

GenericXmlApplicationContext  newContext = new GenericXmlApplicationContext();
Resource resource = new ByteArrayResource(resource.getBytes()); //resource holds the XML context information from table.
newContext.load(resource);
newContext.refresh();

现在第二个要求是,在运行时不关闭应用程序的情况下,我想刷新骆驼上下文。例如,如果我对表中的XML上下文进行了一些更改,并且执行了api(称为“刷新api”),则现有的骆驼上下文将被关闭,并使用相同的上下文名称重新加载新数据。

示例:

 <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://camel.apache.org/schema/spring
                                    http://camel.apache.org/schema/spring/camel-spring.xsd">                  
        <camelContext id="myContext">       
            <route id="FileReadRoute">
                <from uri="file:///Users/Data?noop=false&amp;idempotent=false&amp;delay=30s" />
                <split streaming="true">
                    <tokenize token="\n"/>
                    <log message="print the body message: ${body}" />
                    <log message="newly updated log message, print the body message: ${body}" />
                </split>
            </route>
        </camelContext>
    </beans>

我在xml中添加了另一行。现在,如果我执行refresh api,它将刷新现有上下文并加载更新的上下文。但是这里发生的是,它没有在骆驼上下文中注销现有上下文“ myContex”,而是创建了名为“ myContext-1”的新上下文。我了解,如果已经注册过,骆驼默认会使用一种模式在Mbean中注册上下文。但我正在寻找一种不使用新名称注册的选项。我希望注销现有的名称并再次重新注册使用相同的名称。我不确定如何注销它。

<log message="newly updated log message, print the body message: ${body}" />

我实际上是在使用下面的代码来刷新它。

GenericXmlApplicationContext  newRefereshContext = new GenericXmlApplicationContext();
            newRefereshContext.setParent(applicationContext);
            newRefereshContext.setAllowBeanDefinitionOverriding(true);
            newRefereshContext.load(resource);
            newRefereshContext.refresh();
            newRefereshContext.start();

我创建GenericXmlApplicationContext()的新实例的原因是,由于抛出错误消息“上下文不能多次刷新”而出错,因此无法多次使用refresh()方法。 >

我尝试过的另一种选择是,从GenericXmlApplicationContext()实例中删除“ myContext” bean定义。是的,它是第一次工作,但是当我下次尝试使用相同的技术刷新时,GenericXmlApplicationContext实例中没有“ myContext”。我认为问题是,当我创建新的GenericXmlApplicationContext()实例时,不确定它是否已在应用程序中注册。如何获取GenericXmlApplicationContext()的所有实例并删除其中的“ myContext” bean,然后重新创建具有相同名称的bean。

以下是我之前尝试过的内容:

    @Autowired
    GenericXmlApplicationContext springContext;

CamelContext existingCamelContext = (CamelContext) springContext.getBean("myContext");
existingCamelContext.stop();
springContext.removeBeanDefinition("myContext");

    GenericXmlApplicationContext  newContext = new GenericXmlApplicationContext();
    Resource resource = new ByteArrayResource(resource.getBytes()); //resource holds the XML context information from table.
    newContext.load(resource);
    newContext.refresh();

在此先感谢您。是否有人可以提供有关此信息。

0 个答案:

没有答案