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

问题描述 投票:0回答:1

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

大家好,

我希望出现以下情况:1)在我的应用程序中创建了一个通知(在数据库中)2)之后,应将推送通知发送到特定客户端3)在客户端中,它将更新我页面中的特定@form ...

这是我的代码:

@Stateless
public class NotificationCreationSendServiceBean implements NotificationCreationSendService {

@Inject
private BeanManager beanManager;

public void createNotification {

// createNotificationInDatabase();
.....

        PushEvent event = new PushEvent("Test");
        beanManager.fireEvent(event);
}
}

我的JSF Bean:

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中定义通道名称?

有人可以帮我吗。

jsf omnifaces
1个回答
0
投票

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

此标题很奇怪,因为您的问题已经显示了完全正确的代码。

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

在当前情况下,这个特定问题确实很奇怪。我只能假定您实际上有多个@Observes PushEvent方法,并且您实际上只想针对与特定@Push通道相关联的特定方法。仅在这种情况下,这个问题才有意义。

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

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

    beanManager.fireEvent(new PushEvent("notificationChannel", "Test"));
    

    然后只需在您的观察者方法中进行检查:

    if ("notificationChannel".equals(event.getChannelName())) {
        // ...
    }
    

    随意使用枚举。


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

    beanManager.fireEvent(new NotificationEvent("Test"));
    

    然后确保只用一种方法观察它:

    public void pushUser(@Observes NotificationEvent event) {
        // ...
    }
    

  3. 或者,为@Qualifier创建一个PushEvent

    @Qualifier
    @Retention(RUNTIME)
    @Target({ FIELD, PARAMETER })
    public @interface Notification {}
    

    您通过@Inject通过Event<T>

    @Inject @Notification
    private Event<PushEvent> pushEvent;
    
    public void createNotification {
        pushEvent.fire(new PushEvent("Test"));
    }
    

    然后确保只用一种方法观察它:

    public void pushUser(@Observes @Notification PushEvent event) {
        // ...
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.