After trying everything I could think of I had to give up injecting those session and I switched back to a classical try/finally block.
Really too bad Jersey doesn't provide a clean mechanism to clean up injected per request resources, I am not quite ready to switch to another IoC right now and this seems to be the accepted answer.
Something really basic would do - e.g. if (injected instanceof JerseyCloseable( { injected.close() }, InjectableProvider.releaseValue()...
Franck
________________________________
From: Franck Mangin <franck102_at_yahoo.com>
To: "users_at_jersey.java.net" <users_at_jersey.java.net>
Sent: Friday, March 15, 2013 1:54 PM
Subject: [Jersey] CloseableService and InjectableProvider
Hi all,
I would like to use a CloseableService to properly release an injected session object when the request gets out of scope.
It seems like the pattern below should work, however I would rather not depend on the resource classes to add the session to the closeable service, I would like that to be the responsibility of the InjectableProvider itself... However I don't know/understand whether injecting a CloseableService into the provider itself would work, and would have the proper scope?
Suggestions welcome!
Thanks,
Franck Mangin
class MyResource
{
@Context CloseableService _cs;
public MyResource getIt(@Context Session session, ...)
{
_cs.add(session);
...
}
}
class SessionProvider implements InjectableProvider<Session>
{
...
public ComponentScope getScope()
{
return ComponentScope.PerRequest;
}
}