Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

event listener - 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

question from:https://stackoverflow.com/questions/65873600/why-the-cdi-is-not-working-in-a-singleton-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...