远程EJB中的可观察模式 [英] Observable pattern in Remote EJB

查看:212
本文介绍了远程EJB中的可观察模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个应用程序。一个主,另一个是远程EJB。第一个应用程序在第二个应用程序中调用Remote EJB。



现在我要实现Observable模式。但是它似乎没有跨应用程序的工作。



所以我想要我的Observable在第二个应用程序,我的观察者在第一个。当我尝试它似乎没有工作。只有在同一个应用程序中使Observable和Observers才可以工作。



我有另一个解决方案,但我觉得很脏。我可以在第二个应用程序中使用MDB,并在主题上发布消息,然后在第一个应用程序中使用这些消息。但是再一次,这似乎很脏。



我在Payara 4.1.1.171服务器上使用JEE7。



编辑
因为我发现触发的事件不会退出EAR文件,我现在正在使用MDB方法。但这还是不行。
更多信息,这是EAR文件的包结构。

  my-application.ear 
+ --- common-ejb.jar(EJB)
| + --- MessageDrivenBean.java(触发事件)
+ --- my-application.jar(EJB)
+ --- my-application.war(WEB)
+ - WEB-INF
+ --- lib
| + --- common-web.jar(资源WEB jar)
| + --- SessionBean1接收事件
+ --- classes
+ --- SessionBean2接收事件

正如你可以看到有2个会话bean来接收事件,但是没有一个实际接收到它们。

解决方案

使用Payara服务器版本171和Hazelcast启用,您可以启动CDI事件并在另一个应用程序中观察它们。请参阅有关远程CDI事件



如果这两个应用程序都部署在同一个Payara Server实例上,那么您将触发类似MyEvent.class的事件,如下所示:

  @Inject 
@Outbound(loopBack = true)//如果观察应用程序部署在相同的Payara服务器实例上,则需要loopBack true
事件< MyEvent>事件;

...
event.fire(new MyEvent());

另一个应用程序将观察到以下事件:

  public void receiveEvent(@Observes @Inbound MyEvent event){
}

如果观察应用程序在同一个Hazelcast群集中的另一个Payara Server实例上运行,则还会收到该事件。在这种情况下,loopBack属性不是必需的,但也不会造成任何影响。



远程CDI事件通过嵌入式Hazelcast数据网格发送,这将可靠地将事件传递到所有在群集中观察应用程序。观察应用程序必须在事件发生时运行 - 通过JMS主题与广播事件相似的行为。


I have 2 applications. One 'main' and a second one with a Remote EJB. The first application calls the Remote EJB in the second one.

Now I want to implement the Observable pattern. But it doesn't seem to work cross-application.

So I want my Observable inside the second application, and my Observers in the first. When I try it, it doesn't seem to work. It only works if I make the Observable and Observers in the same application.

I got another solution, but I think it's dirty. I could use a MDB inside the second application, and publish a message on a Topic, and then consume those messages inside the first application. But then again, that seems kinda dirty.

I'm using JEE7 on a Payara 4.1.1.171 server.

Edit: Because I figured out that triggered Events don't exit an EAR file, I'm now using the MDB method. But that still does not work. More information, this is the package structure of the EAR file.

my-application.ear
+--- common-ejb.jar (EJB)
|       +--- MessageDrivenBean.java (Triggers the event)
+--- my-application.jar (EJB)
+--- my-application.war (WEB)
        +--- WEB-INF
                +--- lib
                |       +--- common-web.jar (Resources WEB jar)
                |           +--- SessionBean1 to receive the events
                +--- classes
                        +--- SessionBean2 to receive the events

As you can see there are 2 session beans to receive the events, but none of the 2 actually receive them.

解决方案

With Payara Server version 171 and Hazelcast enabled, you can fire CDI events and observe them in another application. See the documentation about remote CDI events

If both applications are deployed on the same Payara Server instance, you would fire an event of type MyEvent.class like this:

@Inject
@Outbound(loopBack = true) // loopBack true is required if the observing application is deployed on the same Payara Server instance
Event<MyEvent> event;

...
event.fire(new MyEvent());

And the other application would observe these events with:

public void receiveEvent(@Observes @Inbound MyEvent event) {
}

If the observing application is running on another Payara Server instance in the same Hazelcast cluster, it would also receive the event. In that case, loopBack attribute is not required, but would do no harm either.

Remote CDI events are dispatched via embedded Hazelcast data grid, which will reliably deliver the events to all observing applications across the cluster. The observing application has to be running at the time when the event is fire - a similar behavior to broadcasting events via a JMS topic.

这篇关于远程EJB中的可观察模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆