1

I have a JSF @ApplicationScoped bean, MyBean.

This bean of course is instantiated when I start my JSF application.

I have a RefreshListsTask implements Runnable which takes a reference to this bean using the JSF ManagedProperty annotation.

@ManagedProperty(value="#{myBean}")
private MyBean myBean;

This task queries the database every N minutes refreshing a List inside myBean

To my surprise I've found out I can't get a refreshed list when, from another bean, I invoke:

((MyBean)Faces.evaluateExpressionGet("#{myBean}")).getList();

So I created a new property on this bean, and now it works:

@ManagedProperty(value="#{myBean}")
private MyBean myBean;

...

myBean.getList();

#{myBean} is being replaced with a new instance at some point (and I get the fresh list obtained when this bean is created, which is never updated again by the RefreshListsTask, which keeps updating the list on the original bean).

I don't know if this is an issue related to JSF or Omnifaces or if I am doing somethig wrong on my application. I'm almost certain it is the latter, but I can't figure out where is this replacement happening. Any ideas on how to trace this?

NotGaeL
  • 8,344
  • 5
  • 40
  • 70
  • `@ManagedProperty` in a `Runnable` class? Doesn't sound good. At least, the top100 example here must get you started as to design: http://stackoverflow.com/questions/6149919/is-it-safe-to-start-a-new-thread-in-a-jsf-managed-bean In short: you must design it the other way round as compared to your current approach. – BalusC Aug 26 '15 at 13:55
  • Actually the `ManagedProperty` was not in the `Runnable`. I had an `@ApplicationBean`, `RefreshListsTaskScheduler`, where the `@ManagedProperty` is retrieved and passed to the RefreshListsTask implementing `Runnable`. I oversimplified the example. – NotGaeL Aug 26 '15 at 14:49
  • Anyway, now I don't pass any managed property to my `Runnable`. I pass the reference to the `RefreshListsTaskScheduler`, and the `RefreshListsTaskScheduler` takes care of the actual refresh. I don't use managed properies anymore, just the `Faces.evaluateExpressionGet` method, and that fixes the problem, but not the cause. I still get different object ids for the application bean retrieved as a `@ManagedProperty` and as `Faces.evaluateExpressionGet`; it happens only on some runs; sometimes the application starts and both ids are the same and nothing happens... – NotGaeL Aug 26 '15 at 14:51
  • ..., and I still can't identify what triggers this issue. – NotGaeL Aug 26 '15 at 14:53
  • Anyway, thanks for the link. I had already checked it, as well as your other excellent answer: http://stackoverflow.com/questions/7499534/spawning-threads-in-a-jsf-managed-bean-for-scheduled-tasks-using-a-timer which I followed for the implementation I have right now (using the `ScheduledExecutorService` on my `RefreshListsTaskScheduler` and the `@PostConstruct` and `@Predestroy` methods). – NotGaeL Aug 26 '15 at 14:57
  • Can you provide the problem in MCVE format with annotations and all? There must be a timing error at the moment you call evaluateExpressionGet(). – BalusC Aug 26 '15 at 20:14
  • I've been trying to, but no luck. Whatever it is causing the problem I am not able to pinpoint it clearly enough. Now that I have a workaround I'll let it rest and try again with a fresher mind and more time to analyze it. If I find something I'll be posting it here. Thanks for the help :-) – NotGaeL Aug 26 '15 at 21:54
  • I have this idea that maybe it is something related to serialization. Maybe my `Serializable` `@SessionBean` is serializing and saving a `@ManagedProperty` on my `Serializable` `@ApplicationBean`. Maybe when I restart the application a new `@ApplicationBean` is created but the deserialization recovers the old one on the `@SessionBean` `@ManagedProperty`, and that's why I have two instances and two different references for my `@ApplicationBean`? – NotGaeL Aug 27 '15 at 17:49

0 Answers0