gpt4 book ai didi

java - Apache Camel 将 XML 路由和 beans 从文件加载到 CamelContext 中

转载 作者:行者123 更新时间:2023-12-01 09:09:39 25 4
gpt4 key购买 nike

所以,现在我尝试将 XML 文件中的路由导入到 Java DSL 中。

我一直在尝试从 this link 开始但由于这是一个如此简单的示例,它并没有真正帮助我,也没有给我指出一个更复杂的示例。

我的问题是我的 Camel 路由使用 beans。 bean 类PropertiesComponentFileIdempotentRepository以及其他内容在 XML 文件中定义,以供 XML 文件中的路由使用。

我原来的 Spring 配置如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

<bean id="bean1" class="class1" />
<bean id="bean2" class="class2" />
<bean id="bean3" class="FileIdempotentRepository"> [...] </bean>
<bean id="properties" class="PropertiesComponent"> [...] </bean>

<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="{{someplace}}&amp;filter=#bean1" />
<setHeader headerName="FileRepoKey">
<simple>${file:name}-${file:modified}</simple>
</setHeader>
<idempotentConsumer messageIdRepositoryRef="bean3">
<header>FileRepoKey</header>
<process ref="bean2" />
<to uri="{{otherplace}}"/>
</idempotentConsumer>
</route>
</camelContext>
</beans>

那么我如何将这些困惑的东西转换成 Java DSL 可以用来导入路由的东西呢?

通过查看该链接,我了解到我需要执行诸如转换 <camelContext> 之类的操作至<routes> 。但是留在 bean 里会给我一个错误:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.springframework.org/schema/beans", local:"beans"). Expected elements are [...]

我需要改变什么?或者我可以不在 XML 文件中包含 beans 以便链接中使用的 Java 导入它吗?

最佳答案

我想我应该以不同的方式问这个问题,也许有人会想到这种方式。

我不确定它可能会让你做噩梦。请注意。

因此,由于概念是“让东西可能从 XML 文件与 Java 一起运行”,因此产生了以下最终结果:

    public static void main(String[] args) throws Exception {

Main main = new Main();

//the XML file has a CamelContext in it.
main.setApplicationContextUri("myRoutes.xml");
main.start();//instantiates the CamelContext so we can use it in Java
List<CamelContext> camelContexts = main.getCamelContexts(); //should only have 1 item in the list
CamelContext context = camelContexts.get(0);

//in order to add a component to the registry the following is needed for set up
// afterwards, should just be able to add anything to the registry with registry.put("name", object)
final SimpleRegistry registry = new SimpleRegistry();
final CompositeRegistry compositeRegistry = new CompositeRegistry();
compositeRegistry.addRegistry(context.getRegistry());
compositeRegistry.addRegistry(registry);
((DefaultCamelContext) context).setRegistry(compositeRegistry);

final FileIdempotentRepository myFileStore = new FileIdempotentRepository();
File myFile = new File("idempotentRepoFiles/myFileStore.txt");

final TimeStampFileFilter<?> myFileFilter = new TimeStampFileFilter<Object>(0L);
registry.put("myFileFilter", myFileFilter);

//512MB
myFileStore.setMaxFileStoreSize(536870912L);
myFileStore.setFileStore(myFile);
myFileStore.setCacheSize(100000);

//add a route to the CamelContext that was initially created in the XML file
context.addRoutes(new RouteBuilder() {

@Override
public void configure() throws Exception {
onException(myException.class)
.handled(true);
onException(GenericFileOperationFailedException.class)
.onException(SocketException.class)
.maximumRedeliveries(2)
.redeliveryDelay(5000L)
;
Processor myProcessor = new myProcessor();
from("{{myStart}}&filter=#myFileFilter")
.setHeader("myFileRepoKey", simple("${file:name}-${file:modified}"))
.idempotentConsumer(header("myFileRepoKey"), myFileStore)
.process(myProcessor)
.to("{{myEnd}}")
;

}

});

context.start();
main.run();
}
<小时/>

基本上:在 Spring XML 文件中创建一个 CamelContext,初始化它,抓取它,修改它以包含 Java 中内置的路由。

关于java - Apache Camel 将 XML 路由和 beans 从文件加载到 CamelContext 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40998793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com