0 Replies Latest reply: Oct 11, 2011 10:02 AM by Juan Ignacio Garzon RSS

Observe Events raised by another conversation

Juan Ignacio Garzon Newbie

Hi, I'm developing a messaging subsystem that has two main beans.


The first one is an APPLICATION scoped bean, that has a method to publish a message:



@Name("gestorMensajeria")
@Scope(ScopeType.APPLICATION)
@Synchronized
public class GestorMensajeria {
     @In
     EntityManager entityManager;

     public void publicarMensaje(Mensaje mensaje) {
          mensaje.setEstado(EstadoMensaje.ENVIADO);
          entityManager.persist(mensaje);
          entityManager.flush();
          Events.instance().raiseEvent("mensajeEnviado",
                    mensaje.getDestinatario());
     }
}


It simply flushes the message, and raises an event, passing the user that should receive the message (mensaje.getDestinatario() has a reference to the receiver user object)


On the other hand (the receiver of the message), I have a CONVERSATION scoped Entity Query that lists the messages for the current user. I want to refresh the query every time a message is received for the current user:



@Name("mensajesRecibidosQuery")
@Scope(ScopeType.CONVERSATION)
public class MensajesRecibidosEntityQuery extends EntityQueryPaginada<Mensaje> {
     @In
     Usuario usuarioActual;

     @Observer("mensajeEnviado")
     public void onMensajeRecibido(Usuario usuario) {
          if (usuario != null && usuario.equals(usuarioActual)) {
               this.refresh();
          }
     }

     public MensajesRecibidosEntityQuery() {
          setEjbql("select m from Mensaje m ");
          List<String> restricciones = new ArrayList<String>();
          restricciones.add("m.destinatario=#{usuarioActual}");
          setRestrictionExpressionStrings(restricciones);
     }
}



The problem is that the method marked as @Observer(mensajeEnviado) is called only for the MensajesRecibidosEntityQuery bean in the Conversation scope of the sender. I mean, there are multiple instances of mensajesRecibidosQuery Entity Query objects living in different conversation scopes, and only one (the one that belongs to the sender of the message) is called. Is this a bug? Shouldn't the event mechanism ignore scopes and call every single bean instance that is marked as @Observer?


Please help!
Thanks for you time.