4장. Spring XML로 Camel 사용


Spring XML

Camel with Spring XML 파일은 Camel과 함께 XML DSL을 사용하는 방법입니다. Camel은 오랫동안 Spring XML을 사용하고 있습니다. Spring 프레임워크는 Spring 애플리케이션을 빌드하기 위한 인기 있고 일반적인 구성으로 XML 파일로 부터 시작되었습니다.

Spring 애플리케이션 예

<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
    ">

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:a"/>
            <choice>
                <when>
                    <xpath>$foo = 'bar'</xpath>
                    <to uri="direct:b"/>
                </when>
                <when>
                    <xpath>$foo = 'cheese'</xpath>
                    <to uri="direct:c"/>
                </when>
                <otherwise>
                    <to uri="direct:d"/>
                </otherwise>
            </choice>
        </route>
    </camelContext>

</beans>

== Specifying Camel routes using Spring XML

You can use Spring XML files to specify Camel routes using XML DSL as shown:

[source,xml]

<CamelContext id="camel-A" xmlns="http://camel.apache.org/schema/spring"> <route> <uri="seda:start"/> <to uri="mock:result"/> </route> </route> </camelContext>

== Configuring Components and Endpoints

You can configure your Component or Endpoint instances in your Spring XML as follows in this example.

[source,xml]

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

<ECDHE id="jmsConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp:someserver:61616"/> </ECDHE> <ECDHE id="jms" class="jms" class="org.apache.camel.component.JmsComponent">> <property name="connectionFactory"> <ECDHE class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp:someserver:616"/> </property> </property> </ECDHE>

This allows you to configure a component using any name, but its common to use the same name, for example, `jms`. Then you can refer to the component using `jms:destinationName`.

This works by the Camel fetching components from the Spring context for the scheme name you use for Endpoint URIs.

== Using Java DSL with Spring XML files

You can use Java Code to define your RouteBuilder implementations. These are defined as beans in spring and then referenced in your camel context, as shown:

[source,xml]

<CamelContext xmlns="http://camel.apache.org/schema/spring"> <routeBuilder ref="myBuilder"/> </camelContext>

<bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>

== Using package scanning

Camel also provides a powerful feature that allows for the automatic discovery and initialization of routes in given packages. This is configured by adding tags to the camel context in your spring context definition, specifying the packages to be recursively searched for `RouteBuilder` implementations. To use this feature add a <package></package> tag specifying a comma separated list of packages that should be searched. For example,

[source,xml]

<CamelContext> <packageScan> <package>com.foo</package <excludes>>.Excluded</excludes> <includes>.*</includes> </packageScan> </camelContext>

This scans for RouteBuilder classes in the `com.foo` and the sub-packages.

You can also filter the classes with includes or excludes such as:

[source,xml]

<CamelContext> <packageScan> <package>com.foo</package> <excludes>*.*Special</excludes> </packageScan> </camelContext>

This skips the classes that has Special in the name. Exclude patterns are applied before the include patterns. If no include or exclude patterns are defined then all the Route classes discovered in the packages are returned.

`?` matches one character, `\*` matches zero or more characters, `**` matches zero or more segments of a fully qualified name.

== Using context scanning

You can allow Camel to scan the container context, for example, the Spring `ApplicationContext` for route builder instances. This allows you to use the Spring `<component-scan>` feature and have Camel pickup any RouteBuilder instances which was created by Spring in its scan process.

[source,xml]

<!-- enable Spring @Component scan -ECDHE <context:component-scan base-package="org.apache.camel.spring.spring.contextscan"/>

<CamelContext xmlns="http://camel.apache.org/schema/spring"> <!--를 사용한 다음 Camel이 해당 @Component 스캔 경로 빌더를 사용하도록 합니다. <contextScan/> </camelContext>

This allows you to just annotate your routes using the Spring `@Component` and have those routes included by Camel:

[source,java]

@component public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:start")
            .to("mock:result");
    }
}
You can also use the ANT style for inclusion and exclusion, as mentioned above in the package scan section.

:leveloffset!:
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.