Java 8 lambda - 运行时错误

标签 java lambda java-8

我们的项目中有以下代码,虽然它工作正常,但在运行时随机出现 class def not found 错误。我们的应用程序服务器在每个星期天重新启动,因此有时我们会在任何随机服务器实例上遇到此错误。 服务器重新启动解决了问题,但关于为什么类加载在两者之间中断的任何线索。

我在这个问题中遇到了一些类似的错误,似乎问题已在 jdk 9 Transforming lambdas in Java 8 中修复

但在我得出结论之前,有人可以解释这是同一种错误,以及为什么它偶尔会发生。

public boolean isAttachmentExpired(final Document_Attachment da) {
    return this.bcDocumentScreen.getValidator().getAttachmentsValidator().isAttachmentExpired(da);
}

public boolean isAttachmentWarningShown() {
    return CollectionUtils.isNotEmpty(getAttachments()) && getAttachments().stream().anyMatch(this::isAttachmentExpired);
}

public boolean isAttachmentExpired(final Document_Attachment da) {
        final Date today = DateHelper.today();
        return DateHelper.diffInYears(today, da.getUploaded()) >= 1;
    }

错误:-

Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.GeneratedMethodAccessor1913.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
        at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
        at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:79)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.persistence.ManagedEntityInterceptor.aroundInvoke(ManagedEntityInterceptor.java:48)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
        at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:196)
        at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:114)
        at com.XXX.BcdAttachmentsSection_$$_javassist_seam_91.isAttachmentWarningShown(BcdAttachmentsSection_$$_javassist_seam_91.java)
        at sun.reflect.GeneratedMethodAccessor1912.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)
        at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
        at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
        at org.jboss.el.parser.AstPropertySuffix.getValue(AstPropertySuffix.java:53)
        at org.jboss.el.parser.AstValue.getValue(AstValue.java:67)
        at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
        at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
        ... 122 more
Caused by: java.lang.NoClassDefFoundError: com/XXX/docsections/BcdAttachmentsSection$$Lambda$75
        at com.XXX.BcdAttachmentsSection$$Lambda$75/1736532374.get$Lambda(Unknown Source)
        at com.XXXX.isAttachmentWarningShown(BcdAttachmentsSection.java:51)
        ... 150 more

最佳答案

很有可能 the bug, you have linked确实适用,如果涉及仪器。考虑这个错误,JDK-8027681, “Lambda serialization fails once reflection proxy generation kicks in” ,这影响了所有执行超过 16 次的反射操作(这是一个可配置的阈值),因为底层实现将通过生成一个访问器类来优化后续调用,该访问器类由 HotSpot 可以内联的字节码组成。此字节码无法访问为 Java 8 早期版本中的 lambda 表达式生成的匿名类。

虽然此错误已得到修复,但所描述的在一定数量的调用后生成类的行为仍然存在,因此如果代理尝试检测这些生成的类,由于仍然存在的检测错误和对调用次数可能是这种情况偶尔发生的原因。

虽然应该修复 Instrumentation/JVM 中的这个错误(并将在下一个版本中修复),但不尝试检测这些类也会有所帮助。通常,没有理由检测这些内部帮助程序类。

关于Java 8 lambda - 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42265044/

相关文章:

java-stream-List of Long to list in one line

lambda - 计划中的关闭如何运作?

django - 如何将 Django 模型字段的默认值设置为函数调用/可调用(例如,相对于模型对象创建时间的日期)

python lambda打印空间

java - 在设置变量之前检查 null - 没有 if-else

java - 有没有办法在谷歌驱动器文件的属性中搜索?

消费者接口(interface)中的 Java 泛型类型参数

java - ByteArrayInputStream/ByteArrayOutputStream的实际使用

java - 使用Recyclerview解析Json

java - 按条件将项目从一个集合插入到另一个集合,并使用 Google Guava 或 java 8 Stream API 迭代这两个集合