2 Replies Latest reply on Mar 30, 2010 1:19 AM by Arbi Sookazian

    destroying a @Startup application-scoped component

    Arbi Sookazian Master

      I have the following Seam component (code is not complete) and I'm wondering if it's bad to keep the component around/alive b/n restarts of the app server as it takes up server heap space and resources, etc?  This code needs to run once and then the component can be destroyed as the methods will never get called again by any other client/component until the app deploys again (or server restarts and deploys app, etc.)

      Should I add a @Destroy method and have the component kill itself after purgeTempFiles() completes?  Or have another component kill it or have the Seam container kill it or have it time out or what??

      from ref doc: The @Destroy method is called when the context that the Seam component is bound to ends.
      Components may define only one @Destroy method.

      public class PurgeTempFiles {
           public void purgeTempFiles() throws IOException {
                //Java I/O routine to delete files from the appropriate Linux directories.
           // Deletes all files and subdirectories under dir.
           // Returns true if all deletions were successful.
           // If a deletion fails, the method stops attempting to delete and returns false.
           private boolean deleteDir(File dir) {
               if (dir.isDirectory()) {
                   String[] children = dir.list();
                   for (int i=0; i<children.length; i++) {
                       boolean success = deleteDir(new File(dir, children[i]));
                       if (!success) {
                           return false;
               // The directory is now empty so delete it
               return dir.delete();

        • 1. Re: destroying a @Startup application-scoped component
          Tim Evers Master

          The size of a single Seam bean on the heap is going to be extremely small. Removing it from the App will probably not make any measurable increase in performance. (Other then maybe a few mili seconds at startup)

          So, unless you have completely finished your app and are trying to squeeze every little bit out of it then I honestly wouldn't worry about it. And even if you are in performance mode worrying about a single object on the heap is not going to help.

          However, if you are absolutely set on getting rid of it from the heap then let's see.

          So far in most of the apps I've written (which isn't many but, I'm working on that :P) there's always been a need for at least 1 Application scoped bean. So, just get that Application scoped bean to call this process rather then making it a seperate application scoped bean.

          Or, what about a context listener and just do your purgeTempFiles inside the contextInitialized event? Is there any reason why it has to be a Seam bean?

          • 2. Re: destroying a @Startup application-scoped component
            Arbi Sookazian Master

            The solution for this problem has changed and the new code will be placed inside an existing MBean (the files need to be flushed every time a method is invoked from jmx-console, rather than once per deployment).  But thx for the reply.