在目标文件夹中运行jar与在项目文件夹中运行jar

时间:2020-09-10 09:04:47

标签: java apache-camel spring-camel

我有个奇怪的问题。当我从项目文件夹运行jar时:

java -jar ./target/project.jar

一切正常,可以正确读取路径。

1107 [main] INFO org.apache.camel.core.xml.AbstractCamelContextFactoryBean - JMXAgent enabled: CamelJMXAgent[usePlatformMBeanServer=true, createConnector=true, registryPort=10098, serviceUrlPath=/, statisticsLevel=All, onlyRegisterProcessorWithCustomId=false, registerAlways=true, registerNewRoutes=true, mask=false]
2657 [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded 179 type converters
2853 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext: data-feed-camel) is starting
2853 [main] INFO org.apache.camel.management.ManagedManagementStrategy - JMX is enabled
3078 [Camel Thread #1 - Camel Thread #0 - JMXConnector: service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/] INFO org.apache.camel.management.DefaultManagementAgent - JMX Connector thread started and listening at: service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/f

3089 [main] INFO org.apache.camel.spring.SpringCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
3107 [main] INFO org.apache.camel.spring.SpringCamelContext - Total 0 routes, of which 0 is started.
3128 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext: data-feed-camel) started in 0.255 seconds


org.apache.camel.component.file.FileEndpoint - Endpoint is configured with noop=true so forcing endpoint to be idempotent as well
    59471 [RMI TCP Connection(5)-10.88.55.167] INFO org.apache.camel.spring.SpringCamelContext - Route: flt_data_for_0543 started and consuming from: Endpoint[file://src/main/resources/view/flight/following/default/3648/flt_data?idempotentRepository=%23repo-flt_data_for_0543&noop=true&readLock=none]
    60552 [Camel (data-feed-camel) thread #4 -

但是当我在目标文件夹中运行jar时

1149 [main] INFO org.apache.camel.core.xml.AbstractCamelContextFactoryBean - JMXAgent enabled: CamelJMXAgent[usePlatformMBeanServer=true, createConnector=true, registryPort=10098, serviceUrlPath=/, statisticsLevel=All, onlyRegisterProcessorWithCustomId=false, registerAlways=true, registerNewRoutes=true, mask=false]
2688 [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded 179 type converters
2796 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext: data-feed-camel) is starting
2796 [main] INFO org.apache.camel.management.ManagedManagementStrategy - JMX is enabled
2975 [Camel Thread #1 - Camel Thread #0 - JMXConnector: service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/] INFO org.apache.camel.management.DefaultManagementAgent - JMX Connector thread started and listening at: service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/
2981 [main] INFO org.apache.camel.spring.SpringCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2989 [main] INFO org.apache.camel.spring.SpringCamelContext - Total 0 routes, of which 0 is started.
2991 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext: data-feed-camel) started in 0.194 seconds
21055 [RMI TCP Connection(4)-10.88.55.167] INFO org.apache.camel.component.file.FileEndpoint - Endpoint is configured with noop=true so forcing endpoint to be idempotent as well
21588 [RMI TCP Connection(4)-10.88.55.167] INFO org.apache.camel.spring.SpringCamelContext - Route: flt_data_for_0432 started and consuming from: Endpoint[file://src/main/resources/view/flight/following/default/3648/flt_data?idempotentRepository=%23repo-flt_data_for_0432&noop=true&readLock=none]

它卡住了,什么也没做

路径一直都是相同的:

private static final String DEFAULT_DATA_PATH = "view/flight/following/default/3648/";

有什么问题吗?如何避免这种情况?

1 个答案:

答案 0 :(得分:0)

您的骆驼路线从端点file://src/main/resources/view/flight/following/default/3648/flt_data消耗,该端点以相对路径给出(我猜在您的项目中)。因此,无论您是否从target目录执行jar,都会有所不同。这是因为您是直接从文件系统而不是从类路径读取文件。

为避免这种情况,至少有2种方法:

  1. 您将文件视为应用程序提供的数据:然后将该文件的路径作为应用程序的参数传递,并将其传递给您的路由,当前使用的文件名常量可能变得没有用的。

  2. 您将文件视为资源(即某种不可变数据):然后可以为资源创建InputStream并从中读取字节。对于您的情况,您将编写如下内容:

     InputStream resourceStream = getClass().getResourceAsStream(DEFAULT_FLT_DATA_PATH);
     // Read bytes from resourceStream
    

有关getResourceAsStream的文档,请参见here

如果您必须使用骆驼路线,则this answer建议您可以使用骆驼的Stream component(它也建议您使用InputStream使用上述方法,但细节较少)。但是,它没有提及如何,我也不知道。我建议对这个答案进行澄清。