【问题标题】:Camel-Spring Boot: Route doesn't stop after executionCamel-Spring Boot:执行后路由不会停止
【发布时间】:2017-04-19 09:35:37
【问题描述】:

我正在使用 Spring Boot + Camel,配置如下。

主类

 @SpringBootApplication
public class Application extends RouteBuilder {
    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }
    @Override
    public void configure() throws Exception {
        from("sql:{{list.sql}}?dataSource=#dataSource")
        .log("process row ${body}")
        .to("stream:out");
    }}

application.properties

camel.springboot.name=ListJob
camel.springboot.main-run-controller=true

list.sql=<SELECT QUERY>

spring.datasource.driver=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://IP:3306/test
spring.datasource.username=un
spring.datasource.password=pwd

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.abc</groupId>
    <artifactId>wl-event-notification-batch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>notification</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- Camel -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.17.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>2.17.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-stream</artifactId>
            <version>2.17.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-sql</artifactId>
            <version>2.17.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jdbc</artifactId>
            <version>2.17.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot</artifactId>
            <version>2.17.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>2.17.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

由于 "camel.springboot.main-run-controller" 属性,我的 Spring Boot 在执行 SELECT 查询的无限循环中运行路由,直到我点击 CNTRL+C。如果我删除此属性,应用程序只会立即启动和停止路由,而不会完成执行。

有人可以帮我,让我的路线开始,执行步骤并在我运行 Spring Boot 应用程序时停止吗?

【问题讨论】:

  • 您只想从数据库中选择一次,然后处理并停止?
  • 是的,我希望我的路线在执行我的选择、查询过程并停止后运行。
  • 使用计时器并告诉它运行一次,然后你可以从一个路由中停止一个路由:camel.apache.org/how-can-i-stop-a-route-from-a-route.html
  • 不幸的是,该链接没有帮助,因为没有代码,只有一条错误消息:Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20。所以我看不到应该做什么:-(

标签: spring-boot apache-camel camel-sql


【解决方案1】:

您可以使用 spring-boot 属性:

# handle only one 1 message and then stop the route
camel.springboot.duration-max-messages=1
# run for 180 seconds and then gracefully shutdown
camel.springboot.duration-max-seconds=180
# a polling consumer will cancel the graceful shutdown so set the shutdownTimeout to a minimum
camel.springboot.shutdownTimeout=1

【讨论】:

    【解决方案2】:

    添加.stop()

    public void configure() throws Exception {
            from("sql:{{list.sql}}?dataSource=#dataSource")
            .log("process row ${body}")
            .stop();
        }
    

    【讨论】:

    • 这根本没有帮助,因为.stop() 仅将当前交换标记为完成并停止进一步路由
    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多