博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asynchronous CDI Events
阅读量:5968 次
发布时间:2019-06-19

本文共 9999 字,大约阅读时间需要 33 分钟。

hot3.png

CDI事件本身是属于同步的,但在一些场景下,你可能会需要异步的CDI events。本文将概述异步cdi event事件。

1.Default Synchronous Events

先看一个基本的例子

@Path("/produce")public class EventGenerator {    @Inject    private Logger logger;    @Inject    private Event
events; @Path("/cdiBean/{eventsNum}") @GET public String generateEvents(@PathParam("eventsNum") int numberOfEventsToGenerate) { for (int i = 0; i < numberOfEventsToGenerate; i++) { MyEvent event = new MyEvent(i); logger.info("Generating Event: " + event); events.fire(event); } return "Finished. Generated " + numberOfEventsToGenerate + " events."; }}
MyEvent只是一些事件对象,它在这里不是很重要。
消费者是一个非常简单的CDI bean:
public class EventConsumer {    @Inject    private Logger logger;    public void consumeEvent(@Observes MyEvent myEvent) throws InterruptedException {        logger.info("Receiving event: " + myEvent);        TimeUnit.MILLISECONDS.sleep(500);    }}
请注意,我写一个线程睡眠来模拟一些长时间运行的事件接收器模拟复杂费时的处理过程。
现在,让我们通过调用REST命令,这是EventProducer露出运行这个例子。
14:15:59,196 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=0]14:15:59,197 [com.piotrnowicki.EventConsumer] (http-/127.0.0.1:8080-1) Receiving event: MyEvent[seqNo=0]14:15:59,697 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=1]14:15:59,698 [com.piotrnowicki.EventConsumer] (http-/127.0.0.1:8080-1) Receiving event: MyEvent[seqNo=1]14:16:00,199 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=2]14:16:00,200 [com.piotrnowicki.EventConsumer] (http-/127.0.0.1:8080-1) Receiving event: MyEvent[seqNo=2]
通过观察,我们可以得到结论,其cdi event默认就是同步的。下面我们将折腾异步的。

Solution 1 – CDI Producer and Singleton EJB as Receiver

我们的生产者保持不变

@Path("/produce") public class EventGenerator {    @Path("/cdiBean/{eventsNum}")    @GET    public String generateEvents(@PathParam("eventsNum") int numberOfEventsToGenerate) { ... }}
现在你把接受者设置为 @Singleton EJB,并且标记oberves方法为@Asynchronous
@Singletonpublic class EventConsumer {    @Asynchronous    public void consumeEvent(@Observes MyEvent myEvent) throws InterruptedException { ...  }}
你将得到以下结果
14:21:19,341 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=0]14:21:19,343 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=1]14:21:19,343 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=2]14:21:19,347 [com.piotrnowicki.EventConsumer] (EJB default – 2) Receiving event: MyEvent[seqNo=1]14:21:19,848 [com.piotrnowicki.EventConsumer] (EJB default – 1) Receiving event: MyEvent[seqNo=0]14:21:20,350 [com.piotrnowicki.EventConsumer] (EJB default – 3) Receiving event: MyEvent[seqNo=2]
事件一个接一个被Singleton EJB在单独的线程处理(看看时间事件处理。),
其实这里的consumeEvent是隐式的write:
异步:是
Ob方法是线程安全的:是

Solution 2 – Use Singleton EJB as Receiver With Read Lock

这种方法是和解决方案1非常相似的,但是,它提供了高得多的吞吐量,因为所有的事件平行处理。produce仍然不变。

@Path("/produce")public class EventGenerator {    @Path("/cdiBean/{eventsNum}")    @GET    public String generateEvents(@PathParam("eventsNum") int numberOfEventsToGenerate) { ... }}
我们的接收者的OB上有@Lock(READ);这使得能够在同一时间服务于多个事件(而在一中是Lock(Write)):
@Singletonpublic class EventConsumer {    @Asynchronous    @Lock(LockType.READ)    public void consumeEvent(@Observes MyEvent myEvent) throws InterruptedException { ... }}
result:
14:24:44,202 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=0]14:24:44,204 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=1]14:24:44,205 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=2]14:24:44,207 [com.piotrnowicki.EventConsumer] (EJB default – 4) Receiving event: MyEvent[seqNo=0]14:24:44,207 [com.piotrnowicki.EventConsumer] (EJB default – 6) Receiving event: MyEvent[seqNo=2]14:24:44,207 [com.piotrnowicki.EventConsumer] (EJB default – 5) Receiving event: MyEvent[seqNo=1]

因此,我们结论是,这样的写法给你更大的吞吐量.

异步:是 

Ob方法是线程安全的:不是

Solution 3 – EJB Producer and CDI Consumer

CDI可以让你在事务的特定阶段去处理观察者位置的事件。
你可以指定使用@Observes(during=TransactionPhase...).
在我们的例子中,我们希望CDI观察者事件在事务结束之后才进行。要做到这一点,我们只需要把属性添加到我们的CDI Bean的观察者方法注解上:
public class EventConsumer {    public void consumeEvent(@Observes(during = TransactionPhase.AFTER_COMPLETION) MyEvent myEvent) { ... } }

现在我们只需要确认我们在Event Generator方法运行的事务。我们可以通过改变我们的CDI Bean成为EJB @Stateless 和使用其隐含的事务--REQUIRED TransactionAttribute:

@Stateless@Path("/produce")public class EventGenerator {    @Path("/cdiBean/{eventsNum}")    @GET    public String generateEvents(@PathParam("eventsNum") int numberOfEventsToGenerate) { ... }}

结果:

14:39:06,776 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=0]14:39:06,776 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=1]14:39:06,776 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=2]14:39:06,778 [com.piotrnowicki.EventConsumer] (http-/127.0.0.1:8080-1) Receiving event: MyEvent[seqNo=2]14:39:07,279 [com.piotrnowicki.EventConsumer] (http-/127.0.0.1:8080-1) Receiving event: MyEvent[seqNo=1]14:39:07,780 [com.piotrnowicki.EventConsumer] (http-/127.0.0.1:8080-1) Receiving event: MyEvent[seqNo=0]
EJB Event Generator启动一个事务,CDI bean的观察者将在事务完成后,才可以调用。

Asynchronous: yes

Thread-safe observer method: yes

Solution 4 – EJB Producer and EJB Consumer

与3类似
@Stateless@Path("/produce")public class EventGenerator {    @Path("/cdiBean/{eventsNum}")    @GET    public String generateEvents(@PathParam("eventsNum") int numberOfEventsToGenerate) { ... }}

但是更改了观察者方法

@Singletonpublic class EventConsumer {    @Asynchronous    @Lock(LockType.READ)    public void consumeEvent(@Observes(during = TransactionPhase.AFTER_COMPLETION) MyEvent myEvent) throws InterruptedException { ...  }}

result 

14:44:09,363 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=0]14:44:09,464 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=1]14:44:09,564 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=2]14:44:09,670 [com.piotrnowicki.EventConsumer] (EJB default – 8) Receiving event: MyEvent[seqNo=2]14:44:09,670 [com.piotrnowicki.EventConsumer] (EJB default – 2) Receiving event: MyEvent[seqNo=1]14:44:09,670 [com.piotrnowicki.EventConsumer] (EJB default – 1) Receiving event: MyEvent[seqNo=0]
我们在这里使用的两个特点 - 一是该事件消费者的方法是异步的,第二个是生产者事务完成之前,消费者将不会通知。
Asynchronous: yes
Thread-safe observer method: no

Solution 4 vs Solution 2

这两个解决方案似乎是相同的。他们只是消费者的注释不同:@Observes vs @Observes(during = TransactionPhase.AFTER_COMPLETION). 
我们的测试的情况表明它们的作用是相同的:它们是异步的,并且多个线程可以在同一时间进行事件处理。但是,他们之间有一个很大的不同。
在我们的测试情况下,我们触发的事件一个接一个。试想一下,有一些其它的操作之间的事件触发。在这种情况下:
Solution 2 (@Observes)在第一个就会fire,启动处理events.
Solution 4 (@Observes(during = TransactionPhase.AFTER_COMPLETION)) 它只会在事务完成后开始处理,所以那个时候所有的events将被fired.
Solution 2 (@Observes)15:01:34,318 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=0]15:01:34,320 [com.piotrnowicki.EventConsumer] (EJB default – 3) Receiving event: MyEvent[seqNo=0]15:01:34,419 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=1]15:01:34,420 [com.piotrnowicki.EventConsumer] (EJB default – 6) Receiving event: MyEvent[seqNo=1]15:01:34,520 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=2]15:01:34,521 [com.piotrnowicki.EventConsumer] (EJB default – 9) Receiving event: MyEvent[seqNo=2]Solution 4 (@Observes(during = TransactionPhase.AFTER_COMPLETION))15:00:41,126 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=0]15:00:41,226 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=1]15:00:41,326 [com.piotrnowicki.EventGenerator] (http-/127.0.0.1:8080-1) Generating Event: MyEvent[seqNo=2]15:00:41,432 [com.piotrnowicki.EventConsumer] (EJB default – 10) Receiving event: MyEvent[seqNo=2]15:00:41,432 [com.piotrnowicki.EventConsumer] (EJB default – 4) Receiving event: MyEvent[seqNo=1]15:00:41,432 [com.piotrnowicki.EventConsumer] (EJB default – 5) Receiving event: MyEvent[seqNo=0]

Solution 5 – EJB Producer and CDI Consumer II

上述我们一直让观察者异步.我们也可以让事件生产者异步.
@Stateless@Path("/produce")public class EventGenerator {    // ...    @Resource    private SessionContext sctx;    @Path("/cdiBean/{eventsNum}")    @GET    public String generateEvents(@PathParam("eventsNum") int numberOfEventsToGenerate) {        for (int i = 0; i < numberOfEventsToGenerate; i++) {            sctx.getBusinessObject(EventGenerator.class).fireEvent(new MyEvent(i));        }        return "Finished. Generated " + numberOfEventsToGenerate + " events.";    }    @Asynchronous    public void fireEvent(final MyEvent event) {        events.fire(event);    }}
仔细看看EJB使用sessioncontext。在此情况下,因为我们希望在容器派遣我们方法的调用,并添加了它的异步性质。我们不希望使之成为本地呼叫,所以我们拒绝使用隐含此对象。
public class EventConsumer {    public void consumeEvent(@Observes MyEvent myEvent) throws InterruptedException { ... }}
00:40:32,820 [com.piotrnowicki.EventGenerator] (EJB default – 2) Generating Event: MyEvent[seqNo=1]00:40:32,820 [com.piotrnowicki.EventGenerator] (EJB default – 3) Generating Event: MyEvent[seqNo=2]00:40:32,820 [com.piotrnowicki.EventGenerator] (EJB default – 1) Generating Event: MyEvent[seqNo=0]00:40:32,821 [com.piotrnowicki.EventConsumer] (EJB default – 1) Receiving event: MyEvent[seqNo=0]00:40:32,821 [com.piotrnowicki.EventConsumer] (EJB default – 2) Receiving event: MyEvent[seqNo=1]00:40:32,821 [com.piotrnowicki.EventConsumer] (EJB default – 3) Receiving event: MyEvent[seqNo=2]
Asynchronous: yes
Thread-safe observer method: no

Solution 6 – CDI With JMS

https://weblogs.java.net/blog/jjviana/archive/2010/04/13/decoupling-event-producers-and-consumers-jee6-using-cdi-and-jms

转载于:https://my.oschina.net/zhaoqian/blog/351634

你可能感兴趣的文章
Vuex学习
查看>>
bootstrap - navbar
查看>>
服务器迁移小记
查看>>
FastDFS存储服务器部署
查看>>
Android — 创建和修改 Fragment 的方法及相关注意事项
查看>>
swift基础之_swift调用OC/OC调用swift
查看>>
Devexpress 15.1.8 Breaking Changes
查看>>
ElasticSearch Client详解
查看>>
mybatis update返回值的意义
查看>>
expdp 详解及实例
查看>>
通过IP判断登录地址
查看>>
深入浅出JavaScript (五) 详解Document.write()方法
查看>>
Beta冲刺——day6
查看>>
在一个程序中调用另一个程序并且传输数据到选择屏幕执行这个程序
查看>>
代码生成工具Database2Sharp中增加视图的代码生成以及主从表界面生成功能
查看>>
关于在VS2005中编写DLL遇到 C4251 警告的解决办法
查看>>
提高信息安全意识对网络勒索病毒说不
查看>>
使用Jquery 加载页面时调用JS
查看>>
css+div+jquery弹出层
查看>>
求职相关(链接,不定期更新)
查看>>