How can I update a collection that is @Produces @ApplicationScoped?

梦想的初衷 提交于 2019-12-10 18:27:00

问题


I'm currently migrating away from Seam's @Factory annotation. Combined with @Observer, I could do this:

@Factory(value = "optionsList", scope = ScopeType.APPLICATION)
@Observer("entity.modified")
public List<MyBean> produceEntityOptions() {
    List l = getEm().createQuery('select e from entity e').getResultList();
    Contexts.getApplicationContext().set("optionsList", l);
    return l;
}

Which would cache a list of possible options for use in e.g. <f:selectItems> (the actual computation can be more complicated).

I've translated this for use with CDI to

@Produces @Named("optionsList") @ApplicationScoped
public List<MyBean> produceEntityOptions() {
    return getEm().createQuery('select e from entity e').getResultList();
}

but this loses the ability to recreate the cache (only) when an external event signals the cache has gone stale. How can I get that back?


回答1:


Here's what you could do:

@ApplicationScoped
public class MyListProducer {

    // the current list
    private List<MyBean> listOfBeans;

    // resets / reloads/ refreshes list
    private void loadList() {
        this.listOfBeans = getEm().createQuery('select e from entity e').getResultList();
    }

    // initialize the list
    @PostConstruct
    protected void postConstruct() {
        loadList();
    }

    // listen for the stale event - you'll have to create a type (maybe even qualifiers) yourself
    private void resetList(@Observes MyCustomListIsStaleEvent evt) {
        loadList();
    }

    // the producer - to ensure that the producer is called after you refresh the list, make the list of scope @Dependent instead of @ApplicationScoped
    @Produces @Named("optionsList")
    protected List<MyBean> getList() {
        return this.listOfBeans;
    }
}

I think that in effect, this is what you want. But I don't exclude the possibility that there might be differences - don't know Seam very much.

Side note: You should think about synchronizing the observer and the producer methods, either with plain old synchronization or by making the above a stateful session bean and taking advantage of EJB synchronization mechanisms.



来源:https://stackoverflow.com/questions/18774025/how-can-i-update-a-collection-that-is-produces-applicationscoped

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!