本文介绍了Mina 在 Netbeans 上工作但在一个胖罐子里失败了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的问题,如果我们创建一个 fat-jar 来执行项目但不是从 Netbeans 执行,它会失败.有什么线索吗?

这是一个简单的 apache-camel 应用程序,它使用 Mina 启动一个路由来接收 HL7,然后使用一个直接回答 ACK 消息的 bean,一个简单的示例.

<属性名称=字符集";值=utf-8"/></豆子><bean id="answerACKHL7";类=com.mycompany.AnswerEMRBean";/><camelContext xmlns="http://camel.apache.org/schema/spring><route customId=true";id=路由ID"><来自 uri="mina:tcp://localhost:8255?sync=true&amp;codec=#hl7codec"/><bean ref="answerACKHL7";方法=发送ACK";/></路线></camelContext>

该应用程序有另一个 java 定义的 REST 路由,但这部分在 Netbeans 和执行 jar 文件中均有效.

如果我们从 Netbeans 运行它并向它工作的端口发送一条 HL7 消息.好的,我们使用 maven-shade-plugin 3.2.4 构建一个 fat-jar 并使用 java -jar jarfile.jar 运行它,一切似乎都开始正常,然后我们发送相同的 HL7 消息,如果从 netbeans 运行并得到一个异常.

错误信息的副本:GRAVE:(MessageId:ID-DESKTOP-O7B5GIN-1603385955728-0-1 上的 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:没有可用的类型的正文: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 - 否类型转换器可用于从 type: 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 日下午 6:59:15 org.apache.camel.component.mina.MinaConsumer$ReceiveHandlerexceptionCaught ADVERTENCIA:作为异常关闭会话是2020 年 10 月 22 日下午 6:59:15 从 MINA 抛出org.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><版本>3.2.1</版本><执行><执行><阶段>包</阶段><目标><目标>阴影</目标></目标><配置><shadedArtifactAttached>true</shadedArtifactAttached><shadedClassifierName>executable-jar</shadedClassifierName><变压器><变压器实现=org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>my.package.MainClass</mainClass></变压器><transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"><资源>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&amp;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 上工作但在一个胖罐子里失败了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:32