3
我有一个gwt应用程序。我有一个频繁向服务器端显示服务器日期的要求,为此,我使用了GWTEventService。当用户登录到应用程序时,我正在发送一个rpc并创建一个定时器,它将定期向客户端发送事件。发送服务器时间到客户端的正确方法是什么?
但由于内存泄漏,在使用JProfiler进行调试后,我开始知道在timer内部创建的日期对象,并且由于线程创建,它没有被垃圾回收。
另外我附加了一个图像,它增加了调试模式下的线程。这可能不会引起战争,我还没有用战争来测试。
1)我们如何让对象符合在线程内创建的垃圾收集?
2)发送服务器时间到客户端的其他方式是什么?
3)如何使用JProfiler测试GWT战争?
处理代码:
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](http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/Timer)处理此问题。 HTML)而不是推动信息? –
@RonanQuillevere AFAIK在客户端计时器,我必须每次进入rpc请求,它被称为服务器池。在这里,我使用服务器推送来避免客户端到服务器的请求。如果我没有得到任何其他,那么我会去争取。 – iMBMT