Apache Camel - 21 - Embedding Camel in a Spring application(将Camel嵌入到Spring应用中)

版权声明:本文为博主原创文章,可以转载,但请注明出处。 https://blog.csdn.net/Simba_cheng/article/details/81318944

将Camel嵌入到Spring应用中

Apache Camel相关代码已经上传GitHub,需要的自取:GitHub - Apache Camel 完整Demo

如果觉得还行,麻烦点个Star

这个Demo,并没有按照书中的栗子,我稍作了修改。

假定你会使用Eclipse或IDEA 编译器,以及Maven。

如果不会使用,抱歉,这里也不会讲,环境搭建没什么好说的,就像你平时往pom.xml中添加一个jar配置一样简单。

项目结构:

在pom.xml中新增一个jar,版本随意,这里用的比较新

<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-spring -->
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-spring</artifactId>
  <version>2.22.0</version>
</dependency>

Spring的主配置XML:applicationContext.xml

<?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.xsd
               http://camel.apache.org/schema/spring
               http://camel.apache.org/schema/spring/camel-spring.xsd">

    <bean id="fileConverter" class="com.server.process.FileConvertProcessor"></bean>

    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="file:./input?delay=30000"/>
            <process ref="fileConverter"/>
            <to uri="file:./output"/>
        </route>
    </camelContext>

</beans>

注意哦,在Spring主配置文件中,上面的命名空间是很重要的,缺少命名空间,会导致程序报错。

<camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
......
</camelContext>

使用camelContext标签来配置Camel 上下文,其中的配置,就是camel 的配置信息,而不是Spring的配置信息。

然后可以使用XML DSL在camelContextelement中定义路由:

<route>
        .....
</route>

配置了camelContext节点之后,Spring会为我们创建SpringCamelContext对象。

并且初始化其中定义的路由信息,等到Spring上下文启动的时候,一并启动。

使用Java DSL 代替 Spring XML DSL

使用Spring XML DSL 并不是强制的,也可以使用Java DSL 代替。

下面对代码稍微进行一些调整:

新增 FileRoutes 路由类:

package com.server.routes;

import com.server.process.FileConvertProcessor;
import org.apache.camel.builder.RouteBuilder;

/**
 * @author 
 */
public class FileRoutes extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("file:./input?delay=30000").process(new FileConvertProcessor()).to("file:./output");

    }
}

修改下applicationContext.xml

<?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.xsd
               http://camel.apache.org/schema/spring
               http://camel.apache.org/schema/spring/camel-spring.xsd">

    <!--<bean id="fileConverter" class="com.server.process.FileConvertProcessor"></bean>

    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="file:./input?delay=30000"/>
            <process ref="fileConverter"/>
            <to uri="file:./output"/>
        </route>
    </camelContext>-->

    <bean id="fileRoutes" class="com.server.routes.FileRoutes"></bean>
    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <routeBuilder ref="fileRoutes"/>
    </camelContext>
</beans>

将原来的注释掉。

上面这个挺好理解的吧,将fileRoutes以Bean的方式注入到camelContext上下文中。

当然,可以再camelContext中使用多个routeBuilder(路由)

自动扫描包下的RouteBuilders

(还是上面的工程结构)

如果在同一个包中定义了许多RouteBuilder,Camel可以使用扫描该包并实例化它找到的所有路由

先看下applicationContext.xml中的配置:

<?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.xsd
               http://camel.apache.org/schema/spring
               http://camel.apache.org/schema/spring/camel-spring.xsd">

    <!--<bean id="fileConverter" class="com.server.process.FileConvertProcessor"></bean>

    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="file:./input?delay=30000"/>
            <process ref="fileConverter"/>
            <to uri="file:./output"/>
        </route>
    </camelContext>-->

    <!--<bean id="fileRoutes" class="com.server.routes.FileRoutes"></bean>
    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <routeBuilder ref="fileRoutes"/>
    </camelContext>-->

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <packageScan>
            <package>com.server.routes</package>
        </packageScan>
    </camelContext>

</beans>

在packageScan节点中可以添加多个package子节点。

还可以使用通配符按照名称进行过滤RouteBuilders(具体例子后面再说)。

pring提供了组件扫描的功能。

启动之后,Spring应用程序上下文会递归扫描指定的包,并实例化其中使用org.springframework.stereotype.Component注释的任何类。

修改下FileRoutes:

package com.server.routes;

import com.server.process.FileConvertProcessor;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

/**
 * @author
 */
@Component
public class FileRoutes extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("file:./input?delay=30000").process(new FileConvertProcessor()).to("file:./output");

    }
}

然后直接运行。

我本地测试没有问题,运行没有报错,测试结果我就不贴出来了。

 

参考来源:《Apache Camel Developer's Cookbook》

电子书我已经上传了,需要的自取 《Apache Camel Developer's Cookbook》

猜你喜欢

转载自blog.csdn.net/Simba_cheng/article/details/81318944