I have two ManagedBeans.
Concerning my problem, they do the following:
First:
@ManagedBean public class Provider { private Event<ProvideEvent> event; private static boolean handling = false; public provide(@Observes ConsumeEvent consume){ if(!handling){ //provide some stuff event.fire(new ProvideEvent(ProvidedStuff stuff); } } } Second:
@ManagedBean @SessionScoped public class Consumer { private Event<ConsumeEvent> event; @PostConstruct public void initialize(){ event.fire(new ConsumeEvent()); } private static boolean handling = false; public consume(@Observes ProvideEvent providedStuff){ if(!handling){ //use the provided stuff } } } This happens, when the website is called: 1. Consumer is instantiated. 2. Consumer fires the event. 3. Provider is instantiated. 4. provide() is called. 5. A NEW CONSUMER IS INSTANTIATED 6. consume() is called.
As you can see, I had to use a boolean "handling" to keep the application from looping infinitly.
Why is the container not using the instantiated SessionScoped ManagedBean? I thought SessionScoped ManagedBeans are like Singleton for the Session? I guess I could work around this by: A: Using static variables for the changed properties. B: Implementing the Observer-Pattern manually.
But there has to be an easier way here!?