我需要帮助来帮助@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的哪里定义通道名?
请问有人可以帮我吗。
答案 0 :(得分:1)
如何通过EJB通过o:socket向客户端发送推送?
这个标题很奇怪,因为您的问题已经显示了完全正确的代码。
如何用Socket识别我的@EJB容器是正确的容器?我在@EJB的哪里定义通道名?
在当前情况下,这个特定问题确实很奇怪。我只能假设您实际上有多个@Observes PushEvent
方法,并且您实际上只想针对与特定@Push
通道相关联的特定方法。仅在这种情况下,这个问题才有意义。
为了达到这个目的,有几种方法。
将其作为PushEvent
类的参数/属性传递:
beanManager.fireEvent(new PushEvent("notificationChannel", "Test"));
然后在您的观察者方法中进行检查:
if ("notificationChannel".equals(event.getChannelName())) {
// ...
}
随意使用枚举。
或者,为每个特定事件创建一个特定类:
beanManager.fireEvent(new NotificationEvent("Test"));
然后确保只用一种方法观察它:
public void pushUser(@Observes NotificationEvent event) {
// ...
}
或者,为@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) {
// ...
}