2013-04-17 45 views
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); 
      } 
     } 

    } 

enter image description here

+2

恕我直言,你应该只发送一次。将差异保存在客户端并稍后使用它显示服务器时间。 –

+0

为什么不从客户端使用[Timer](http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/Timer)处理此问题。 HTML)而不是推动信息? –

+0

@RonanQuillevere AFAIK在客户端计时器,我必须每次进入rpc请求,它被称为服务器池。在这里,我使用服务器推送来避免客户端到服务器的请求。如果我没有得到任何其他,那么我会去争取。 – iMBMT

回答

1

即使使用GWTEventService的往返费用是一样的。您不会节省很多带宽或处理推或拉。我更喜欢以下方法之一

  1. 使用RPC进行基于计时器的服务器轮询。将计时器设置为延迟5到10分钟(具有该延迟的一个rpc呼叫不会花费太多)
  2. Subir建议在加载时对日期进行一次RPC调用,并将其用于浏览器上的当前会话。
相关问题