I fixed problem by this simple code . (I shared this code for you)
public class Information {
private String name ;
private String family ;
// constructor
// Getter & Setter
// override equal and hashCode
}
This is a simple service . (I simulated database on this class)
@Stateless
public class InformationService {
private static final List<Information> db = new ArrayList<>();
@Inject
@Push(channel = "infoChannel")
PushContext push;
@PostConstruct
public void init() {
Information userA = new Information("John", "Vankate");
Information userB = new Information("Julius", "Sampao");
db.add(userA);
db.add(userB);
}
public void remove(Information info) {
db.remove(info);
push.send("deleteInfo");
}
public List<Information> findAll() {
return db;
}
}
and simple JaxRs resources :
@Path("/info")
@RequestScoped
public class InformationResources {
@EJB
private InformationService informationService;
@Path("/delete")
@POST
@Consumes("application/json")
public String send(Information information) {
informationService.remove(information);
return "Receive : " + information;
}
}
Now Start JSF :
@Named
@ViewScoped
public class InformationBean implements Serializable {
private Information info ;
private List<Information> informationList ;
@EJB
private InformationService informationService ;
@PostConstruct
public void init() {
informationList = informationService.findAll();
info = new Information() ;
}
public void deleteInformation() {
informationService.remove(info);
}
public Information getInfo() {
return info;
}
public void setInfo(Information info) {
this.info = info;
}
public List<Information> getInformationList() {
return informationList;
}
public void setInformationList(List<Information> informationList) {
this.informationList = informationList;
}
}
and xhtml :
<h:body>
<p:dataTable value="#{informationBean.informationList}" var="info" id="infoTable">
<p:column rowHeader="name">
<h:outputText value="#{info.name}"/>
</p:column>
<p:column rowHeader="family">
<h:outputText value="#{info.family}"/>
</p:column>
<p:column rowHeader="action">
<h:form>
<p:commandButton value="Delete" action="#{informationBean.deleteInformation}">
<f:setPropertyActionListener value="#{info}" target="#{informationBean.info}"/>
</p:commandButton>
</h:form>
</p:column>
</p:dataTable>
<hr/>
<f:websocket channel="infoChannel">
<p:ajax event="deleteInfo" update="infoTable"/>
</f:websocket>
</h:body>
I already thought , PushContext must implemented on JSF beans , Now I understand can implement that in service or business logic layer .
Now you can remove information from JaxRs (Rest API) and record removed from p:dataTable
without refresh page .
Note : this example use @ViewScoped