Событие пожара в сеансе без состояния Bean не работает

Я новичок в разработке WebSocket Java, у меня еще есть сессионный компонент, который содержит службы:

@Stateless
@LocalBean
public class TransferService implements TransferServiceLocal {

@Inject
private Event<PushNotification> notificationEvent;

@PersistenceContext
private EntityManager em;

private PushNotification createNotification(Integer userId, String title,
        String message) {
    PushNotification pushNotification = new PushNotification();
    pushNotification.setUserId(userId);
    pushNotification.setTitle(title);
    pushNotification.setMessage(message);
    pushNotification.setDcr(new Date());
    // Notify the observers about the new push notification
    notificationEvent.fire(pushNotification);
    return pushNotification;
}

@Override
public void addTransfer(Transfer transfer) {
    em.persist(createNotification(visitor.getId(), "New Task", "You have new task"));
    em.persist(transfer);

}
}

Я хочу, чтобы при создании новой персистенции на EntityManager было отправлено новое уведомление от веб-сокета клиенту, проблема в том, что событие огня не работает!!

и вот что у меня есть в моем WebSocket POJO:

import javax.enterprise.event.Observes;
import javax.websocket.server.ServerEndpoint;
import persistence.PushNotification;

@ServerEndpoint("/testws")
public class TestEndpoint {

private SessionRegistry sessionRegistry;

public void send(@Observes PushNotification notification) throws IOException{

    Set<Session> sessions = sessionRegistry.getAll();
    for (Session session : sessions) {
        session.getBasicRemote().sendText(toJson(notification));
    }

    System.out.println("new notification, message : "
            + notification.getMessage());
}

private String toJson(PushNotification notification) {
    final JsonObject jsonObject = Json.createObjectBuilder()
            .add("id", notification.getId())
            .add("message", notification.getMessage()).build();
    return jsonObject.toString();
}
}

что не так в моем коде, пожалуйста, у меня есть эта проблема в течение последних двух дней и до сих пор смущен:(

0 ответов

Другие вопросы по тегам