[Camel in Action] 2-2. Camel XML DSL과 Spring 통합 – 설정 기반 라우트 정의

XML DSL – 설정 파일로 라우트 관리

XML DSL은 코드 변경 없이 라우팅 규칙을 수정할 수 있다는 장점이 있습니다. 특히 Spring XML 설정 방식에 익숙한 팀이나, 운영 중 동적 변경이 필요한 경우에 유용합니다.

기본 XML DSL 구조

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:camel="http://camel.apache.org/schema/spring">

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

        <route id="order-route">
            <from uri="file:orders/inbox?noop=true"/>
            <log message="파일 수신: ${header.CamelFileName}"/>
            <choice>
                <when>
                    <simple>${header.CamelFileName} contains 'urgent'</simple>
                    <to uri="direct:urgentOrder"/>
                </when>
                <otherwise>
                    <to uri="direct:normalOrder"/>
                </otherwise>
            </choice>
        </route>

    </camelContext>
</beans>

Spring Boot + application.yml 설정

Spring Boot 환경에서는 application.yml로 Camel 기본 설정을 관리합니다.

camel:
  springboot:
    name: MyApp
    main-run-controller: true
  dataformat:
    json-jackson:
      auto-discover-object-mapper: true

# Camel 라우트 자동 검색 패턴
  routes-include-pattern: "classpath:camel/*.xml"

Bean 참조와 Spring 의존성 주입

XML DSL에서 Spring Bean을 참조하는 방법입니다.

<!-- Spring Bean 정의 -->
<bean id="orderService" class="com.example.OrderService"/>

<!-- Camel 라우트에서 Bean 참조 -->
<route>
    <from uri="direct:processOrder"/>
    <bean ref="orderService" method="process"/>
    <to uri="jms:queue:completed"/>
</route>

프로퍼티 플레이스홀더 활용

환경별 설정값을 외부화하는 것은 Camel에서도 중요합니다.

# application.properties
order.input.dir=orders/inbox
jms.queue.name=orderQueue
retry.count=3
<!-- XML DSL에서 프로퍼티 사용 -->
<route>
    <from uri="file:{{order.input.dir}}?noop=true"/>
    <to uri="jms:queue:{{jms.queue.name}}"/>
</route>
// Java DSL에서 프로퍼티 사용
from("file:{{order.input.dir}}?noop=true")
    .to("jms:queue:{{jms.queue.name}}");

Java DSL vs XML DSL 선택 기준

  • Java DSL 선택: 복잡한 조건 로직, IDE 지원 필요, 타입 안전성, 팀이 Java에 익숙
  • XML DSL 선택: 빌드 없이 라우트 변경 필요, 비개발자도 이해해야 하는 경우, 레거시 Spring XML 환경

YAML DSL – 최신 트렌드

Camel 3.x 이후 YAML DSL도 지원합니다. Camel K (클라우드 네이티브 통합)에서 주로 사용됩니다.

- route:
    id: order-route
    from:
      uri: "file:orders/inbox"
      parameters:
        noop: true
    steps:
      - log:
          message: "파일 수신: ${header.CamelFileName}"
      - to: "jms:queue:orders"

Leave a Comment