Migrate from Spring: release resources on shutdown?

173 views
Skip to first unread message

climbingrose

unread,
May 16, 2008, 11:10:23 PM5/16/08
to google-guice
Hi all,

I'm in the process of migrating our reasonably high traffic site from
Spring to Guice. This reduces the deployment time from 10 seconds to 1
second! I love the Guice's simplicity and the fact that I can do DI in
Java code which is very useful for debugging. Thanks for this great
piece of software, guys!

However, I'm having trouble with the fact that Guice doesn't provide
lifecycle support. With Spring, I can declare BasicDataSource and have
it close() method called upon shutdown to release resources:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="$
{jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
....
</bean>

I wrote a custom provider to create BasicDataSource:

public class BasicDataSourceProvider implements
Provider<BasicDataSource> {
@Inject @Named(value="jdbc.url")
private String jdbcUrl;

@Inject @Named(value="jdbc.username")
private String jdbcUsername;

@Inject @Named(value="jdbc.password")
private String jdbcPassword;

@Inject @Named(value="jdbc.driverClassName")
private String driverClassName;

public BasicDataSource get() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(jdbcUrl);
dataSource.setUsername(jdbcUsername);
dataSource.setPassword(jdbcPassword);
dataSource.setDriverClassName(driverClassName);
.....

return dataSource;
}
}

and bind it as following:

bind(DataSource.class).toProvider(BasicDataSourceProvider.class).asEagerSingleton();

My question is, how do I call close() method on BasicDataSource when
the webapp is shutdown? This question is also applied to other
providers such as Quartz scheduler which we need to call its
shutdown() method.

I've been looking around in this forum but haven't got a solution to
this problem which I think quite typical in webapps.

Has anyone successfully tackled this problem before? Please share your
experience.

Cheers,
Cuong Hoang

Sam Berlin

unread,
May 17, 2008, 12:22:41 AM5/17/08
to google...@googlegroups.com
Some others have recommended a solution of having a Service interface
that exposes something like init(), start() & stop(), and having a
method on all classes that are services:

@Inject register(ServiceRegistry registry) { registry.register(this); }

That'll let every service-class be managed by a registry that can be
inited, started and stopped. So long as your objects are eager
singletons, this'll work wonderfully.

Sam

Dhanji R. Prasanna

unread,
May 17, 2008, 4:47:08 AM5/17/08
to google...@googlegroups.com
Or (simpler), use warp servlet and @Inject your datasource directly into your servlet:


Dhanji.

climbingrose

unread,
May 18, 2008, 9:32:55 PM5/18/08
to google-guice
Thanks guys,

I think I'll go with Service Registry approach.

On May 17, 6:47 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> Or (simpler), use warp servlet and @Inject your datasource directly into
> your servlet:http://www.wideplay.com/warp::servlet
>
> Dhanji.
>
> On Sat, May 17, 2008 at 2:22 PM, Sam Berlin <sber...@gmail.com> wrote:
>
> > Some others have recommended a solution of having a Service interface
> > that exposes something like init(), start() & stop(), and having a
> > method on all classes that are services:
>
> > @Inject register(ServiceRegistry registry) { registry.register(this); }
>
> > That'll let every service-class be managed by a registry that can be
> > inited, started and stopped. So long as your objects are eager
> > singletons, this'll work wonderfully.
>
> > Sam
>
> > On Fri, May 16, 2008 at 11:10 PM, climbingrose <climbingr...@gmail.com>

Piotr Gabryanczyk

unread,
May 21, 2008, 5:11:38 AM5/21/08
to google-guice
The solution with ServiceRegistry seems to me a bit heavyweight.

Guice has modules which help breaking down functionality.
If you need cleanup, just store the reference to the component you
need to do the cleanup for in the module object and call its destroy
method when you destroy the module.

If you have too many of objects to destroy in your module, maybe it is
time to break down the module... :)

Non-singletons is another story... but solution with ServiceRegistry
does not help there anyway.

Piotr Gabryanczyk

On May 17, 5:22 am, "Sam Berlin" <sber...@gmail.com> wrote:
> Some others have recommended a solution of having a Service interface
> that exposes something like init(), start() & stop(), and having a
> method on all classes that are services:
>
>  @Inject register(ServiceRegistry registry) { registry.register(this); }
>
> That'll let every service-class be managed by a registry that can be
> inited, started and stopped.  So long as your objects are eager
> singletons, this'll work wonderfully.
>
> Sam
>
Reply all
Reply to author
Forward
0 new messages