Java 无状态会话Bean中的激发事件不工作
我是WebSocket Java开发的新手,我有一个会话bean,其中包含以下服务:Java 无状态会话Bean中的激发事件不工作,java,events,java-websocket,Java,Events,Java Websocket,我是WebSocket Java开发的新手,我有一个会话bean,其中包含以下服务: @Stateless @LocalBean public class TransferService implements TransferServiceLocal { @Inject private Event<PushNotification> notificationEvent; @PersistenceContext private EntityManager em; private
@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);
}
}
@无状态
@本地豆
公共类TransferService实现TransferServiceLocal{
@注入
私人事件通知事件;
@持久上下文
私人实体管理者;
私有PushNotification createNotification(整数用户ID、字符串标题、,
字符串消息){
PushNotification PushNotification=新的PushNotification();
pushNotification.setUserId(userId);
pushNotification.setTitle(title);
pushNotification.setMessage(消息);
pushNotification.setDcr(新日期());
//将新推送通知通知观察者
notificationEvent.fire(推送通知);
退货通知;
}
@凌驾
公共转让(转让转让){
empersist(createNotification(visitor.getId(),“新任务”,“您有新任务”);
em.persist(转移);
}
}
我希望当我在EntityManager上创建一个新的持久化时,会从websocket向客户端发送一个新的通知,问题是fire事件不起作用
下面是我的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();
}
}
import javax.enterprise.event.asp;
导入javax.websocket.server.ServerEndpoint;
导入persistence.PushNotification;
@ServerEndpoint(“/testws”)
公共类测试点{
非公开会议登记处会议登记处;
public void send(@notification通知)引发IOException{
Set sessions=sessionRegistry.getAll();
用于(会话:会话){
session.getBasicRemote().sendText(toJson(通知));
}
System.out.println(“新通知,消息:”
+notification.getMessage());
}
私有字符串toJson(PushNotification通知){
最终JsonObject JsonObject=Json.createObjectBuilder()
.add(“id”,notification.getId())
.add(“message”,notification.getMessage()).build();
返回jsonObject.toString();
}
}
我的代码出了什么问题?请告诉我,我在过去两天遇到了这个问题,但仍然感到困惑:(