Question

I have a gwt application. I have a requirement to show frequently server date to client side, For this, I used GWTEventService. While user logged into application , I am sending one rpc and create one timer which will send periodically event to client.

But due to memory leak, after debugging using JProfiler I came to know that date object created inside timer and due to thread creation it is not being garbage collected.

Also I have attached one image which increase thread in debug mode.that might not at war I am not tested with war yet.

1)How can we make object eligible for garbage collection which are created inside thread?

2)what is other way to send server time to client?

3)How can we test GWT War using JProfiler?

Handler Code:

  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

Was it helpful?

Solution

Even with use of GWTEventService the round trip cost is same. You are not saving much bandwidth or processing in push vs pull. I would prefer one of the following approaches

  1. Timer based server polling using RPC. Set the timer for 5 to 10 minutes delay ( one rpc call with that delay does not cost much)
  2. Subir's suggestion of making one RPC call for date on load and using that for the current session on browser.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top