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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…