6.6. camel-sql
SQL 구성 요소를 사용하면 JDBC 쿼리를 사용하여 데이터베이스를 사용할 수 있습니다. 이 구성 요소와 JDBC 구성 요소의 차이점은 SQL의 경우 쿼리는 끝점의 속성이며 쿼리에 전달된 매개 변수로 메시지 페이로드를 사용한다는 것입니다.
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("sql:select name from information_schema.users?dataSource=java:jboss/datasources/ExampleDS")
.to("direct:end");
}
});참고
위에 표시된 JNDI 데이터 소스 조회는 DefaultCamelContext 를 구성하는 경우에만 작동합니다. 아래 CdiCamelContext 및 SpringCamelContext 예제를 참조하십시오.
camel-cdi 구성 요소와 함께 사용하면 Java EE 주석을 사용하면 Camel에서 데이터 소스를 사용할 수 있습니다. 이 예에서는 Camel이 원하는 데이터 소스를 검색할 수 있도록 @Named 주석을 사용합니다.
public class DatasourceProducer {
@Resource(name = "java:jboss/datasources/ExampleDS")
DataSource dataSource;
@Produces
@Named("wildFlyExampleDS")
public DataSource getDataSource() {
return dataSource;
}
}
이제 camel-sql 엔드포인트 구성의 dataSource 매개변수를 통해 데이터 소스를 참조할 수 있습니다.
@ApplicationScoped
@ContextName("camel-sql-cdi-context")
@Startup
public class CdiRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
from("sql:select name from information_schema.users?dataSource=wildFlyExampleDS")
.to("direct:end");
}
}camel-spring 을 사용할 때 경로 구성은 다음과 같습니다.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
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
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<jee:jndi-lookup id="wildFlyExampleDS" jndi-name="java:jboss/datasources/ExampleDS"/>
<camelContext id="sql-spring-context" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="sql:select name from information_schema.users?dataSource=#wildFlyExampleDS" />
<to uri="direct:end" />
</route>
</camelContext>
</beans>6.6.1. Spring JDBC XML 네임스페이스 지원
다음 Spring JDBC XML 구성 지원이 지원됩니다.
jdbc:embedded-database
<jdbc:embedded-database id="datasource" type="H2"> <jdbc:script location="db-schema.sql"/> </jdbc:embedded-database>
참고
JBoss EAP는 이에 대한 기본 지원이 있으므로 H2 데이터베이스만 기본적으로 지원됩니다. 다른 포함된 데이터베이스 공급자를 사용하려면 적절한 데이터베이스 드라이버를 설치해야 합니다.
jdbc:initialize-database
<jdbc:initialize-database data-source="datasource"> <jdbc:script location="classpath:db-init.sql"/> </jdbc:initialize-database>