我有一个 Java 客户端/服务器应用程序,它通过序列化/反序列化对象进行通信。通常客户端发送 RequestObj,服务器用 ResponseObj 响应。服务器也发送心跳RequestObj,客户端响应心跳ResponseObj。
//Sample RequestObj
public class RequestObj implements Serializable {
private static final long serialVersionUID = xxxx; //auto generated
int type; //TYPE_HEARTBEAT
long time;
...
public void setTime() {
this.time=System.currentTimeMillis();
}
}
从服务器向每个客户端发送心跳请求时,它重用相同的RequestObj(每个连接),设置为当前时间,然后发送给客户端。我期待在正确的时间收到 RequestObj。但是,似乎在客户端反序列化的 RequestObj 总是给我错误的时间。事实上,客户端收到的 RequestObj 与第一个心跳 RequestObj 完全相同(相同的对象 id/地址)。但是不更新的时间呢?
//Sample Server side code sending heartbeat
public class ServerSocketWrapper {
Socket socket;
ObjectOutputStream oos;
...
RequestObj heartbeat = New RequestObj(TYPE_HEARTBEAT);
public void sendHeartBeat() {
heartbeat.setTime();
logger.info("HeartBeat {} {}", heartbeat.time, formatFullTime(heartbeat.time));
oos.writeObject(heartbeat);
oos.flush();
}
}
这是一些示例客户端代码:
//Sample Client side code receiving heartbeat
public class ClientSocketWrapper {
Socket socket;
ObjectInputStream ois;
long heartbeatTime;
...
public void run() {
while(true) {
Object obj = ois.readObject();
if (obj instanceof RequestObj) {
RequestObj req = (RequestObj) obj;
if (req.type == TYPE_HEARTBEAT) {
heartbeatTime = req.time;
logger.info("Received heartbeat {} {} {}", heartbeatTime, formatFullTime(heartbeatTime), req);
sendResponse(new ResponseObj(req.id, TYPE_HEARTBEAT));
}
}
...
}
}
}
这是日志
logs on the server
16:33:56.186 [pool-2-thread-2] INFO ServerSocketWrapper - HeartBeat 1352842436186 2012-11-13 16:33:56.186
16:34:56.185 [pool-2-thread-2] INFO ServerSocketWrapper - HeartBeat 1352842496185 2012-11-13 16:34:56.185
16:35:56.185 [pool-2-thread-1] INFO ServerSocketWrapper - HeartBeat 1352842556185 2012-11-13 16:35:56.185
logs on the client
16:34:08.510 [server:port] INFO ClientSocketWrapper - Received heartbeat 1352842436186 2012-11-13 16:33:56.186 RequestObj@8497904
16:35:06.758 [server:port] INFO ClientSocketWrapper - Received heartbeat 1352842436186 2012-11-13 16:33:56.186 RequestObj@8497904
16:36:10.303 [server:port] INFO ClientSocketWrapper - Received heartbeat 1352842436186 2012-11-13 16:33:56.186 RequestObj@8497904
为什么客户端收到的时间没有更新?JVM 是否在做某种对象缓存?服务器在 jdk 1.7.07 64b 上运行,而客户端在 jdk 1.6.31 64b 上运行。