Apache Camel定时器在Fat Jar中出现空指针异常。

huangapple go评论85阅读模式
英文:

Apache Camel Timer Nullpointer exception at Fat Jar

问题

我正在尝试在Gradle项目中使用Camel 3.5实现一个定时器,使用OpenJDK 8作为下一个版本。

from("timer://watchexpiration?fixedRate=true&period=600000&delay=0")...

但是,在使用```./gradlew build```构建了fat jar之后,并且使用```java -jar build/libs/app.jar```运行后,我在控制台上收到了以下错误:

Caused by: org.apache.camel.ResolveEndpointFailedException: 无法解析端点:timer://watchexpiration?delay=0&fixedRate=true&period=600000,原因是:无法将属性(delay=0)与名称(delay)绑定到bean上:timer://watchexpiration?delay=0&fixedRate=true&period=600000,其值为:0
    at org.apache.camel.impl.engine.AbstractCamelContext.doGetEndpoint(AbstractCamelContext.java:888)
    at org.apache.camel.impl.engine.AbstractCamelContext.getEndpoint(AbstractCamelContext.java:777)
    at org.apache.camel.support.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:58)
    at org.apache.camel.reifier.AbstractReifier.resolveEndpoint(AbstractReifier.java:177)
    at org.apache.camel.reifier.RouteReifier.doCreateRoute(RouteReifier.java:250)
    at org.apache.camel.reifier.RouteReifier.createRoute(RouteReifier.java:112)

但是,如果我使用```./gradlew run```运行,则**正常工作**,与我预期的一样。
我不想为这个项目使用任何框架。我觉得这只是一个配置问题,或者我的配置有些问题。

我该如何解决?

build.gradle
```groovy
plugins {
    id 'java'
    id 'application'
    id 'com.github.sherter.google-java-format' version '0.8'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'com.google.guava:guava:29.0-jre'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
    // Camel
    compile group: 'org.apache.camel', name: 'camel-core', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-file', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-file-watch', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-xstream', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-gson', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-rest', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-servlet', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-http', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-jackson', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-quartz', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-timer', version: '3.5.0'

    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'

    // Dev Libs
    compileOnly("org.projectlombok:lombok:1.18.12")
    annotationProcessor("org.projectlombok:lombok:1.18.12")

    compile group: 'org.apache.commons', name: 'commons-csv', version: '1.4'
}

application {
    mainClassName = 'com.eip.App'
}

configurations {
    // configuration that holds jars to include in the jar
    extraLibs
}

jar {
    manifest {
        attributes(
                'Main-Class': 'com.beam.agent.App'
        )
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

test {
    useJUnitPlatform()
}

googleJavaFormat {
    exclude '**/App.java'
}
英文:

I'm trying to implement a timer using Camel 3.5 at Gradle project with OpenJDK8 as next

     from("timer://watchexpiration?fixedRate=true&period=600000&delay=0")...

But, after build the fat jar using ./gradlew build and run as java -jar build/libs/app.jar

I receive the next error at console

Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: timer://watchexpiration?delay=0&fixedRate=true&period=600000 due to: Error binding property (delay=0) with name: delay on bean: timer://watchexpiration?delay=0&fixedRate=true&period=600000 with value: 0
at org.apache.camel.impl.engine.AbstractCamelContext.doGetEndpoint(AbstractCamelContext.java:888)
at org.apache.camel.impl.engine.AbstractCamelContext.getEndpoint(AbstractCamelContext.java:777)
at org.apache.camel.support.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:58)
at org.apache.camel.reifier.AbstractReifier.resolveEndpoint(AbstractReifier.java:177)
at org.apache.camel.reifier.RouteReifier.doCreateRoute(RouteReifier.java:250)
at org.apache.camel.reifier.RouteReifier.createRoute(RouteReifier.java:112)

But If I run using ./gradlew run then works fine as I expected.
I don't want to use any frameworks for this project. I feel this is just a config issue or something is wrong with my configuration I guess.

How can I fix it?

build.gradle

plugins {
    id 'java'
    id 'application'
    id 'com.github.sherter.google-java-format' version '0.8'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'com.google.guava:guava:29.0-jre'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
    // Camel
    compile group: 'org.apache.camel', name: 'camel-core', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-file', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-file-watch', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-xstream', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-gson', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-rest', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-servlet', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-http', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-jackson', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-quartz', version: '3.5.0'
    compile group: 'org.apache.camel', name: 'camel-timer', version: '3.5.0'

    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'

    // Dev Libs
    compileOnly("org.projectlombok:lombok:1.18.12")
    annotationProcessor("org.projectlombok:lombok:1.18.12")

    compile group: 'org.apache.commons', name: 'commons-csv', version: '1.4'
}

application {
    mainClassName = 'com.eip.App'
}

configurations {
    // configuration that holds jars to include in the jar
    extraLibs
}

jar {
    manifest {
        attributes(
                'Main-Class': 'com.beam.agent.App'
        )
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

test {
    useJUnitPlatform()
}

googleJavaFormat {
    exclude '**/App.java'
}

答案1

得分: 1

Shadowing jars can be tricky, because you need to handle duplicate entries. In Apache Camel, there are many META-INF service files, which are getting overwritten with your simple jar approach. Use com.github.johnrengelman.shadow which allows you to customize the merging process.

plugins {
    id 'java'
    id 'application'
    id 'com.github.sherter.google-java-format' version '0.8'
    id "com.github.johnrengelman.shadow" version "6.0.0" // Added plugin
}
repositories {
    jcenter()
}
dependencies {
    // ...
}
application {
    mainClassName = 'com.eip.App'
}
// Removed jar step
test {
    useJUnitPlatform()
}
googleJavaFormat {
    exclude '**/App.java'
}
// Added shadow plugin configuration
shadowJar {
    mergeServiceFiles() // Tell the plugin to merge duplicate service files
    manifest {
        attributes 'Main-Class': 'com.eip.App'
    }
}
apply plugin: 'com.github.johnrengelman.shadow'

The shaded executable jar will have the suffix -all.jar.

java -jar build/libs/app-all.jar
英文:

Shadowing jars can be tricky, because you need to handle duplicate entries. In Apache Camel there are many META-INF service files, which are getting overwritten with your simple jar approach. Use com.github.johnrengelman.shadow which is allowing you to customize the merging process.

plugins {
id 'java'
id 'application'
id 'com.github.sherter.google-java-format' version '0.8'
id "com.github.johnrengelman.shadow" version "6.0.0" // Added plugin
}
repositories {
jcenter()
}
dependencies {
// ...
}
application {
mainClassName = 'com.eip.App'
}
// Removed jar step
test {
useJUnitPlatform()
}
googleJavaFormat {
exclude '**/App.java'
}
// Added shadow plugin configuration
shadowJar {
mergeServiceFiles() // Tell plugin to merge duplicate service files
manifest {
attributes 'Main-Class': 'com.eip.App'
}
}
apply plugin: 'com.github.johnrengelman.shadow'

Shaded executable jar will have suffix -all.jar

java -jar build/libs/app-all.jar

huangapple
  • 本文由 发表于 2020年9月23日 01:54:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64015236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定