index.xhtml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui" xmlns:ui="http://java.sun.com/jsf/facelets"
xml:lang="en" lang="en">
<h:head>
<title>GG Well Trade</title>
<script type="text/javascript">
function add(){
}
</script>
</h:head>
<h:body>
<h:form>
<p:socket channel="/notify" onMessage="add"/>
<p:commandButton value="Start" action="#{bean.start}" update="@form"/>
<ui:repeat value="#{bean.items}" var="item" id="content">
<p:outputLabel for="foo" value="#{item.label}" />
<p:inputText id="foo" value="#{item.value}" />
<p:commandButton value="Remove" action="#{bean.remove(item)}" update="@form" />
<br/>
</ui:repeat>
<p:commandButton value="Add" action="#{bean.add}" update="@form" />
</h:form>
</h:body>
</html>
Code: Select all
public class Item {
private String label;
private String value;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Code: Select all
@ManagedBean
@ViewScoped
public class Bean implements Serializable{
private List<Item> items;
@PostConstruct
public void init() {
items = new ArrayList<Item>();
}
public void start() {
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(()->add(),0,3,TimeUnit.SECONDS);
}
public void add() {
Item item = new Item();
item.setLabel("label" + items.size());
items.add(item);
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/notify", item);
}
public void remove(Item item) {
items.remove(item);
}
public List<Item> getItems() {
return items;
}
}
Code: Select all
@PushEndpoint(value = "/notify")
public class BeanPush {
@OnMessage(encoders = {JSONEncoder.class})
public Item onMessage(Item item){
return item;
}
}