I decided to go for option 1: find a way to call PersistService.start() at
module initialization time in a way that does not block PersistFilter when
it calls PersistService.start() later too (= when GuiceFilter is itself
initialized by the servlet container):
I added an Interceptor around PersistService which swallows
IllegalStateExceptions thrown when PersistService.start()/stop() are called
multiple times:
bindInterceptor(subclassesOf(PersistService.class), new
SameMethodMatcher(PersistService.class.getDeclaredMethods()), new
MethodInterceptor() {
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
return invocation.proceed();
} catch (IllegalStateException illegalStateException) {
return null;
}
}
});
That way, I can start the PersistService in one of my module and index the
database at the same timer:
@Inject
protected void startPersistenceAndIndexDatabase(PersistService
persistService, Provider<EntityManagerFactory> factory) throws
InterruptedException {
persistService.start();
EntityManager entityManager = factory.get().createEntityManager();
try {
Search.getFullTextEntityManager(entityManager).createIndexer().startAndWait();
} finally {
entityManager.close();
}
}
Now I don't need to create a filter only to index my database.
On Tuesday, July 2, 2013 1:21:41 PM UTC+2, scl wrote:
>
> First of all, just to make this clear:
> - You cannot inject an EntityManager before PersistService.start() has
> been called. If you do so you will get an exception.
> - You can inject a Provider<EntityManager> before a call to
> PersistService.start()
>
> I do think, that a custom filter is the way to go.
> - Either use a second filter (after PersistFilter) to do the work.
> - Or write you own PersistFilter.
>
> I usually have several filters in a chain where each filter has a specific
> purpose and will initialize a part of the software.
>
>
> On 07/02/2013 09:50 AM, Xavier Dury wrote:
>
> Hi,
>
> I'm using guice-persist, guice-servlet and hibernate-search in my
> application.
>
> For testing purposes, I am using an in-memory database (which gets
> automatically populated with hibernate) and would like to index this
> database for hibernate-search.
>
> My problem is the following: PersistService is not started until
> GuiceFilter is initialized (init() method called), so I can't get a
> (FullText)EntityManager until that moment. That means I cannot trigger the
> database indexing just by requiring injection of EntityManager in ony of my
> guice modules because that will occur before the PersistService has been
> started. And If I try to start the PersistService myself, I get an error
> later saying it has already been started.
>
> As workaround, I've added a guice-managed filter (after PersistFilter)
> that will perform the index processing in its init(FilterConfig) method but
> I don't like to do that very much.
>
> I can see 2 possible solutions to that problem:
>
> - allow PersistService.start() to be called multiple times (as stated
> by the javadoc), this is a recurring problem
> - have a way to call custom code after GuiceFilter initialization.
>
> Does anybody know another/better way?
>
> Thanks,
>
> Xavier
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.