1 Reply Latest reply on Jun 3, 2005 4:57 PM by g king

    Remote Iterator Pattern

    Mario Winterer Newbie


      I've searched through lots of webpages about J2EE design patterns, but I could not find any satisfying answer to my problem concerning "continuous loading". The only design pattern that almost fits is the value-list-handler (also called page-by-page pattern) - but I think it is not the best way to do what I want:

      My client is a rich client (implemented with Eclipse RCP) accessing an EJB application on JBoss Application Server 4.x (using EJB 3.0).
      Very often the clients shows lists (and trees) containing data (merely lists of Tranfer Objects) from the server. To enhance UI-responsiveness on the client, it would be nice, if I could fetch the data from the server in a continous stream and so populate the list asynchronous instead of waiting until all items are deserialized on the client (the list often contains more than 1000 elements).

      So what I need is something like a "Remote Iterator Pattern". My suggestion:
      A stateless session bean implements the query-methods, but does not return the result directly, but a reference to a stateful session bean that is able to iterate over the result.

      // lookup stateless session bean
      QuerySession querySession = (QuerySession)ctx.lookup("querySession");
      // Execute query. The result is a stateful session bean that acts as iterator
      RemoteIterator remoteResult = querySession.findItemsByName("*");
      // Iterate over the remote(!) result:
      while (remoteResult.hasNext()) {
       // fetch another 10 items
       Item[] items = (Item[])remoteResult.fetchNext(10);
       // process items here (e.g. populate UI-list)
      remoteResult.done(); // removes stateful session bean

      I'm not sure if this approach works. How do I implement my QuerySession and RemoteIterator interfaces? Is it possible to have a generic RemoteIterator implementation that is initialized within the finder-method, or do I have to implement one class per finder-method (e.g. an FindItemsByNameRemoteIterator)?

      Thanks in advance,

      P.S.: Please have in mind I'm using EJB 3.0.

        • 1. Re: Remote Iterator Pattern
          g king Newbie

          Hey there,

          The pattern works great and with a little modification you can get it to handle your page display as well.

          I started out using stateful session beans as well, but, found that they were creating some Connection errors. I couldn't figure it out so I ended up changing it back to HttpSession objects.

          Here's some things I found creating the Iterator:

          i. You need a list of lists if you want one object to handle many results

          That way you can switch back and forth between pages retaining your data.

          i. A possible implementation for your stateful bean is to extend a predetermined ListHandler. That way you can have multiple beans using the same code. Each finder method could add an entry into the list object and a generic getCachedValues could retrieve your subsets.

          I now have about 6 httpsession client models which extend my listhandler and couldn't be happier with the results. The pattern pays off in the end.

          Hope it works for you.