Asynchronous CDI Events

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

1.Default Synchronous Events

先看一个基本的例子

@Path("/produce")
public class EventGenerator {

    @Inject
    private Logger logger;

    @Inject
    private Event<MyEvent> 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
@Singleton
public 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)):
@Singleton
public 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) { ... }
}

但是更改了观察者方法

@Singleton
public 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值