Reputation: 5306
I've got a mishmash of JAX-RS webservices and JSF/CDI beans. Usual display of my @Entity
s is from a @ViewScoped
JSF bean collecting relevant entities in a @PostConstruct
method:
@Named @ViewScoped
public class Manager {
private List<MyEntity> entities; // + getter
private MyEntity instance; // + getter/setter
@PostConstruct
public void init() {
entities = collectEntities();
instance = new MyEntity();
}
public void save() {
instance = persistInstance();
entities.add(instance);
}
// additional methods like collectEntities, persistInstance
}
Normal operation can call manager.save
to persist a new entity and display it alongside the old ones.
Now, a JAX-RS service can also create entities that should be in the collection managed by such a scoped bean:
@Path("/myentity")
public class MyEntityService {
@PersistenceContext EntityManager em;
@PUT
public Response save(@FormParam("name") String name) {
MyEntity entity = new MyEntity(name);
em.persist(entity);
return Response.ok(entity.getId()).build();
}
}
The service can be called on a page where there's also a manager
instance.
My question is: how can I make the existing manager
instance aware of the additional entity, so that a JSF ajax re-render of a manager.entities
list will include the entity created by the webservice?
So far, I've tried a CDI event observed by the CDI bean. The event gets fired from the service but is never received by the bean.
As a workaround I can fire a JSF ajax function telling the manager
to refresh it's entity list (leveraging <a4j:jsFunction action="#{manager.init()}">
, for example). However I'm unsure about the implications: will this expose a timing problem when the user asks for the entity list to be displayed earlier than the initialization can complete (the list isn't shown by default)?
As a total hack I can probably grab the bean from the session in the service and punch my data in. I shudder just thinking about it.
Upvotes: 0
Views: 194
Reputation: 11723
View scope is something that is JSF specific, as a JSF specific CDI context. It is alive only within the scope of the given view. JAX-RS has no specific way that I can think of to access this scope. I don't believe view scope would even have access to the HTTP request.
Upvotes: 0