views:

57

answers:

2

Hello,

I've read some blog articles about Observer pattern implementation on JEE6 and something bother me... I can't find any information atm so i ask there...

I've found the following exemples:

@Stateless
[...]
public class ParisJugService {

   @Inject
   Event event;

   public void helloParis(){
        System.out.println("Hello Paris");
        event.fire("hello Paris invoked!");
   }
}

@Stateless
public class EventReceiver {

    public void onHelloParis(@Observes String message){
        System.out.println("----------- " + message);
    }
}

And

public class MyEvent {
    String data;
    Date eventTime;
    ....

}

public class EventProducer {

  @Inject @Any Event<MyEvent> event;


  public void doSomething() {
       MyEvent e=new MyEvent();
      e.data="This is a test event";
      e.eventTime=new Date();
      event.fire(e);

  }

}


public class EventConsumer {


    public void afterMyEvent(@Observes MyEvent event) {

        // .. Insert event logic here
    }

}

I can't understand how the link between event consumer and event producer is done...

Is it by naming convention of the methods? (Where the words "on", "after", "before"... will have sense)

Is it by comparison of given arguments (here String and MyEvent)?

I can't know and don't see what else could it be...

+2  A: 

The container keeps metadata about all beans inside it. On startup it registers all observer methods together with the event type (the Class of the argument) they observe. Whenever an event is fired, the list of observers is checked for methods that accept the appropriate type of event.

Bozho
to late but thanks anyway
Sebastien Lorber
@Sebastien Lorber what do you mean by "to late" ?? My answer was given 5 minutes after you asked the question, and 2 minutes before the accepted answer.
Bozho
Is the signature of onHelloParis in the example in the question actually correct?It takes a String, how can it be connected to the approriate event?
tkr
It's incorrect, I believe. But the 2nd class is correct.
Bozho
perhaps i'm wrong but anyway you already have enough points :p
Sebastien Lorber
+2  A: 

The link between the event firing and the listener is based on the event type.

In your example:

public class EventConsumer {

    public void afterMyEvent(@Observes MyEvent event) {
        // .. Insert event logic here
    }

}

This class will receive all fired events of type MyEvent, wherever they come from.

Source: http://download-llnw.oracle.com/javaee/6/api/javax/enterprise/event/Observes.html

Vivien Barousse
Is the signature of onHelloParis in the example in the question actually correct?
tkr
First answer posted accepted :)So in these exemples, i wonder what the "onXXX" and "afterXXX" means... nothing?
Sebastien Lorber
i guess the signature is correct yes. This exemple was given by Adam Bien
Sebastien Lorber
It takes a String, how can it be connected to the approriate event?
tkr
@Sebastien Lorber note that the order of showing answers is random, not chronological.
Bozho
tkr actually it seems all observers with a String attribute signature will be triggered i think, including onHelloParis.Bozho i know but i used a DateComparator() :)
Sebastien Lorber