2

I am using Spring Boot + Camel with below config.

Main Class

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

My Spring Boot runs the route in infinite loop executing SELECT query because of "camel.springboot.main-run-controller" property until I hit CNTRL+C. If I remove this property, application just starts and stops the route immediately without completing the execution.

Can someone please help me, to get my route start, execute the steps and stop when I run Spring Boot application?

Kenster
  • 23,465
  • 21
  • 80
  • 106
user1637487
  • 241
  • 1
  • 9
  • 17
  • You want to select from the database only ONCE and then process that and stop? – Claus Ibsen Dec 05 '16 at 08:12
  • Yes, I want my route to run once execute my select, query process and stop. – user1637487 Dec 05 '16 at 15:16
  • Use a timer and tell it to run once, and then you can stop a route from a route: http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html – Claus Ibsen Dec 05 '16 at 15:34
  • Unfortunately the link does not help, because instead of the code there is only an error message: `Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20`. So I'm not able to see what should be done :-( – the hand of NOD Jul 02 '19 at 14:43

2 Answers2

1

You can use the spring-boot properties:

# 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
the hand of NOD
  • 1,639
  • 1
  • 18
  • 30
-2

add .stop():

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