选择某些CDI事件观察器 [英] Select certain CDI event observer

查看:84
本文介绍了选择某些CDI事件观察器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有几个班观察一些CDI事件。可以声明在不同情况下应该使用哪个类?

I have a few classes that observe some CDI event. Is it possible to declare which class should be used in different cases?

例如,我有一个生成事件的类:

For example I have class that produce events:

class Producer
   {
        @Inject
        private Event<SomeEvent> event;
        public void fire()
        {
             event.fire(new SomeEvent());
        }
   }

我有两个消费者类别:

class ConsumerA
{
       public void onEvent(@Observes SomeEvent event);
}
class ConsumerB
{
       public void onEvent(@Observes SomeEvent event);
}

如何使用 ConsumerA 个实例,另一种实例中有 ConsumerB 个实例?

How can I use ConsumerA instances in one case and ConsumerB instances in other case?

我需要这样的东西:

class UseCaseA
{
    @Inject
    Producer producer;
    @Inject
    ConsumerA consumerA;
    public void doWork()
    {
         producer.fire(); //consumerA.onEvent() will be called
    }
}
class UseCaseB
{
    @Inject
    Producer producer;
    @Inject
    ConsumerB consumerB;
    public void doWork()
    {
         producer.fire(); //consumerB.onEvent() will be called
    }
}


推荐答案

这里的技巧是使事件和观察者都具有资格。

The trick here is to Qualify both the event and the observer.

在此处查看jee6教程,尤其是 @Credit @借项限定符。

要动态执行此操作,而不是注入其他事件来触发,您可以使用 select(java.lang.annotation.Annotation ... qualifiers)单个事件上的方法。

To do this dynamically instead of injecting different events to fire you can use the select(java.lang.annotation.Annotation... qualifiers) methods on a single event.

限定观察者的资格,然后致电

Qualify your observers and then call.

event.select(....一些注释文字。。)。fire();

您可以将消防代码放在超类中,然后让每个子类提供注释文字。

You could have the fire code in a superclass and then have each subclass supply the annotation literal.

注释文字是javax.enterprise.util.AnnotationLiteral。
您将需要为每个要使用的限定符创建一个注释文字,但是可以将它们实例化并作为对象传递。

Annotation literal is javax.enterprise.util.AnnotationLiteral. you will need to create an annotation literal for each Qualifier you want to use but these can be instantiated and passed as objects.

BTW您不需要注入您的观察者。这是错误的。您应该可以为每个事件添加任意数量的观察者,而不必重新访问事件的来源。您的容器应在启动时实例化它们。

BTW you do not need to inject your observers. This defeats the point. You should be able to add as many observers as you like for each event and not have to revisit the source of the event. Your container should instantiate them on startup.

如果效果更好,您还可以根据事件类的子类来选择事件。

You can also select the event on the basis of a subclass of the event class if that works better.

编辑:
我只是想在此方面添加有关使用事件的哲学的想法。

Just thought I'd add my thoughts to this about the philosophy of using events.


  1. 引发事件的组件既可以指定有效负载的特定类型,也可以指定附加到事件的限定符,但不指定,也不应实际选择将处理事件的观察者。

  2. 您可以为一个事件添加许多观察者。可能有:一个发送电子邮件;一个写审核日志;一个发送短信;一个写一条JMS消息;并且由于一次 event.fire(...)调用而立即触发所有更多事件。

  3. 观察员应具有如上所述的单一目的。

  1. The component that is firing the event can specify both the specific type of the payload and qualifiers that are attached to the event but does not and should not actually choose the observer that will process the event.
  2. You can add many observers for any one event. There may be: one to send an email; One to write an audit log; One to send a text message; One to write a JMS message; and many more all firing at once as a result of a single event.fire(...) call.
  3. Observers should IMO have a single purpose like those described above.

观察员不应依赖任何执行顺序(事务性与非事务性除外...请参阅如下),因为不能保证。如果需要保证订单,则观察者应触发另一个事件,该事件的观察者可以按顺序进行后续处理。

Observers should not rely on any order of execution (except transactional vs non-transactional ... see below) as it cannot be guaranteed. If you need to guarantee the order then the observer should fire a different event and an observer for that event can do the later processing in the order.

这篇关于选择某些CDI事件观察器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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