开发者

$1.class and $2.class not Serializable exception in jgroups

开发者 https://www.devze.com 2023-02-28 08:38 出处:网络
I am creating a demo stock exchange distributed program in Java us开发者_JAVA技巧ing jgroups as middleware. My Stock class has a priority queue which has a comparator, and results in Stock$1.class and

I am creating a demo stock exchange distributed program in Java us开发者_JAVA技巧ing 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;
                }
            }
        );
}


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;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消