Nomin 和 Spring 引导应用程序:递归映射规则 a = b 导致死循环
Nomin and Spring Boot application: recursive mapping rule a = b causes infinite loop
由于 Nomin、Eclipse 和 Groovy(请参阅 and link2)的组合我已经遇到了一些问题,我再次为此苦苦挣扎。
我的应用程序通过 Gradle 在控制台和 Eclipse 中使用 JUnit 测试。但是现在执行SpringBoot-Main的时候不想工作了class。无论是在 Eclipse 中还是在控制台上使用 gradle bootRun
或 Eclipse Gradle Task => Same Exception
@SpringBootApplication
class CcmApplication {
static void main(String[] args) {
def ctx = SpringApplication.run CcmApplication, args
def scanner = new Scanner(System.in);
print 'Press <Return> to quit program'
scanner.nextLine()
scanner.close()
ctx.close()
}
}
变形金刚Class:
class CoinMarketCapTransformer {
def resource = ClassPathResource.newInstance("coinmarketcap2coin.groovymapper")
println resource.isFile() // Prints true
println resource.isReadable() // Prints true
Nomin nomin = Nomin.newInstance(resource.filename)
CoinOnMarketPlace transform(Coin coin, @Header(name="marketName", required=true) String marketName) {
MarketPlace marketPlace = MarketPlace.newInstance(name: marketName)
CoinOnMarketPlace comp = nomin.map(coin, CoinOnMarketPlace)
comp.setMarketPlace(marketPlace)
comp
}
Coin transform(CoinOnMarketPlace comp) {
nomin.map(comp, Coin.class)
}
}
coinmarketcap2coin.groovymapper
为什么我将它命名为 .groovymapper
请参阅上面的 link。它的位置是src/main/resources
import net.hemisoft.ccm.domain.CoinOnMarketPlace
import net.hemisoft.ccm.porter.Coin
mappingFor a: CoinOnMarketPlace, b: Coin
introspector exploding
automap()
a.coin.coinId = b.coinId
a.coin.name = b.name
a.coin.symbol = b.symbol
a.lastUpdate = b.lastUpdateEpoch
convert to_a: { lastUpdateEpoch -> DateUtils.convertEpochMillis(lastUpdateEpoch) }
convert to_b: { lastUpdate -> DateUtils.convertLocalDateTime(lastUpdate) }
堆栈跟踪:
2018-01-21 17:18:23.288 ERROR 16872 --- [sk-scheduler-10] o.s.integration.handler.LoggingHandler : org.springframework.integration.transformer.MessageTransformationException: Failed to transform Message; nested exception is org.springframework.messaging.MessageHandlingException: nested exception is org.nomin.core.NominException: org.nomin.Mapping: Recursive mapping rule a = b causes infinite loop!, failedMessage=GenericMessage [payload=net.hemisoft.ccm.porter.Coin(bitcoin, Bitcoin, BTC, 1, 11524.4, 1.0, 9.96663E9, 1.93806837423E11, 1.6817087E7, 1.6817087E7, 2.1E7, -3.42, -10.21, -15.21, 1516551267), headers={sequenceNumber=1, Server=cloudflare, sequenceSize=100, Connection=keep-alive, http_statusCode=200, Date=1516551503000, marketName=coinMarketCap, Set-Cookie=__cfduid=dba860f31637c55e679ebf89660fcc7701516551503; expires=Mon, 21-Jan-19 16:18:23 GMT; path=/; domain=.coinmarketcap.com; HttpOnly; Secure, correlationId=dd703a62-f9b4-4838-ddfe-d5f272ce894a, id=c0ce3ae4-f478-c8c4-7901-dbf0061da1e2, Content-Length=54264, contentType=application/json, timestamp=1516551503272}], failedMessage=GenericMessage [payload=net.hemisoft.ccm.porter.Coin(bitcoin, Bitcoin, BTC, 1, 11524.4, 1.0, 9.96663E9, 1.93806837423E11, 1.6817087E7, 1.6817087E7, 2.1E7, -3.42, -10.21, -15.21, 1516551267), headers={sequenceNumber=1, Server=cloudflare, sequenceSize=100, Connection=keep-alive, http_statusCode=200, Date=1516551503000, marketName=coinMarketCap, Set-Cookie=__cfduid=dba860f31637c55e679ebf89660fcc7701516551503; expires=Mon, 21-Jan-19 16:18:23 GMT; path=/; domain=.coinmarketcap.com; HttpOnly; Secure, correlationId=dd703a62-f9b4-4838-ddfe-d5f272ce894a, id=c0ce3ae4-f478-c8c4-7901-dbf0061da1e2, Content-Length=54264, contentType=application/json, timestamp=1516551503272}]
at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:95)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.dispatcher.BroadcastingDispatcher.invokeHandler(BroadcastingDispatcher.java:224)
at org.springframework.integration.dispatcher.BroadcastingDispatcher.dispatch(BroadcastingDispatcher.java:180)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:438)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:388)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:132)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:438)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:388)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.MessageHandlerChain$ReplyForwardingMessageChannel.send(MessageHandlerChain.java:238)
at org.springframework.messaging.MessageChannel.send(MessageChannel.java:45)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.handler.MessageHandlerChain.lambda$configureChain[=14=](MessageHandlerChain.java:124)
at org.springframework.messaging.MessageChannel.send(MessageChannel.java:45)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.splitter.AbstractMessageSplitter.produceOutput(AbstractMessageSplitter.java:231)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:110)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:132)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:438)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:388)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.endpoint.PollingConsumer.handleMessage(PollingConsumer.java:129)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:278)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.lambda$run[=14=](AbstractPollingEndpoint.java:379)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute[=14=](ErrorHandlingTaskExecutor.java:53)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:373)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.messaging.MessageHandlingException: nested exception is org.nomin.core.NominException: org.nomin.Mapping: Recursive mapping rule a = b causes infinite loop!, failedMessage=GenericMessage [payload=net.hemisoft.ccm.porter.Coin(bitcoin, Bitcoin, BTC, 1, 11524.4, 1.0, 9.96663E9, 1.93806837423E11, 1.6817087E7, 1.6817087E7, 2.1E7, -3.42, -10.21, -15.21, 1516551267), headers={sequenceNumber=1, Server=cloudflare, sequenceSize=100, Connection=keep-alive, http_statusCode=200, Date=1516551503000, marketName=coinMarketCap, Set-Cookie=__cfduid=dba860f31637c55e679ebf89660fcc7701516551503; expires=Mon, 21-Jan-19 16:18:23 GMT; path=/; domain=.coinmarketcap.com; HttpOnly; Secure, correlationId=dd703a62-f9b4-4838-ddfe-d5f272ce894a, id=c0ce3ae4-f478-c8c4-7901-dbf0061da1e2, Content-Length=54264, contentType=application/json, timestamp=1516551503272}]
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:107)
at org.springframework.integration.transformer.AbstractMessageProcessingTransformer.transform(AbstractMessageProcessingTransformer.java:90)
at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:89)
... 84 more
Caused by: org.nomin.core.NominException: org.nomin.Mapping: Recursive mapping rule a = b causes infinite loop!
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
at org.nomin.core.MappingEntry.validate(MappingEntry.groovy:64)
at sun.reflect.GeneratedMethodAccessor121.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at org.nomin.core.MappingEntry.parse(MappingEntry.groovy:56)
at sun.reflect.GeneratedMethodAccessor120.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1224)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1033)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:1010)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:993)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:168)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodNSafe(ScriptBytecodeAdapter.java:176)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodNSpreadSafe(ScriptBytecodeAdapter.java:183)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0SpreadSafe(ScriptBytecodeAdapter.java:198)
at org.nomin.Mapping.parse(Mapping.groovy:44)
at org.nomin.core.Nomin.findApplicable(Nomin.java:228)
at org.nomin.core.Nomin.findCachedApplicable(Nomin.java:211)
at org.nomin.core.Nomin.map(Nomin.java:201)
at org.nomin.core.Nomin.map(Nomin.java:159)
at org.nomin.core.Nomin.map(Nomin.java:156)
at org.nomin.NominMapper$map.call(Unknown Source)
at net.hemisoft.ccm.repository.CoinMarketCapTransformer.transform(CoinMarketCapTransformer.groovy:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:181)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:114)
at org.springframework.integration.util.MessagingMethodInvokerHelper$HandlerMethod.invoke(MessagingMethodInvokerHelper.java:1039)
at org.springframework.integration.util.MessagingMethodInvokerHelper.invokeHandlerMethod(MessagingMethodInvokerHelper.java:608)
at org.springframework.integration.util.MessagingMethodInvokerHelper.processInternal(MessagingMethodInvokerHelper.java:504)
at org.springframework.integration.util.MessagingMethodInvokerHelper.process(MessagingMethodInvokerHelper.java:313)
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:104)
... 86 more
更新:
来自 Nomin.class
的以下代码片段在测试 运行 时按预期工作。当通过 bootRun 运行 else if 在第 220 行为假时,什么应该为真。
在第 220 行我们有 pm.sideB.isAssignableFrom(key.source)
true
的结果,因为 pm.sideB
是 Coin
的类型,而 key.source
是 Coin
(见截图)。
您的问题是由spring-boot-devtools
运行时间依赖性引起的。当您 运行 您的应用程序带有调试器时,您会发现 class 是 net.hemisoft.ccm.porter.Coin
和 net.hemisoft.ccm.domain.CoinOnMarketPlace
使用两个不同的 classloader 加载了两次:
sun.misc.Launcher$AppClassLoader
org.springframework.boot.devtools.restart.classloader.RestartClassLoader
这就是为什么以下部分代码的计算结果为 false
:
else if (pm.sideB.isAssignableFrom(key.source) && pm.sideA.isAssignableFrom(key.target)) result.add(new MappingWithDirection(pm, false));
尽管 key.source
和 pm.sideB
似乎是 "same" class,但它们并不相等,因为它们由两个不同的 class装载机。具有相同规范名称的 类 仅在相同的 classloader 中是相等的。这就是为什么您的单元测试如此有效的原因 - 不涉及 spring 引导开发工具,如果您使用调试器 Nomin.java:(line 220)
而 运行 进行单元测试,您会看到 key.source
和 pm.sideB
在一个 classloader 中持有对相同 class 的引用,因此此表达式的计算结果为 true
并且正在使用您定义的映射。否则 nomin 会尝试自动生成映射(可以使用 Nomin.disableAutoMapping()
禁用它),在这种情况下,它使用默认的内省器 - ReflectionIntrospector
。这会导致 Groovy classes 出现问题,正如我们已经在您之前的一个 SO 问题中发现的那样。
解决方案
禁用 spring-boot-devtools
或禁用重新启动或至少尝试将您的域 class 排除在重新启动应用程序之外。您可以检查 Spring Boot Devtools docs 以了解如何操作。我会摆脱它 - 正如您所看到的,它使您的应用程序 运行ning 在您的开发环境中变得不同,因此您永远无法确定它在 运行ning 上线时的行为方式。
由于 Nomin、Eclipse 和 Groovy(请参阅
我的应用程序通过 Gradle 在控制台和 Eclipse 中使用 JUnit 测试。但是现在执行SpringBoot-Main的时候不想工作了class。无论是在 Eclipse 中还是在控制台上使用 gradle bootRun
或 Eclipse Gradle Task => Same Exception
@SpringBootApplication
class CcmApplication {
static void main(String[] args) {
def ctx = SpringApplication.run CcmApplication, args
def scanner = new Scanner(System.in);
print 'Press <Return> to quit program'
scanner.nextLine()
scanner.close()
ctx.close()
}
}
变形金刚Class:
class CoinMarketCapTransformer {
def resource = ClassPathResource.newInstance("coinmarketcap2coin.groovymapper")
println resource.isFile() // Prints true
println resource.isReadable() // Prints true
Nomin nomin = Nomin.newInstance(resource.filename)
CoinOnMarketPlace transform(Coin coin, @Header(name="marketName", required=true) String marketName) {
MarketPlace marketPlace = MarketPlace.newInstance(name: marketName)
CoinOnMarketPlace comp = nomin.map(coin, CoinOnMarketPlace)
comp.setMarketPlace(marketPlace)
comp
}
Coin transform(CoinOnMarketPlace comp) {
nomin.map(comp, Coin.class)
}
}
coinmarketcap2coin.groovymapper
为什么我将它命名为 .groovymapper
请参阅上面的 link。它的位置是src/main/resources
import net.hemisoft.ccm.domain.CoinOnMarketPlace
import net.hemisoft.ccm.porter.Coin
mappingFor a: CoinOnMarketPlace, b: Coin
introspector exploding
automap()
a.coin.coinId = b.coinId
a.coin.name = b.name
a.coin.symbol = b.symbol
a.lastUpdate = b.lastUpdateEpoch
convert to_a: { lastUpdateEpoch -> DateUtils.convertEpochMillis(lastUpdateEpoch) }
convert to_b: { lastUpdate -> DateUtils.convertLocalDateTime(lastUpdate) }
堆栈跟踪:
2018-01-21 17:18:23.288 ERROR 16872 --- [sk-scheduler-10] o.s.integration.handler.LoggingHandler : org.springframework.integration.transformer.MessageTransformationException: Failed to transform Message; nested exception is org.springframework.messaging.MessageHandlingException: nested exception is org.nomin.core.NominException: org.nomin.Mapping: Recursive mapping rule a = b causes infinite loop!, failedMessage=GenericMessage [payload=net.hemisoft.ccm.porter.Coin(bitcoin, Bitcoin, BTC, 1, 11524.4, 1.0, 9.96663E9, 1.93806837423E11, 1.6817087E7, 1.6817087E7, 2.1E7, -3.42, -10.21, -15.21, 1516551267), headers={sequenceNumber=1, Server=cloudflare, sequenceSize=100, Connection=keep-alive, http_statusCode=200, Date=1516551503000, marketName=coinMarketCap, Set-Cookie=__cfduid=dba860f31637c55e679ebf89660fcc7701516551503; expires=Mon, 21-Jan-19 16:18:23 GMT; path=/; domain=.coinmarketcap.com; HttpOnly; Secure, correlationId=dd703a62-f9b4-4838-ddfe-d5f272ce894a, id=c0ce3ae4-f478-c8c4-7901-dbf0061da1e2, Content-Length=54264, contentType=application/json, timestamp=1516551503272}], failedMessage=GenericMessage [payload=net.hemisoft.ccm.porter.Coin(bitcoin, Bitcoin, BTC, 1, 11524.4, 1.0, 9.96663E9, 1.93806837423E11, 1.6817087E7, 1.6817087E7, 2.1E7, -3.42, -10.21, -15.21, 1516551267), headers={sequenceNumber=1, Server=cloudflare, sequenceSize=100, Connection=keep-alive, http_statusCode=200, Date=1516551503000, marketName=coinMarketCap, Set-Cookie=__cfduid=dba860f31637c55e679ebf89660fcc7701516551503; expires=Mon, 21-Jan-19 16:18:23 GMT; path=/; domain=.coinmarketcap.com; HttpOnly; Secure, correlationId=dd703a62-f9b4-4838-ddfe-d5f272ce894a, id=c0ce3ae4-f478-c8c4-7901-dbf0061da1e2, Content-Length=54264, contentType=application/json, timestamp=1516551503272}]
at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:95)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.dispatcher.BroadcastingDispatcher.invokeHandler(BroadcastingDispatcher.java:224)
at org.springframework.integration.dispatcher.BroadcastingDispatcher.dispatch(BroadcastingDispatcher.java:180)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:438)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:388)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:132)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:438)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:388)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.MessageHandlerChain$ReplyForwardingMessageChannel.send(MessageHandlerChain.java:238)
at org.springframework.messaging.MessageChannel.send(MessageChannel.java:45)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.handler.MessageHandlerChain.lambda$configureChain[=14=](MessageHandlerChain.java:124)
at org.springframework.messaging.MessageChannel.send(MessageChannel.java:45)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.splitter.AbstractMessageSplitter.produceOutput(AbstractMessageSplitter.java:231)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:110)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:132)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:438)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:388)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.endpoint.PollingConsumer.handleMessage(PollingConsumer.java:129)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:278)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.lambda$run[=14=](AbstractPollingEndpoint.java:379)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute[=14=](ErrorHandlingTaskExecutor.java:53)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:373)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.messaging.MessageHandlingException: nested exception is org.nomin.core.NominException: org.nomin.Mapping: Recursive mapping rule a = b causes infinite loop!, failedMessage=GenericMessage [payload=net.hemisoft.ccm.porter.Coin(bitcoin, Bitcoin, BTC, 1, 11524.4, 1.0, 9.96663E9, 1.93806837423E11, 1.6817087E7, 1.6817087E7, 2.1E7, -3.42, -10.21, -15.21, 1516551267), headers={sequenceNumber=1, Server=cloudflare, sequenceSize=100, Connection=keep-alive, http_statusCode=200, Date=1516551503000, marketName=coinMarketCap, Set-Cookie=__cfduid=dba860f31637c55e679ebf89660fcc7701516551503; expires=Mon, 21-Jan-19 16:18:23 GMT; path=/; domain=.coinmarketcap.com; HttpOnly; Secure, correlationId=dd703a62-f9b4-4838-ddfe-d5f272ce894a, id=c0ce3ae4-f478-c8c4-7901-dbf0061da1e2, Content-Length=54264, contentType=application/json, timestamp=1516551503272}]
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:107)
at org.springframework.integration.transformer.AbstractMessageProcessingTransformer.transform(AbstractMessageProcessingTransformer.java:90)
at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:89)
... 84 more
Caused by: org.nomin.core.NominException: org.nomin.Mapping: Recursive mapping rule a = b causes infinite loop!
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
at org.nomin.core.MappingEntry.validate(MappingEntry.groovy:64)
at sun.reflect.GeneratedMethodAccessor121.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at org.nomin.core.MappingEntry.parse(MappingEntry.groovy:56)
at sun.reflect.GeneratedMethodAccessor120.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1224)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1033)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:1010)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:993)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:168)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodNSafe(ScriptBytecodeAdapter.java:176)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodNSpreadSafe(ScriptBytecodeAdapter.java:183)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0SpreadSafe(ScriptBytecodeAdapter.java:198)
at org.nomin.Mapping.parse(Mapping.groovy:44)
at org.nomin.core.Nomin.findApplicable(Nomin.java:228)
at org.nomin.core.Nomin.findCachedApplicable(Nomin.java:211)
at org.nomin.core.Nomin.map(Nomin.java:201)
at org.nomin.core.Nomin.map(Nomin.java:159)
at org.nomin.core.Nomin.map(Nomin.java:156)
at org.nomin.NominMapper$map.call(Unknown Source)
at net.hemisoft.ccm.repository.CoinMarketCapTransformer.transform(CoinMarketCapTransformer.groovy:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:181)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:114)
at org.springframework.integration.util.MessagingMethodInvokerHelper$HandlerMethod.invoke(MessagingMethodInvokerHelper.java:1039)
at org.springframework.integration.util.MessagingMethodInvokerHelper.invokeHandlerMethod(MessagingMethodInvokerHelper.java:608)
at org.springframework.integration.util.MessagingMethodInvokerHelper.processInternal(MessagingMethodInvokerHelper.java:504)
at org.springframework.integration.util.MessagingMethodInvokerHelper.process(MessagingMethodInvokerHelper.java:313)
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:104)
... 86 more
更新:
来自 Nomin.class
的以下代码片段在测试 运行 时按预期工作。当通过 bootRun 运行 else if 在第 220 行为假时,什么应该为真。
在第 220 行我们有 pm.sideB.isAssignableFrom(key.source)
true
的结果,因为 pm.sideB
是 Coin
的类型,而 key.source
是 Coin
(见截图)。
您的问题是由spring-boot-devtools
运行时间依赖性引起的。当您 运行 您的应用程序带有调试器时,您会发现 class 是 net.hemisoft.ccm.porter.Coin
和 net.hemisoft.ccm.domain.CoinOnMarketPlace
使用两个不同的 classloader 加载了两次:
sun.misc.Launcher$AppClassLoader
org.springframework.boot.devtools.restart.classloader.RestartClassLoader
这就是为什么以下部分代码的计算结果为 false
:
else if (pm.sideB.isAssignableFrom(key.source) && pm.sideA.isAssignableFrom(key.target)) result.add(new MappingWithDirection(pm, false));
尽管 key.source
和 pm.sideB
似乎是 "same" class,但它们并不相等,因为它们由两个不同的 class装载机。具有相同规范名称的 类 仅在相同的 classloader 中是相等的。这就是为什么您的单元测试如此有效的原因 - 不涉及 spring 引导开发工具,如果您使用调试器 Nomin.java:(line 220)
而 运行 进行单元测试,您会看到 key.source
和 pm.sideB
在一个 classloader 中持有对相同 class 的引用,因此此表达式的计算结果为 true
并且正在使用您定义的映射。否则 nomin 会尝试自动生成映射(可以使用 Nomin.disableAutoMapping()
禁用它),在这种情况下,它使用默认的内省器 - ReflectionIntrospector
。这会导致 Groovy classes 出现问题,正如我们已经在您之前的一个 SO 问题中发现的那样。
解决方案
禁用 spring-boot-devtools
或禁用重新启动或至少尝试将您的域 class 排除在重新启动应用程序之外。您可以检查 Spring Boot Devtools docs 以了解如何操作。我会摆脱它 - 正如您所看到的,它使您的应用程序 运行ning 在您的开发环境中变得不同,因此您永远无法确定它在 运行ning 上线时的行为方式。