1

要求是将表中可用的 spring 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 中添加了另外一行。现在如果我执行刷新 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 定义。是的,它是第一次工作,但是当我下次尝试使用相同的技术刷新时,“myContext”不在 GenericXmlApplicationContext 实例中。我认为问题是,当我创建新的 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();

在此先感谢..如果有人可以提供有关此的一些信息..

4

0 回答 0