【发布时间】:2013-04-17 11:51:38
【问题描述】:
我有一个 gwt 应用程序。我需要经常向客户端显示服务器日期,为此,我使用了GWTEventService。当用户登录到应用程序时,我正在发送一个 rpc 并创建一个计时器,它将定期向客户端发送事件。
但由于内存泄漏,在使用JProfiler 进行调试后,我知道在计时器内创建了日期对象,并且由于线程创建,它没有被垃圾收集。
我还附上了一张图片,它在调试模式下增加了线程。这可能不是战争我还没有经过战争测试。
1)我们怎样才能使在线程内创建的对象有资格进行垃圾回收?
2) 将服务器时间发送给客户端的其他方法是什么?
3)我们如何使用 JProfiler 测试 GWT War?
处理程序代码:
public class UtilityCallerActionHandler extends RemoteEventServiceServlet implements
ActionHandler<UtilityCaller, UtilityCallerResult> {
private static Timer myEventGeneratorTimer;
@Inject
public UtilityCallerActionHandler() {
}
@Override
public UtilityCallerResult execute(UtilityCaller action,
ExecutionContext context) throws ActionException {
try{
if(myEventGeneratorTimer == null) {
myEventGeneratorTimer = new Timer(true);
myEventGeneratorTimer.schedule(new ServerTimerTask(), 0, 10000);
}
return new UtilityCallerResult();
}catch (Exception e) {
CommonUtil.printStackTrace(e);
long exceptionBeanId = 0l;
if (e instanceof NTException) {
exceptionBeanId = ((NTException)e).getExceptionBeanId();
}
throw new NTActionException(NTException.getStackTrace(e),e.getCause(), exceptionBeanId);
}
}
@Override
public void undo(UtilityCaller action, UtilityCallerResult result,
ExecutionContext context) throws ActionException {
}
@Override
public Class<UtilityCaller> getActionType() {
return UtilityCaller.class;
}
private class ServerTimerTask extends TimerTask
{
public void run() {
final Date theEventMessage = new Date();
//create the event
Event theEvent = new NTTimerEvent(theEventMessage);
//add the event, so clients can receive it
addEvent(NTTimerEvent.SERVER_MESSAGE_DOMAIN, theEvent);
}
}
}
【问题讨论】:
-
恕我直言,您应该只发送一次。保存客户端的差异,稍后使用它来显示服务器时间。
-
为什么不用Timer 从客户端处理这个问题,而不是推送信息?
-
@RonanQuillevere AFAIK 在客户端计时器,我每次都必须向它发出 rpc 请求,这称为服务器池。在这里,我使用服务器推送来避免客户端到服务器的请求。如果我没有得到任何其他,那么我会去。
-
也许你应该看看websockets(我相信comet不是基于websockets的)。我从未尝试过任何基于它们的 GWT 解决方案,但似乎有些人正在研究它。