6 Replies Latest reply: Apr 19, 2012 3:33 PM by Ronaldo Campos RSS

Fire a event inside a determined scope

Ronaldo Campos Newbie

Is there some way I can fire an event, but only observers that are in the same scope catch it?

 

Exemple:

 

I am in a conversation cid=1, and at some point I fire an event:

 

createEvent.fire(new DetailChangeEvent("Some description"))

 

Then I have a observer:

 

public void changeNotifier(@Observes DetailChangeEvent event){
       log.info("Detail changed!");
}

 

But I want that only the components that are in the same conversation (cid=1) catch this event and notify it! Is is possible using some Weld or Solder functionality?

 

Thanks in advance.

  • 1. Re: Fire a event inside a determined scope
    Marko Lukša Apprentice

    That is exactly how events work - when you fire an event in a session, only the observers in the same session will be notified (observers in other sessions will not be notified).

  • 2. Re: Fire a event inside a determined scope
    Ronaldo Campos Newbie

    Editing my awnser:

     

     

    What realy happens is:

     

    When I fire a event in a @ConversationScoped component, other components @ConversationScoped that are in other conversations don't cath this event. Until now great!

    But i have some @ApplicationScoped and @ViewScoped that also catch it.

     

    Can I isolate there components from my conversation or do something to my event not be captured in a scope diferent then @ConversationScoped?

  • 3. Re: Fire a event inside a determined scope
    Rafael Pestano Newbie

    As a workaround you could get the bean reference and test the beanScope in changeNotifier method,

     

    something like:

     

    public void changeNotifier(@Observes DetailChangeEvent event){
           Bean bean = beanManager.getBeans("managedBeanName").iterator().next();
           if(bean.getScope().equals(ConversationScoped.class)){//dont know if its the right way to test the bean scope
                  log.info("Detail changed!");
              }
       
    }
    
    

     

    I didnt test but maybe it helps.

  • 4. Re: Fire a event inside a determined scope
    Ronaldo Campos Newbie

    Rafael,

     

    I've done some workaround and it´s working for me. But I would like to know if there is something I can do using CDI/Seam/Solder to avoid this workaround.

     

    Thanks anyway!!!

  • 5. Re: Fire a event inside a determined scope
    Marko Lukša Apprentice

    OK, I was worried when you said all other conversations also receive the event.

     

    Anyway, CDI doesn't have anything that would deliver events only to observers in the same scope type. But you can use different event types or different event qualifiers to achieve what you want.

     

    Something like:

     

    @Inject @ConversationOnly Event<Foo> fooEvent;

     

    and

     

    void handleEvent(@Observes @ConversationOnly Foo) {

    ...

    }

     

    where @ConversationOnly is:

     

    @Qualifier

    @Target(PARAMETER)

    @Retention(RUNTIME)

    public @interface ConversationOnly {

        String value();

    }

     

    Note: @ConversationOnly has nothing to do with conversations or scopes. It's just a qualifier for filtering events. If you had a handleEvent(@Observes @ConversationOnly Foo) inside a @SessionScoped bean, it would be notified also, of course.

  • 6. Re: Fire a event inside a determined scope
    Ronaldo Campos Newbie

    Marko,

     

    My workaround is something like this! Using qualifiers.

     

    Thanks!