0

I am trying to find an analog to Seam 2 Factory paradigm in CDI. I have used factory annotated methods and factory definitions in components.xml extensively in the past projects. Generally I use them as a sort of Cache and when I know the cache is stale I remove the component from the context and the factory method will run again the next time the Factory is required.

For instance, in a production application we have lists of available medications that gets updated yearly. This list is used extensively in the application so we cache it in memory using a Factory annotated Application scoped method.

@Factory(scope = ScopeType.APPLICATION, autoCreate = true, value = "meds")
public List<Medications> buildMedsList(){
///....Do work to parse, index and build list
}

Once the new list is uploaded we simply remove the "meds" Object from the Application Context and then access it to be sure it is cached

Contexts.getApplicationContext().remove("meds");
Component.getInstance("meds");

There are a LOT of lists/objects that behave similarly. Supplying everything from SelectItem lists for radio button groups to dialog message text and end user license text. All of which have lifetimes that are VERY long but are required to be update-able without restarting the application server. I also use this same technique with some session scoped variables which have an unknown lifespan (May need to be refreshed dependant on user interactions).

So far I see the CDI Producer which is very similar to a Factory but I can't see how it can be used in a similar way to how I was using the Seam 2 Equivalent without building a wrapper class for each factory. Also I don't see a way to remove things from the CDI contexts. Therefore, I can't have these arbitrarily long lived objects without some special scope that I invent.

I am totally new to CDI so I am probably missing some technique.

cyberoblivion
  • 669
  • 7
  • 23
  • AFAIK such feature is planned for DeltaSpike but I don't know when it will be developed. – Adrian Mitev Sep 27 '13 at 10:15
  • Excellent, I just started playing with DeltaSpike. So far very encouraging. – cyberoblivion Sep 27 '13 at 14:24
  • 1
    See this answer http://stackoverflow.com/questions/18774025/how-can-i-update-a-collection-that-is-produces-applicationscoped/18774723#18774723, seems to be what you need, albeit it's more work. – rdcrng Sep 29 '13 at 17:52
  • Yeah, that makes sense. It will make for an incredible amount of boilerplate code. I hope DeltaSpike will come through on this, it is a powerful feature to loose. Anyway, if you want to post your comment as an answer I will accept it. – cyberoblivion Oct 01 '13 at 13:50

0 Answers0