Skip to content
Ralph Schaer edited this page Apr 14, 2020 · 14 revisions

Server

ExtDirectSpring supports polling services with (POST) and without (GET) parameters. To configure a polling method it must be annotated with @ExtDirectMethod(value = ExtDirectMethodType.POLL, event = "eventName"). The method must be a member of a Spring managed bean. The event attribute is the name of the event and is optional. If omitted the name of the method will be used as the event name. If a polling method needs to support parameters they have to be annotated with @RequestParam. The value attribute must match the name of the request parameter and is optional if the code is compiled with debug informations. If there is no value attribute Spring tries to read the name of the parameter from the debug informations.

    @Service
    public class Poll {
    
      @ExtDirectMethod(value = ExtDirectMethodType.POLL, event = "message1")
      public String handleMessage1() {
        ...
      }
    
      @ExtDirectMethod(value = ExtDirectMethodType.POLL, event = "message2")
      public int handleMessage2(@RequestParam int id) {
        ...
      }
    
      @ExtDirectMethod(value = ExtDirectMethodType.POLL, event = "message3")
      public int handleMessage3(@RequestParam(value = "id", defaultValue = "1") int id) {
        ...
      }
    
      @ExtDirectMethod(value = ExtDirectMethodType.POLL, event = "message4")
      public int handleMessage4(@RequestParam(required = false) Integer id) {
        ...
      }

The annotation @RequestParam works the same way as in Spring MVC. The parameter id from handleMessage2 is mandatory and an exception will be thrown if it's missing in the request. In handleMessage3 the parameter is optional and will be set to 1 if it's missing. In handleMessage4 the parameter is optional too, but will be set to null if it's missing.

The signature of a polling method can also contain server object parameters (see also Simple method).

    @ExtDirectMethod(value = ExtDirectMethodType.POLL)
    public String message5(HttpServletResponse response, HttpServletRequest request,
                           HttpSession session, Locale locale, 
                           @RequestParam(value = "id") int id) {
        ...
    }

The parameters may appear in any order and the name of the parameter is irrelevant.

Client

In JavaScript you have the option of configuring a polling provider either directly via new or with a config object.

      var pollProvider = new Ext.direct.PollingProvider({
        type:'polling',
        url: Ext.app.POLLING_URLS.message1
      });
      
      //Ext JS 3
      Ext.Direct.addProvider(pollMessage1);
      
      //Ext JS 4.x and Sencha Touch 2
      Ext.direct.Manager.addProvider(pollMessage1);

      //Ext JS 3.x 
      Ext.Direct.addProvider({
        type: 'polling',
        url: Ext.app.POLLING_URLS.message2,
        baseParams: {
          id: 1
        }
      });
      
      //Ext JS 4.x and Sencha Touch 2
      Ext.direct.Manager.addProvider({
        type: 'polling',
        url: Ext.app.POLLING_URLS.message2,
        baseParams: {
          id: 1
        }
      });

baseParams specifies an object containing properties which are sent as parameters with every polling request.

      //Ext JS 3.x 
      Ext.Direct.addProvider({
        id: 'myPollingProvider',
        type: 'polling',
        url: Ext.app.POLLING_URLS.message3,
        interval : 9000
      });
      
      //Ext JS 4.x and Sencha Touch 2
      Ext.direct.Manager.addProvider({
        id: 'myPollingProvider',
        type: 'polling',
        url: Ext.app.POLLING_URLS.message3,
        interval : 9000
      });

interval specifies how often the server method will be called. The configuration above will call handleMessage3 every 9 seconds (9000 milliseconds). If the interval property is omitted it defaults to 3000 (3 seconds). Polling starts immediately after the call to addProvider(). It's possible to stop and start the polling with a call to pollProvider.disconnect() respectively pollProvider.connect(). If there is an id assigned to the polling provider a call to Ext.Direct.getProvider('myPollingProvider') / Ext.direct.Manager.getProvider('myPollingProvider') returns the PollingProvider instance.

Ext JS 3

Every time JavaScript polls the server method it creates an event containing the result. With Ext.Direct.on a listener can be registered on these events and do something with the result.

    Ext.Direct.on({
      message1: function(e) {
        if (true === e.status) {
            //Successful
            Ext.MessageBox.alert("Result", e.data);
          } else {
            //Failure
          }
        },
        message2: function(e) {
          ...
        },
        message3: function(e) {
          ...
        },
    });

Ext JS 4.x and Sencha Touch 2

With these two libraries add the listener directly to the PollingProvers data event. Either adding the listener direct in the config object or at a later time with addListener.

    Ext.direct.Manager.addProvider({
      type: 'polling',
      url: Ext.app.POLLING_URLS.message3,
      listeners: {
        data: function(provider, e) {
          //first parameter is a reference to PollingProvider that sent the event
          //e event object
        }
      }
    });
    
    //OR
    
    var pollProvider = new Ext.direct.PollingProvider({
      type:'polling',
      url: Ext.app.POLLING_URLS.message1
    });
    
    pollProvider.addListener('data', callback, scope);

The event e contains these properties

  • data - return value from the server method
  • name - name of the event
  • status - true if call was successful or false something went wrong
  • type - 'event' or 'exception' if there was an error