问题描述
奇怪的问题,如果我们创建一个 fat-jar 来执行项目而不是来自 Netbeans,它就会失败.知道为什么吗?
这是一个简单的 apache-camel 应用程序,它使用 Mina 启动一个路由来接收 HL7,然后使用一个直接回答 ACK 消息的 bean,一个简单的示例.
</bean><bean id="answerACKHL7";class =com.mycompany.AnswerEMRBean"/><camelContext xmlns="http://camel.apache.org/schema/spring"><route customId="true";id=路由ID"><from uri="mina:tcp://localhost:8255?sync=true&codec=#hl7codec";/><bean ref="answerACKHL7";方法=sendACK"/></路线></camelContext>
该应用程序有另一个 java 定义的 REST 路由,但这部分在 Netbeans 和执行 jar 文件中都有效.
如果我们从 Netbeans 运行它并将 HL7 消息发送到它工作的端口.好的,我们使用 maven-shade-plugin 3.2.4 构建了一个 fat-jar 并使用 java -jar jarfile.jar 运行它,一切似乎都开始正常然后我们发送相同的 HL7 消息,如果从 netbeans 运行并得到异常,则该消息有效.
错误信息的副本:严重:失败的传递(MessageId:ID-DESKTOP-O7B5GIN-1603385955728-0-1 on ExchangeId:ID-DESKTOP-O7B5GIN-1603385955728-0-1).交付尝试后耗尽:1 次捕获:org.apache.camel.CamelExecutionException:在交易所执行期间发生异常:Exchange[]
消息历史(禁用完整的消息历史)
RouteId ProcessorId Processor Elapsed (ms)[routeID] [routeID] [来自[mina://tcp://localhost:8255?codec=%23hl7codec&sync=true]] [45]...[routeID] [bean1] [bean[ref:answerACKHL7 方法:sendACK]] [0]
堆栈跟踪
org.apache.camel.CamelExecutionException: 期间发生异常在交易所执行:Exchange[]在 org.apache.camel.CamelExecutionException.wrapCamelExecutionException(CamelExecutionException.java:47)在 org.apache.camel.support.builder.ExpressionBuilder$32.evaluate(ExpressionBuilder.java:943)在 org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:36)在 org.apache.camel.component.bean.MethodInfo$ParameterExpression.evaluateParameterBinding(MethodInfo.java:748)在 org.apache.camel.component.bean.MethodInfo$ParameterExpression.evaluateParameterExpressions(MethodInfo.java:640)在org.apache.camel.component.bean.MethodInfo$ParameterExpression.evaluate(MethodInfo.java:608)在 org.apache.camel.component.bean.MethodInfo.createMethodInvocation(MethodInfo.java:195)在 org.apache.camel.component.bean.BeanInfo.createInvocation(BeanInfo.java:265)在 org.apache.camel.component.bean.AbstractBeanProcessor.process(AbstractBeanProcessor.java:135)在 org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:56)在 org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:395)在 org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:148)在 org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:60)在 org.apache.camel.processor.Pipeline.process(Pipeline.java:147)在 org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:286)在 org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)在 org.apache.camel.support.AsyncProcessorSupport.process(AsyncProcessorSupport.java:40)在 org.apache.camel.component.mina.MinaConsumer$ReceiveHandler.messageReceived(MinaConsumer.java:409)在 org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.messageReceived(DefaultIoFilterChain.java:1015)在 org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:650)在 org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:49)在 org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:1128)在 org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:106)在 org.apache.mina.core.session.IoEvent.run(IoEvent.java:89)在 org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTask(OrderedThreadPoolExecutor.java:766)在 org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTasks(OrderedThreadPoolExecutor.java:758)在org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.run(OrderedThreadPoolExecutor.java:697)在 java.lang.Thread.run(Unknown Source) 引起:org.apache.camel.InvalidPayloadException: No body available of type:ca.uhn.hl7v2.model.Message 但具有值:...这里所有消息的副本...类型:java.lang.String on: 消息.原因:没有可用的类型转换器从 type: java.lang.String 转换为所需的类型:ca.uhn.hl7v2.model.Message with value ... 所有 HL7 消息的副本....交换[].引起:[org.apache.camel.NoTypeConversionAvailableException - No类型转换器可用于从类型转换:java.lang.String 到所需类型:ca.uhn.hl7v2.model.Message 带有值...所有 HL7 消息的副本再次... ]在 org.apache.camel.support.MessageSupport.getMandatoryBody(MessageSupport.java:118)在 org.apache.camel.support.builder.ExpressionBuilder$32.evaluate(ExpressionBuilder.java:941)... 更多 26 引起:org.apache.camel.NoTypeConversionAvailableException:无类型转换器可从 type: java.lang.String 转换为所需的类型:ca.uhn.hl7v2.model.Message 带有值...在 org.apache.camel.impl.converter.CoreTypeConverterRegistry.mandatoryConvertTo(CoreTypeConverterRegistry.java:216)在 org.apache.camel.support.MessageSupport.getMandatoryBody(MessageSupport.java:116)... 2020 年 10 月 22 日下午 27:59:15 org.apache.camel.component.mina.MinaConsumer$ReceiveHandlerexceptionCaught ADVERTENCIA:关闭会话作为异常是从 MINA 抛出 2020 年 10 月 22 日下午 6:59:15org.apache.mina.core.filterchain.DefaultIoFilterChaincallNextExceptionCaught ADVERTENCIA:来自异常捕获处理程序.org.apache.camel.CamelException:org.apache.mina.filter.codec.ProtocolEncoderException:org.apache.camel.CamelExecutionException: 期间发生异常在交易所执行:Exchange[]在 org.apache.camel.component.mina.MinaConsumer$ReceiveHandler.exceptionCaught(MinaConsumer.java:387)在 org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.exceptionCaught(DefaultIoFilterChain.java:987)在 org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextExceptionCaught(DefaultIoFilterChain.java:706)在 org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1100(DefaultIoFilterChain.java:49)在 org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.exceptionCaught(DefaultIoFilterChain.java:1110)在 org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:97)在 org.apache.mina.core.session.IoEvent.run(IoEvent.java:89)在 org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTask(OrderedThreadPoolExecutor.java:766)在 org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTasks(OrderedThreadPoolExecutor.java:758)在 org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.run(OrderedThreadPoolExecutor.java:697)在 java.lang.Thread.run(Unknown Source)
...
更多信息
骆驼版本 3.2.0Java 版本 1.8.0.221(在 Netbeans 和命令行中应该相同)
知道为什么它从构建的 jar 中失败了吗???
问候.
通过对 fat jar 的依赖进行着色,您需要指示 maven 插件如何处理重复资源.在你的情况下是 maven-shade-plugin
覆盖 TypeConverterLoader
负责类型转换,因此你得到没有可用于从 type: java.lang.String 转换为所需类型的类型转换器
异常.
需要配置maven-shade-plugin
来合并这个资源.请参阅 如何创建可执行 JAR对于骆驼主项目:
<groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.2.1</version><执行><phase>包</phase><目标><目标>阴影</目标></目标><配置><shadedArtifactAttached>true</shadedArtifactAttached><shadedClassifierName>executable-jar</shadedClassifierName><变形金刚><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>my.package.MainClass</mainClass></变压器><transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"><resource>META-INF/services/org/apache/camel/TypeConverterLoader</resource></变压器></变形金刚></配置></执行></执行></插件>
Strange problem, it fails if we create a fat-jar to execute the project but not from Netbeans. Any clue why?
It's simple apache-camel app, it starts a route using Mina to receive an HL7 and then uses a bean that directly answer the ACK message, a simple sample.
<bean id="hl7codec" class="org.apache.camel.component.hl7.HL7MLLPCodec">
<property name="charset" value="utf-8"/>
</bean>
<bean id="answerACKHL7" class="com.mycompany.AnswerEMRBean" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route customId="true" id="routeID">
<from uri="mina:tcp://localhost:8255?sync=true&codec=#hl7codec" />
<bean ref="answerACKHL7" method="sendACK" />
</route>
</camelContext>
The app has another java defined REST route but this part works equals both in Netbeans and executing jar file.
If we run it from Netbeans and send an HL7 message to the port it works. Ok, we build a fat-jar with maven-shade-plugin 3.2.4 and run it with java -jar jarfile.jar everything seems to start ok then we send the same HL7 message that works if running from netbeans and got an Exception.
Copy of error message:GRAVE: Failed delivery for (MessageId: ID-DESKTOP-O7B5GIN-1603385955728-0-1 on ExchangeId: ID-DESKTOP-O7B5GIN-1603385955728-0-1). Exhausted after delivery attempt: 1 caught: org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[]
Message History (complete message history is disabled)
RouteId ProcessorId Processor Elapsed (ms)
[routeID ] [routeID ] [from[mina://tcp://localhost:8255?codec=%23hl7codec&sync=true] ] [ 45]
...
[routeID ] [bean1 ] [bean[ref:answerACKHL7 method:sendACK] ] [ 0]
Stacktrace
More info
Camel version 3.2.0Java version 1.8.0.221 (should be the same in both Netbeans and command-line)
Any idea why it fails from built jar???
Regards.
With shading of depedencies to fat jar you need to instruct maven plugin, how to handle duplicate resources. In your case is maven-shade-plugin
overriding TypeConverterLoader
which is responsible for type conversion and thus you getNo type converter available to convert from type: java.lang.String to the required type
exception.
You need to configure maven-shade-plugin
to merge this resource. See How to create executable JAR for camel-main project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>executable-jar</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>my.package.MainClass</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/org/apache/camel/TypeConverterLoader</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
这篇关于Mina 在 Netbeans 上工作并在一个胖罐子里失败了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!