jgroup 中 $1.class 和 $2.class 不可序列化异常
我正在使用 jgroups 作为中间件,用 Java 创建一个演示股票交易分布式程序。我的 Stock 类有一个优先级队列,其中有一个比较器,结果是 Stock$1.class
和 Stock$2.class
以及 Stock.class
。 Jgroups 只能发送可序列化的数据,但据我了解 $1.class
和 $2.class
来自内部类的结果,这些内部类是由于比较器而推断出来的,并且不可序列化,从而导致异常JGroups,有人可以帮助我了解如何使它们也可序列化或其他一些调整,以免使其看起来像内部类。
import java.io.*;
import java.util.*;
import java.io.Serializable;
public class Stock implements Serializable
{
public String name;
public String symbol;
public int shares = 10000;
public int price = 100;
public PriorityQueue<Order> sellList = new PriorityQueue<Order>(100, new Comparator<Order>()
{
public int compare(Order oldOrder, Order newOrder)
{
int i = oldOrder.price;
int j = newOrder.price;
if(i > j)
return 1;
else if(i < j)
return -1;
else
return 0;
}
}
);
public PriorityQueue<Order> buyList = new PriorityQueue<Order>(100, new Comparator<Order>()
{
public int compare(Order oldOrder, Order newOrder)
{
int i = oldOrder.price;
int j = newOrder.price;
if(i > j)
return -1;
else if(i < j)
return 1;
else
return 0;
}
}
);
}
I am creating a demo stock exchange distributed program in Java using jgroups as middleware. My Stock class has a priority queue which has a comparator, and results in Stock$1.class
and Stock$2.class
along with Stock.class
. Jgroups can send only serializable data but from what I understand $1.class
and $2.class
result from inner classes that are inferred because of comparator's and are not serializable thus causing exceptions with JGroups, can someone help me as to how to make them serializable too or some other tweek to not to make it look like inner classes.
import java.io.*;
import java.util.*;
import java.io.Serializable;
public class Stock implements Serializable
{
public String name;
public String symbol;
public int shares = 10000;
public int price = 100;
public PriorityQueue<Order> sellList = new PriorityQueue<Order>(100, new Comparator<Order>()
{
public int compare(Order oldOrder, Order newOrder)
{
int i = oldOrder.price;
int j = newOrder.price;
if(i > j)
return 1;
else if(i < j)
return -1;
else
return 0;
}
}
);
public PriorityQueue<Order> buyList = new PriorityQueue<Order>(100, new Comparator<Order>()
{
public int compare(Order oldOrder, Order newOrder)
{
int i = oldOrder.price;
int j = newOrder.price;
if(i > j)
return -1;
else if(i < j)
return 1;
else
return 0;
}
}
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的匿名内部类仅实现
Comparator
。为了实现Comparator
和Serialized
,您应该将它们转换为静态嵌套类,例如,这不仅会解决您眼前的问题,而且还有争议也使代码更具可读性。
顺便说一句,上面的比较器可以重写得更简洁:
Your anonymous inner classes only implement
Comparator
. In order to implementComparator
andSerializable
, you should convert them to static nested classes, e.g.Not only will this fix your immediate problem, it arguable also makes the code more readable.
Incidentally, the above comparator can be rewritten much more succinctly: