jgroup 中 $1.class 和 $2.class 不可序列化异常

发布于 2024-11-02 15:52:45 字数 1559 浏览 4 评论 0原文

我正在使用 jgroups 作为中间件,用 Java 创建一个演示股票交易分布式程序。我的 Stock 类有一个优先级队列,其中有一个比较器,结果是 Stock$1.classStock$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-11-09 15:52:45

您的匿名内部类仅实现Comparator。为了实现Comparator Serialized,您应该将它们转换为静态嵌套类,例如,

public class Stock implements Serializable {

    private static class OrderComparator implements Comparator, Serializable {
        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;
            }
        }
    }

    private PriorityQueue<Order> sellList = new PriorityQueue<Order>(100, new OrderComparator());
}

这不仅会解决您眼前的问题,而且还有争议也使代码更具可读性。

顺便说一句,上面的比较器可以重写得更简洁:

        public int compare(Order oldOrder, Order newOrder) {
            return oldOrder.price - newOrder.price;
        }

Your anonymous inner classes only implement Comparator. In order to implement Comparator and Serializable, you should convert them to static nested classes, e.g.

public class Stock implements Serializable {

    private static class OrderComparator implements Comparator, Serializable {
        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;
            }
        }
    }

    private PriorityQueue<Order> sellList = new PriorityQueue<Order>(100, new OrderComparator());
}

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:

        public int compare(Order oldOrder, Order newOrder) {
            return oldOrder.price - newOrder.price;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文