Session scoped service destructor?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Session scoped service destructor?

Colin Harrington
Is there a way to implement a destructor or something similar on a session scoped service?

I want to do some cleanup when the user's session expires by using some data in a session scoped service.  How would I go about doing this?

I've found the HttpSessionListener, that I'd be should be able to hook into the sessionDestroyed() event to get the session.  How would I access a session scoped service then?  Is there a better mechanism that I may have missed?

Thank you in advance

Colin Harrington
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Session scoped service destructor?

David Russell-7
Hi Colin,

You might want to consider configuring the init-method and destroy-method for Spring managed beans in your resources.groovy configuration file found here:

grails-app/conf/spring/resources.groovy

For example, for your session scoped service:

beans = {

    sessionScopedService(your.very.own.SessionScopedService) { bean ->
bean.scope = 'session'
authenticateService = ref("authenticateService")
bean.initMethod = 'init'
bean.destroyMethod = 'destroy'
    }
}

Then in your service implementation just declare and implement these event handlers:

    public void init() {
        // Setup code for service...
    }

    public void destroy() {
        // Cleanup code for service...
    }

With this approach you might also want to take note of how you inject other beans into your service, see the use of ref above.

David

On Sat, Jul 3, 2010 at 3:27 AM, Colin Harrington <[hidden email]> wrote:
Is there a way to implement a destructor or something similar on a session scoped service?

I want to do some cleanup when the user's session expires by using some data in a session scoped service.  How would I go about doing this?

I've found the HttpSessionListener, that I'd be should be able to hook into the sessionDestroyed() event to get the session.  How would I access a session scoped service then?  Is there a better mechanism that I may have missed?

Thank you in advance

Colin Harrington



--
"When we remember we are all mad, the mysteries disappear and life stands explained." ~Twain
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Session scoped service destructor?

ld@ldaley.com
Spring supports destruction callbacks via the actual scope implementation.

Check out the docs for the SessionScope class.

Sent from my iPhone

On 03/07/2010, at 10:05 AM, David Russell <[hidden email]> wrote:

Hi Colin,

You might want to consider configuring the init-method and destroy-method for Spring managed beans in your resources.groovy configuration file found here:

grails-app/conf/spring/resources.groovy

For example, for your session scoped service:

beans = {

    sessionScopedService(your.very.own.SessionScopedService) { bean ->
bean.scope = 'session'
authenticateService = ref("authenticateService")
bean.initMethod = 'init'
bean.destroyMethod = 'destroy'
    }
}

Then in your service implementation just declare and implement these event handlers:

    public void init() {
        // Setup code for service...
    }

    public void destroy() {
        // Cleanup code for service...
    }

With this approach you might also want to take note of how you inject other beans into your service, see the use of ref above.

David

On Sat, Jul 3, 2010 at 3:27 AM, Colin Harrington <[hidden email]> wrote:
Is there a way to implement a destructor or something similar on a session scoped service?

I want to do some cleanup when the user's session expires by using some data in a session scoped service.  How would I go about doing this?

I've found the HttpSessionListener, that I'd be should be able to hook into the sessionDestroyed() event to get the session.  How would I access a session scoped service then?  Is there a better mechanism that I may have missed?

Thank you in advance

Colin Harrington



--
"When we remember we are all mad, the mysteries disappear and life stands explained." ~Twain
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Session scoped service destructor?

Ian Roberts
In reply to this post by Colin Harrington
Colin Harrington wrote:
> Is there a way to implement a destructor or something similar on a
> session scoped service
> <http://www.grails.org/doc/latest/guide/single.html#8.2%20Scoped%20Services>?
>
> I want to do some cleanup when the user's session expires by using some
> data in a session scoped service.  How would I go about doing this?

import javax.annotation.PreDestroy

@PreDestroy
public void cleanup() {
  // do your cleanup here
}

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Session scoped service destructor?

Colin Harrington
Ian,

Thank you @PreDestory was exactly what I needed.

Just a note for others who read this later.  There was no Hibernate session bound to the thread, so I had to offload that work on another service. (or I could have opened a new one?)

Colin Harrington
[hidden email]


On Sat, Jul 3, 2010 at 8:44 AM, Ian Roberts <[hidden email]> wrote:
Colin Harrington wrote:
> Is there a way to implement a destructor or something similar on a
> session scoped service
> <http://www.grails.org/doc/latest/guide/single.html#8.2%20Scoped%20Services>?
>
> I want to do some cleanup when the user's session expires by using some
> data in a session scoped service.  How would I go about doing this?

import javax.annotation.PreDestroy

@PreDestroy
public void cleanup() {
 // do your cleanup here
}

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Session scoped service destructor?

burtbeckwith
You can wrap the code in a withTransaction block and it will open a session for you:

   @PreDestory
   void cleanup() {
      SomeDomainClass.withTransaction { status ->
         ...
      }
   }

Burt

> Ian,
>
> Thank you @PreDestory was exactly what I needed.
>
> Just a note for others who read this later.  There was no Hibernate session
> bound to the thread, so I had to offload that work on another service. (or I
> could have opened a new one?)
>
> Colin Harrington
> [hidden email]
>
>
> On Sat, Jul 3, 2010 at 8:44 AM, Ian Roberts <[hidden email]>wrote:
>
> > Colin Harrington wrote:
> > > Is there a way to implement a destructor or something similar on a
> > > session scoped service
> > > <
> > http://www.grails.org/doc/latest/guide/single.html#8.2%20Scoped%20Services
> > >?
> > >
> > > I want to do some cleanup when the user's session expires by using some
> > > data in a session scoped service.  How would I go about doing this?
> >
> > import javax.annotation.PreDestroy
> >
> > @PreDestroy
> > public void cleanup() {
> >  // do your cleanup here
> > }
> >
> > Ian
> >
> > --
> > Ian Roberts               | Department of Computer Science
> > [hidden email]  | University of Sheffield, UK
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >    http://xircles.codehaus.org/manage_email
> >
> >
> >
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Session scoped service destructor?

Colin Harrington
Burt,

SomeDomainClass.withTransaction works great.  I initially tried withSession based on the exception, but didn't try withTransaction.

Thank you!

Colin Harrington

On Tue, Jul 6, 2010 at 10:23 AM, Burt Beckwith <[hidden email]> wrote:
You can wrap the code in a withTransaction block and it will open a session for you:

  @PreDestory
  void cleanup() {
     SomeDomainClass.withTransaction { status ->
        ...
     }
  }

Burt

> Ian,
>
> Thank you @PreDestory was exactly what I needed.
>
> Just a note for others who read this later.  There was no Hibernate session
> bound to the thread, so I had to offload that work on another service. (or I
> could have opened a new one?)
>
> Colin Harrington
> [hidden email]
>
>
> On Sat, Jul 3, 2010 at 8:44 AM, Ian Roberts <[hidden email]>wrote:
>
> > Colin Harrington wrote:
> > > Is there a way to implement a destructor or something similar on a
> > > session scoped service
> > > <
> > http://www.grails.org/doc/latest/guide/single.html#8.2%20Scoped%20Services
> > >?
> > >
> > > I want to do some cleanup when the user's session expires by using some
> > > data in a session scoped service.  How would I go about doing this?
> >
> > import javax.annotation.PreDestroy
> >
> > @PreDestroy
> > public void cleanup() {
> >  // do your cleanup here
> > }
> >
> > Ian
> >
> > --
> > Ian Roberts               | Department of Computer Science
> > [hidden email]  | University of Sheffield, UK
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >    http://xircles.codehaus.org/manage_email
> >
> >
> >
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Loading...