-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: Custom Event Listener
PostPosted: Fri Oct 30, 2009 11:33 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

I have been asked to provide the option to enable and disable indexing at runtime. So what I have done is extended the FullTextIndexEventListener and added a property which is checked before delegating to the super class:
Code:
class CustomEventListener extends FullTextIndexEventListener
private boolean isEnabledIndexing = Boolean.TRUE;
   
    @Override
    public void onPostDelete(PostDeleteEvent event) {
        if ( isEnabledIndexing ) {
           super.onPostDelete(event);
        }
    }

    @Override
    public void onPostInsert(PostInsertEvent event) {
        if ( isEnabledIndexing ) {
            super.onPostInsert(event);
        }
    }

    @Override
    public void onPostUpdate(PostUpdateEvent event) {
        if ( isEnabledIndexing ) {
            super.onPostUpdate(event);
        }
    }
    @Override
    public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
        if ( isEnabledIndexing ) {
            super.onPostRecreateCollection( event );
        }
    }

    @Override
    public void onPostRemoveCollection(PostCollectionRemoveEvent event) {
        if ( isEnabledIndexing ) {
            super.onPostRemoveCollection(event);
        }
    }

    @Override
    public void onPostUpdateCollection(PostCollectionUpdateEvent event) {
        if ( isEnabledIndexing ) {
            super.onPostUpdateCollection(event);
        }
    }
   
    @ManagedOperation(description="Operation to enable Hibernate Search Index Event Listeners")
    public void jmxEnableIndexing() {
       this.isEnabledIndexing = Boolean.TRUE;
    }
   
    @ManagedOperation(description="Operation to disable Hibernate Search Index Event Listeners")
    public void jmxDisableIndexing() {
        this.isEnabledIndexing = Boolean.FALSE;
    }
   
    @ManagedAttribute(description="Value that indicates whether Hibernate Search index event listeners are enabled or not")
    public String getJmxHibernateSearchIndexing() {
        return this.isEnabledIndexing ? "Hibernate Search Index Event Listeners Enabled" : "Hibernate Search Index Event Listeners Disabled" ;
    }
    }


This is working ok however I came across this in EventSourceTransactionContext
Code:
private FullTextIndexEventListener getIndexWorkFlushEventListener() {
      if ( this.flushListener != null) {
         //for the "transient" case: might have been nullified.
         return flushListener;
      }
      FlushEventListener[] flushEventListeners = eventSource.getListeners().getFlushEventListeners();
      for (FlushEventListener listener : flushEventListeners) {
         if ( listener.getClass().equals( FullTextIndexEventListener.class )) {
            return (FullTextIndexEventListener) listener;
         }
      }
      log.debug( "No FullTextIndexEventListener was registered" );
      return null;
   }


My custom event listener looks as though it will not work because of listener.getClass().equals( FullTextIndexEventListener.class ). Is there anything I can do to enable/disable indexing at runtime using JMX? I really don't want to update code to check using instanceof or isAssiganbleFrom. Should the line be FullTextIndexEventListener.class.isAssignableFrom(listener.getClass())
?

Any help would be appreciated.


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Fri Oct 30, 2009 1:40 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

please ignore the above. Long day and no sleep. All is ok.

Cheers and apologies!


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Fri Oct 30, 2009 5:27 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Nice it's ok. Anyways, be warned:
Code:
private boolean isEnabledIndexing = Boolean.TRUE;


needs to be at least
Code:
private volatile boolean isEnabledIndexing = Boolean.TRUE;

as eventlisteners have concurrent usage and this flag is probably going to change value after initialization.
(the other state in FullTextIndexEventListener doesn't need it as the class is sealed after initialization)

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Sat Oct 31, 2009 6:38 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

Yep I modified the code after sending that post and made it volatile.


Thanks


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Sun Nov 01, 2009 8:39 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi amin-mc,
another little side-note: we are thinking in making this listener final, so you should not extend it but you could wrap and delegate to it.

What's the general use case for this? I can imagine someone want to disable it globally, but what's the goal in disabling dynamically? Looks like a lot of trouble IMHO.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Sun Nov 01, 2009 3:56 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
The project I am working is going live in December and december is our busiest period and I have been "asked" to provide a mechanism to disable indexing if anything related to search goes wrong (performance degrades or something [i'm not expecting this]. It's a major headache I agree and i have argued against it but the powers that be have "requested" that I put this in place. We will remove this code if all is ok over december. This is only temporary.


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Sun Nov 01, 2009 7:14 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
right, sounds reasonable.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Wed Jan 12, 2011 10:54 am 
Newbie

Joined: Fri Feb 24, 2006 9:15 am
Posts: 17
Hi,

could you share what you did to make it work?

I'd also like to disable Hibernate Search when saving one specific instance. My use case is the following: When I do a full text query and order by relevance, the records return in a certain order. When a user views the details of a record, I need to update the "view count" (not indexed in lucene). When I perform the same query again afterwards, that particular record has disappeared from the list (I guess because the surrounding records have the same relevance index, and when the index get loaded again after a save, lucene has placed the record in another position). So the user suddenly doesn't see that particular record anymore.

What I'd need is to be able to disable the eventlisteners of hibernate search just before persisting, and activate them again just after.
Is there some way to do this?

Cheers Bo


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Wed Jan 12, 2011 11:39 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
Quote:
could you share what you did to make it work?

That was a dirty hack I'd not recommend in stable work, unless you're desperate :) Anyway, amin shares his code in the first post.

Quote:
I'd also like to disable Hibernate Search when saving one specific instance. My use case is the following: When I do a full text query and order by relevance, the records return in a certain order. When a user views the details of a record, I need to update the "view count" (not indexed in lucene). When I perform the same query again afterwards, that particular record has disappeared from the list (I guess because the surrounding records have the same relevance index, and when the index get loaded again after a save, lucene has placed the record in another position). So the user suddenly doesn't see that particular record anymore.

That shouldn't work as you described, something is wrong. Please open a new thread about it if you can't find the issue.
Anyway if that is the reason for you to want disabling indexing, using the latest snapshot of 3.4 should meet your needs as it will skip reindexing of object where you only changes non-indexed properties: HSEARCH-361, so this behaviour would be automatic.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Wed Jan 12, 2011 11:57 am 
Newbie

Joined: Fri Feb 24, 2006 9:15 am
Posts: 17
Great, that's exactly what I need!!

So I don't have to resolve the issue I just described. Just for your information, my conclusion was based on:
  • When I order alfabetically, it doesn't occur
  • When I don't update the record, the record stays in position in the resultset

cheers


Top
 Profile  
 
 Post subject: Re: Custom Event Listener
PostPosted: Wed Jan 12, 2011 12:11 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
I took a quick look at the jboss maven repository, I think snapshots are not published there, right?

we don't push them regularly, but I just uploaded one
https://repository.jboss.org/nexus/cont ... -SNAPSHOT/

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.