外汇平台 网站开发,wordpress分类排序,云南网站建设价格,网站后台多个管理员lambda 序列化因此#xff0c;我一直在思考Tyrus项目所需的增强功能#xff0c;该功能允许用户广播到跨机器集群连接到URL的客户端子集。 有多种方法可以做到这一点。 但是自从我使用JDK 8以来#xff0c;这个问题肯定看起来像钉子。 为此#xff0c;我创建了一个简单的单… lambda 序列化 因此我一直在思考Tyrus项目所需的增强功能该功能允许用户广播到跨机器集群连接到URL的客户端子集。 有多种方法可以做到这一点。 但是自从我使用JDK 8以来这个问题肯定看起来像钉子。 为此我创建了一个简单的单元测试类该类将使用过滤器将其序列化到磁盘读回然后执行。 它有一个实例字段“ VALUE”我们可以使用它直接或间接引用以找出导致序列化失败的原因。 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;import java.util.function.Predicate;import org.junit.Test;public class SerializablePredicateFilterTest {public String VALUE Bob;public interface SerializablePredicateT extends PredicateT, Serializable {}public T void filter(SerializablePredicateT sp, T value) throws IOException, ClassNotFoundException {sp.getClass().isLocalClass();File tempFile File.createTempFile(labmda, set);try (ObjectOutput oo new ObjectOutputStream(new FileOutputStream(tempFile))) {oo.writeObject(sp);}try (ObjectInput oi new ObjectInputStream(new FileInputStream(tempFile))) {SerializablePredicateT p (SerializablePredicateT) oi.readObject();System.out.println(p.test(value));}}} 因此为了进行校准请确保匿名内部类将失败因为它始终包含对封闭对象的引用……。 Test(expected NotSerializableException.class)public void testAnonymousDirect() throws IOException, ClassNotFoundException {String value VALUE;filter(new SerializablePredicateString() {Overridepublic boolean test(String t) {return value.length() t.length();}}, Bob);} 本地类也是如此您不使用本地类吗 Test(expected NotSerializableException.class)public void testLocalClass() throws IOException, ClassNotFoundException {class LocalPredicate implements SerializablePredicateString {Overridepublic boolean test(String t) {// TODO Implement this methodreturn false;}}filter(new LocalPredicate(), Bobby);} 因此当然可以使用独立的类在这种情况下为方便起见可以使用嵌套类。 public static class LengthPredicate implements SerializablePredicateString {private String value;public LengthPredicate(String value) {super();this.value value;}public void setValue(String value) {this.value value;}public String getValue() {return value;}Overridepublic boolean test(String t) {// TODO Implement this methodreturn false;}}Testpublic void testStaticInnerClass() throws IOException, ClassNotFoundException {filter(new LengthPredicate(VALUE), Bobby);} 因此让我们开始使用JDK 8事实证明我的第一次尝试也失败了但是它确实确认序列化对于使用Lambda非常满意。 Test(expected NotSerializableException.class)public void testLambdaDirect() throws IOException, ClassNotFoundException {filter((String s) - VALUE.length() s.length(), Bobby);} 稍作修改即可将值复制到有效的最终属性中现在可以lambda进行序列化并正确检索。 Testpublic void testLambdaInDirect() throws IOException, ClassNotFoundException {String value VALUE;filter((String s) - value.length() s.length(), Bobby);} 当然如果该值是一个简单的方法参数它也可以正常工作。 Testpublic void testLambdaParameter() throws IOException, ClassNotFoundException {invokeWithParameter(VALUE);}private void invokeWithParameter(String value) throws java.lang.ClassNotFoundException, java.io.IOException {filter((String s) - value.length() s.length(), Bobby);} 因此答案是肯定的如果您稍加小心就可以将其序列化。 参考 Lambda会序列化吗 来自我们的JCG合作伙伴 Gerard Davison来自Gerard Davison的博客博客。 翻译自: https://www.javacodegeeks.com/2013/12/lambda-will-it-serialize.htmllambda 序列化