how to use event in weld (cdi)

634 views Asked by At

I am studying Weld Event from jboss weld event tutorial and I want to write an example which observes an event and print helloword when it was fired.

this is my code:

//MyEvent when it was fired, print HelloWorld
public class MyEvent{}

//observe MyEvent and when it happen print HelloWorld
public class EventObserver {
    public void demo(@Observes MyEvent event){
        System.out.println("HelloWorld");
    }
}

//Main Class fire Event in demo method
public class EventTest {
    @Inject @Any Event<MyEvent> events;
    public void demo(){
        Weld weld = new Weld();
        WeldContainer container = weld.initialize();
        events.fire(new MyEvent());
        container.shutdown();
    }
    public static void main(String[] args){
        EventTest test = new EventTest();
        test.demo();
    }
}

it doesnt work and give below exception info:

Exception in thread "main" java.lang.NullPointerException
       at weldLearn.event.EventTest.demo(EventTest.java:18)
       at weldLearn.event.EventTest.main(EventTest.java:24)

it seems that there are no beans in container which can initialize

Event<MyEvent> events;

Then what should I do to make it running, my beans.xml is empty

  • Maybe I should do something in beans.xml?
  • or I should write a java class implements Event interface?
    anything will be apprecited.
1

There are 1 answers

0
John Ament On BEST ANSWER

Basically, your code fails because you're not using a managed instance of your class. Here's a better way to do it

@ApplicationScoped
public class EventTest {
  @Inject Event<MyEvent> events;
  public void demo(){
      events.fire(new MyEvent());
  }
  public static void main(String[] args){
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    EventTest test = container.select(EventTest.class).get();
    test.demo();
    container.shutdown();
  }
}

You start the container in main, and use a managed reference to your class. Injection points are only resolved when you're using managed references.