Fat Jar中的Apache Camel Timer Nullpointer异常

问题描述

我正尝试在Gradle项目中使用Camel 3.5并以OpenJDK8作为下一个计时器

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

但是,在使用./gradlew build构建胖罐并以java -jar build/libs/app.jar的身份运行

我在控制台收到下一个错误

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)

但是,如果我使用./gradlew run运行,那么可以正常工作,如我所料。 我不想为此项目使用任何框架。我觉得这只是一个配置问题,或者我的配置有问题。

我该如何解决

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',name: 'camel-file-watch',name: 'camel-xstream',name: 'camel-gson',name: 'camel-rest',name: 'camel-servlet',name: 'camel-http',name: 'camel-jackson',name: 'camel-quartz',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'
}

解决方法

阴影罐可能很棘手,因为您需要处理重复的条目。在Apache Camel中,有许多META-INF服务文件,这些文件已通过您的简单jar方法被覆盖。使用com.github.johnrengelman.shadow,它允许您自定义合并过程。

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'

阴影的可执行jar将带有后缀-all.jar

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