Java:序列化第二次不起作用 [英] Java: Serialization doesn't work for the second time

查看:116
本文介绍了Java:序列化第二次不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台服务器,用于跟踪一些数据。当我使用管理员应用程序连接到服务器以检查数据的当前状态时。我使用5秒的刷新率。服务器第一次发送数据时,它可以工作。但是,第二次,当数据发生变化时,管理员不会收到最新的数据。我通过ObjectOutputStream和ObjectInputStream发送包装在类中的数据:



这是数据的包装类:

  public class Leerling implements Serializable {

public int llnID;

public String naam;
public String voornaam;
public String klas;
public int klasNummer;
public Date geboorteDatum;

public String getFullName()
{
return voornaam ++ naam;

$ b @Override
public String toString(){
returnLeerling {+llnID =+ llnID +,naam =+ naam + ,voornaam =+ voornaam +,klas =+ klas +,klasNummer =+ klasNummer +,geboorteDatum =+ geboorteDatum +'}';
}

}


公共类SLeerling扩展Leerling实现可序列化{

public boolean votinged;
public int vote = -2;
}

我在从流中读取对象之前尝试调用 System.gc(); 以确保对象旧对象不再在内存中。但没有成功。



有人知道确切的问题是什么吗?如何才能获得真实的最新数据?



预先致谢。






问题的第二个例子:

我还有一些其他数据的包装类(它是一个内部类):

  public static class MonitorResponse implements Serializable 
{
public int numberOfLLN;
public int blocked;
public int blancos;
public List< Integer>票;
}

当我第一次发送数据时,它可以工作。但是我第二次发送它(更新它),除了 List< Integer>票数已更新。所以 votes 不会被刷新。
然后我用一个数组替换List来解决它有点棘手:

  public static class MonitorResponse implements Serializable 
{
public int numberOfLLN;
public int blocked;
public int blancos;
public Integer []票;
}

这个工作非常完美。奇怪,如果你问我。代码的其他部分我几乎没有改变...(除了实现数组而不是List)

解决方案

如果你使用一个 ObjectOutputStream code>在服务器上的对象,那么你需要确保你对它调用 reset ,否则它会将共享引用写入先前写入的对象。这听起来像你所看到的。



为了说明问题:

  class BrokenServer {
void sendBrokenVoteData(ObjectOutputStream out){
out.writeObject(votes);
changeVoteData(票);
out.writeObject(票); //将共享引用写入投票而不更新任何数据。
}
}

class FixedServer {
void sendFixedVoteData(ObjectOutputStream out){
out.writeObject(votes);
changeVoteData(票);
out.reset(); //清除所有共享引用。
out.writeObject(票); //用新数据写一个新的投票副本。
}
}


I have a server on which I keep track of some data. When I connect to the server with the administrator application to check out the current state of the data. I use a refresh rate of 5 seconds. The first time the server sends the data, it works. But the second time, when the data changed, the admin-side does not receive the up-to-date data. I'm sending the data, wrapped in a class, through an ObjectOutputStream and ObjectInputStream:

This is the wrapper class for the data:

public class Leerling implements Serializable {

    public int llnID;

    public String naam;
    public String voornaam;
    public String klas;
    public int klasNummer;
    public Date geboorteDatum;

    public String getFullName()
    {
        return voornaam + " " + naam;
    }

    @Override
    public String toString() {
        return "Leerling{" + "llnID=" + llnID + ", naam=" + naam + ", voornaam=" + voornaam + ", klas=" + klas + ", klasNummer=" + klasNummer + ", geboorteDatum=" + geboorteDatum + '}';
    }

}


public class SLeerling extends Leerling implements Serializable{

    public boolean voted;
    public int vote = -2;
}

What I tried is before reading the Object from the stream to call System.gc(); to make sure the object old object is not longer in memory. But without success.

Does someone know what the exact problem is? And how to make it possible to get the real up-to-date data?

Thanks in advance.


A second example of the problem:

I have again a wrapper class for some other data (It is an inner class):

public static class MonitorResponse implements Serializable
{
    public int numberOfLLN;
    public int blocked;
    public int blancos;
    public List<Integer> votes;
}

When I send the data the first time, it works. But the second time I send it (to update it), everything EXCEPT the List<Integer> votes is updated. So votes isn't refreshed. Then I solved it a bit tricky by replacing the List by an array:

public static class MonitorResponse implements Serializable
{
    public int numberOfLLN;
    public int blocked;
    public int blancos;
    public Integer[] votes;
}

And this works perfect. Strange if you ask me. The the other part of the code I changed almost nothing... (except to implement the array instead of the List)

解决方案

It's probably the ObjectOutputStream causing the trouble.

If you use a single ObjectOutputStream object on the server then you need to make sure you call reset on it, otherwise it will write shared references to previously-written objects. This sounds like what you are seeing.

To illustrate the problem:

class BrokenServer {
    void sendBrokenVoteData(ObjectOutputStream out) {
        out.writeObject(votes);
        changeVoteData(votes);
        out.writeObject(votes); // Writes a shared reference to "votes" WITHOUT updating any data.
    }
}

class FixedServer {
    void sendFixedVoteData(ObjectOutputStream out) {
        out.writeObject(votes);
        changeVoteData(votes);
        out.reset(); // Clears all shared references.
        out.writeObject(votes); // Writes a new copy of "votes" with the new data.
    }
}

这篇关于Java:序列化第二次不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆