在目标文件夹内运行JAR与在项目文件夹内运行JAR的区别。

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

Running jar inside target folder vs running jar inside project folder

问题

以下是您要翻译的内容:

我遇到了一个奇怪的问题。当我从项目文件夹中运行jar文件时,使用以下命令:

java -jar ./target/project.jar

一切正常,路径被正确读取。

1107 [main] INFO org.apache.camel.core.xml.AbstractCamelContextFactoryBean - JMX代理已启用:CamelJMX代理[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 - 加载了179个类型转换器
2853 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext:data-feed-camel) 正在启动
2853 [main] INFO org.apache.camel.management.ManagedManagementStrategy - 已启用JMX
3078 [Camel Thread #1 - Camel Thread #0 - JMXConnector:service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/] INFO org.apache.camel.management.DefaultManagementAgent - JMX连接器线程已启动,并正在侦听:service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/f

3089 [main] INFO org.apache.camel.spring.SpringCamelContext - 未使用StreamCaching。如果使用流,则建议启用流缓存。有关更多详细信息,请参见http://camel.apache.org/stream-caching.html
3107 [main] INFO org.apache.camel.spring.SpringCamelContext - 共有0条路由,其中0条已启动。
3128 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext:data-feed-camel) 在0.255秒内启动

org.apache.camel.component.file.FileEndpoint - 由于配置了noop=true,因此强制使端点成为幂等的
    59471 [RMI TCP Connection(5)-10.88.55.167] INFO org.apache.camel.spring.SpringCamelContext - 路由:flt_data_for_0543已启动,并正在消费自: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) 线程#4 -

但是,当我在target文件夹内运行jar文件时,

1149 [main] INFO org.apache.camel.core.xml.AbstractCamelContextFactoryBean - JMX代理已启用:CamelJMX代理[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 - 加载了179个类型转换器
2796 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext:data-feed-camel) 正在启动
2796 [main] INFO org.apache.camel.management.ManagedManagementStrategy - 已启用JMX
2975 [Camel Thread #1 - Camel Thread #0 - JMXConnector:service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/] INFO org.apache.camel.management.DefaultManagementAgent - JMX连接器线程已启动,并正在侦听:service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/
2981 [main] INFO org.apache.camel.spring.SpringCamelContext - 未使用StreamCaching。如果使用流,则建议启用流缓存。有关更多详细信息,请参见http://camel.apache.org/stream-caching.html
2989 [main] INFO org.apache.camel.spring.SpringCamelContext - 共有0条路由,其中0条已启动。
2991 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext:data-feed-camel) 在0.194秒内启动
21055 [RMI TCP Connection(4)-10.88.55.167] INFO org.apache.camel.component.file.FileEndpoint - 由于配置了noop=true,因此强制使端点成为幂等的
21588 [RMI TCP Connection(4)-10.88.55.167] INFO org.apache.camel.spring.SpringCamelContext - 路由:flt_data_for_0432已启动,并正在消费自: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/";

可能出了什么问题?如何避免这种情况?

英文:

I have weird problem. When I'm running jar from project folder which goes:

java -jar ./target/project.jar

everything works fine, path is read correctly.

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 -

but when I run jar inside target folder

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]

it stucks and nothing is doing further

The path is all the time the same:

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

What can be wrong? How can I avoid such a situation?

答案1

得分: 0

你的Camel路由从file://src/main/resources/view/flight/following/default/3648/flt_data端点消费数据,这被给定为一个相对路径(我猜是在你的项目中)。因此,当你从target目录执行你的jar文件时,会产生差异。这是因为你直接从文件系统而不是从类路径中读取文件。

为了避免这个问题,至少有两种方法:

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

  2. 你将文件视为资源(即一种不可变数据):那么你可以为资源创建一个InputStream并从中读取字节。在你的情况下,你可以编写类似这样的代码:

     InputStream resourceStream = getClass().getResourceAsStream(DEFAULT_FLT_DATA_PATH);
     // 从resourceStream读取字节
    

查看这里获取有关getResourceAsStream的文档。

如果你必须使用Camel路由,这个回答建议你可以使用Camel的Stream组件(它也提到了上述使用InputStream的方法,但描述较少)。然而,它没有提到具体的操作方式,我也不清楚。我建议在那个回答上寻求澄清。

英文:

You Camel route consumes from endpoint file://src/main/resources/view/flight/following/default/3648/flt_data which is given as a relative path (in your project I guess). Therefore, it makes a difference when you execute your jar from target directory or not. This is because you are reading the file directly from the file system and not from your classpath.

In order to avoid this, there are (at least) 2 approaches:

  1. You consider the file as provided data consumed by your application: then pass the path to the file as an argument of your application and pass it to your route, the constants for file names you are currently using then become probably useless.

  2. You consider the file as a resource (i.e. sort of immutable data): then you can create an InputStream for your resource and read the bytes from it. In your case, you would write something like this:

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

See here for the documentation of getResourceAsStream.

If you have to use a Camel route, this answer suggests that you can use Camel's Stream component (it also suggests the above approach using an InputStream, but in lesser detail). However, it does not mention how and I don't know either. I would advice to ask for clarification on that answer.

huangapple
  • 本文由 发表于 2020年9月10日 17:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63826354.html
匿名

发表评论

匿名网友

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

确定