event-listenerobserver-patternjava-ee-8cdi-2.0

Why the CDI is not working in a singleton class


A singleton class is firing an Event but the container is not calling the CDI Event Listener.

Below, the creatData() in the DataLoaderSessionBean singleton class calls the loadUsers() method and in turn the loadUsers() is firing an event, which suppose to invoke XMLDataListener.UserData() but the call never happens. However, if I change the class from a Singleton to a Stateful or Stateless session bean then everything works well.

@Record
@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)

public class DataLoaderSessionBean {

    @Inject
    @UserXMLData
    Event<DataEvent> userData;
    
    private final static int LOAD=0;

    private void loadUsers() {
        DataEvent event = new DataEvent();
        event.setCommand(LOAD);
        userData.fire(event);
    }
    
    @PostConstruct
    public void createData() {
        loadUsers();
        .........
    }
}

Here is the event listener. The userData() method on this event listener class is not being called by the container.

@Record
@SessionScoped
public class XMLDataListener implements Serializable {

    private static final long serialVersionUID = -2230122751970858111L;
    private final static int LOAD=0;
    public void UserData(@Observes @UserXMLData DataEvent event) {
        int cmd = event.getCommand();
        switch(cmd){
            case LOAD: loadUsers();
            break;
            ..........
            
        }
    }
}

The listener interface:

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface UserXMLData {
}

The event data

public class DataEvent implements Serializable {
    
   private static final long serialVersionUID = -2230122751970857224L;
   private int command;

    public DataEvent() {
    }

    public int getCommand() {
        return command;
    }

    public void setCommand(int command) {
        this.command = command;
    }   
}

Any idea why this functionality works well for the session beans and not for the singleton?

Thanks


Solution

  • This is the required behaviour by the CDI 2.0 spec, section 10.5

    If there is no context active for the scope to which the bean
    declaring the observer method belongs, then the observer method
    should not be called.

    The @PostConstruct method of the singleton it's called on startup on the application context, so there is no session active. To trigger the observer method XMLDataListener should be declared annotated with @ApplicationScoped.