0

This non-working implementation tries to start a new ScheduledExecutorService to dynamically push new Items to the client and update the form:

index.xhtml

<?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>
</h:head>
<h:body>
    <h:form>
        <p:socket channel="/notify" onMessage="#{bean.add()}"/>
        <p:commandButton value="Start" action="#{bean.start()}"/>
        <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>

Item.class

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;
    }
}

Bean.class

@ManagedBean
@ViewScoped
@PushEndpoint("/notify")
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);
    }

    @OnMessage(encoders = {JSONEncoder.class})
    public Item onMessage(Item item){
        return item;
    }

    public void remove(Item item) {
        items.remove(item);
    }

    public List<Item> getItems() {
        return items;
    }
}

Pressing the Add button doesn't update the form, Start button instead does but only on clicks. Is there better ways to do this?

EDIT: exception raised:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
    Bean.add(Bean.java:43)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    javax.el.BeanELResolver.invoke(BeanELResolver.java:158)
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
    org.apache.el.parser.AstValue.getValue(AstValue.java:159)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    org.primefaces.component.socket.Socket.getOnMessage(Socket.java:125)
    org.primefaces.component.socket.SocketRenderer.encodeEnd(SocketRenderer.java:55)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.5.8 logs.
TonyRomero
  • 182
  • 1
  • 4
  • 14
  • why do you update the whole form at 'add'? You update the socket to (same is true for the start) – Kukeltje Nov 18 '16 at 14:42
  • is this implementation correct? it is not working properly and what I need to update only? – TonyRomero Nov 18 '16 at 14:55
  • to me it looks like you only need to update the ui:repeat... (or a panel around it, that is what I normally do to be 'safe') – Kukeltje Nov 18 '16 at 15:03
  • there are issues related to publish from eventBus, also it doesn't work properly as the form is updated only using the Start button – TonyRomero Nov 18 '16 at 15:06
  • _"there are issues"_ and _"it doesn't work"_ are both statements that are impossible for us to help with... – Kukeltje Nov 18 '16 at 15:08
  • You're right, I edited my first post with updates and I get a NPE. This should be EventuBus related, it is Primefaces 6.0 and Atmosphere 2.4.8 – TonyRomero Nov 18 '16 at 15:16
  • Eventbus and NPE has multiple Q/A in Stackoverflow. And chameleonizing a question like this is not what is common practice in StackOverflow. The title is totally unrelated now... Please be careful with this next time – Kukeltje Nov 18 '16 at 15:26
  • No, the NPE has come after, my main concern is how to properly update a ui:repeat form using p:push since my implementation doesn't working as expected. – TonyRomero Nov 18 '16 at 15:37
  • Yes I kow it has come after... but then leave it just out of this question and create a new one... And you don't try to update the (content) of the ui repeat anywhere... so what the actual problem is, remains unclear.... – Kukeltje Nov 18 '16 at 16:21
  • What do I have to update then? – TonyRomero Nov 18 '16 at 18:47

0 Answers0