Jsf 如何通过o:socket将推送从EJB发送到客户端?

Jsf 如何通过o:socket将推送从EJB发送到客户端?,jsf,omnifaces,Jsf,Omnifaces,我需要@EJB和@CDI之间的合作帮助 大家好 我希望有以下场景: 1在我的应用程序中,在数据库中创建通知 2之后,应向特定客户端发送推送通知 3在客户端,它将从我的页面更新特定的@表单 这是我的密码: @Stateless public class NotificationCreationSendServiceBean implements NotificationCreationSendService { @Inject private BeanManager beanManager;

我需要@EJB和@CDI之间的合作帮助

大家好

我希望有以下场景: 1在我的应用程序中,在数据库中创建通知 2之后,应向特定客户端发送推送通知 3在客户端,它将从我的页面更新特定的@表单

这是我的密码:

@Stateless
public class NotificationCreationSendServiceBean implements NotificationCreationSendService {

@Inject
private BeanManager beanManager;

public void createNotification {

// createNotificationInDatabase();
.....

        PushEvent event = new PushEvent("Test");
        beanManager.fireEvent(event);
}
}
我的JSFbean:

import static org.omnifaces.util.Messages.addGlobalError;
import static org.omnifaces.util.Messages.addGlobalInfo;

@Named
@ViewScoped
public class NotificationSocket implements Serializable {

    @Inject
    private LoginBean loginBean;

    @Inject
    @Push(channel = "notificationChannel")
    private PushContext push;

    /**
     * Push Notification
     * 
     * @param recipientUser
     */
    public void pushUser(@Observes PushEvent event) {
        Set<Future<Void>> sent = push.send(event.getMessage(), loginBean.getCurrentEmployee().getId());

        if (sent.isEmpty()) {
            addGlobalError("This user does not exist!");
        } else {
            addGlobalInfo("Sent to {0} sockets", sent.size());
        }
    }
}
在此JSF页面中:

<o:socket channel="notificationChannel"
    user="#{loginBean.currentEmployee.id}" scope="view">
    <f:ajax event="someEvent" listener="#{bean.pushed}" render=":notificationLink" />
</o:socket>
我现在的问题是: 如何识别我的@EJB容器,Socket是否正确?在@EJB中,我在哪里定义通道名称

谁能帮帮我吗

如何通过o:socket将推送从EJB发送到客户端

这个标题很奇怪,因为您的问题已经显示了正确的代码

如何识别我的@EJB容器,Socket是否正确?在@EJB中,我在哪里定义通道名称

在目前的情况下,这个具体问题真的很奇怪。我只能假设您实际上有多个@PushEvent方法,并且您实际上只希望针对与特定@Push通道关联的特定方法。只有在这种情况下,这个问题才有意义

好吧,为了实现这一点,有几种方法

将其作为PushEvent类的参数/属性传递:

然后在观察者方法中检查:

if ("notificationChannel".equals(event.getChannelName())) {
    // ...
}
请随意使用枚举

或者,为每个特定事件创建特定类:

beanManager.fireEvent(new NotificationEvent("Test"));
然后确保只使用一种方法进行观察:

public void pushUser(@Observes NotificationEvent event) {
    // ...
}
public void pushUser(@Observes @Notification PushEvent event) {
    // ...
}
或者,为PushEvent创建@限定符:

您通过事件@injection的:


在Omnifaces中,示例也是@ViewScope?我不知道你是什么意思。。。。我可以定义:。。。。但是EJB容器中定义了什么通道呢?你们有什么例子吗?谢谢您的代码,它在EJB中的外观。。。。对不起,我完全不清楚好吧,我明白了-你不能或者你不想真的帮助。。。抱歉,那些简短的回答没有多大帮助
@Qualifier
@Retention(RUNTIME)
@Target({ FIELD, PARAMETER })
public @interface Notification {}
@Inject @Notification
private Event<PushEvent> pushEvent;

public void createNotification {
    pushEvent.fire(new PushEvent("Test"));
}
public void pushUser(@Observes @Notification PushEvent event) {
    // ...
}