-2

I am devoting an instant messaging application using multithreding and JSF 2.2 sockets, richafes 4.5, Tomcat 7.

When client1 sends the message to client2, the latter updates the bean property but the client2 component changes its value only when I update the page.

Is there a method to update a component regularly after the facescontext is released?

thread Client :

    public class ThreadWait extends Thread {
            public void run() {

                String msg;

                try {
                    while ((msg = input.readLine()) != null) {

                        editorBean.setMsg(msg);

                        /*** KO because the FacesContext already released *******/
                        UIViewRoot viewRoot = viewHandler.createView(context, context.getViewRoot().getViewId());
                        context.setViewRoot(viewRoot);
                        context.renderResponse(); 
                        /***************/    
                    }

                } catch (IOException e) {
                    System.err.println("IOException" + e);
                }
            }
        }

Bean Message.java :

public void send() {

        FacesContext context = FacesContext.getCurrentInstance();
        Application application = context.getApplication();
        ViewHandler viewHandler = application.getViewHandler();
        UIViewRoot viewRoot = viewHandler.createView(context, context.getViewRoot().getViewId());

        if (connected) {
            client.sendMessage(treeBean.getCurrentSelection() + "{MESSAGE}" + editor.getMsg());
            return;
        }

        client = new Client(editor, signIn, context, application, viewHandler, viewRoot);
        // test if we can start the Client
        if (!client.runConnexion(treeBean.getCurrentSelection() + "{MESSAGE}" + editor.getMsg()))
            return;

        connected = true;

        return;
}

Bean editor:

@ManagedBean
@SessionScoped
public class EditorBean implements Serializable {
    private static final long serialVersionUID = 1L;


    private String msg;

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @param msg
     *            the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public EditorBean() {

    }

    public EditorBean(String msg) {
        super();
        this.msg = msg;
    }

}

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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:rich="http://richf`enter code here`aces.org/rich" xmlns:a4j="http://richfaces.org/a4j"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
    <title>Welcome</title>
</h:head>
<h:body>


    <h:form>
        <table>
            <tr>
                <td>
                    <div>
                        <rich:editor id="editor" toolbar="full" value="#{editorBean.msg}" style="margin-bottom: 1em">
                        </rich:editor>

                        <h:commandButton value="Send" action="#{message.send}" type="submit"></h:commandButton>

                    </div>
                </td>
            </tr>
        </table>
    </h:form>

</h:body>
</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Possible duplicate of [How can server push asynchronous changes to a HTML page created by JSF?](http://stackoverflow.com/questions/3787514/how-can-server-push-asynchronous-changes-to-a-html-page-created-by-jsf) – BalusC Dec 04 '16 at 17:57
  • Thank's for your help – m.erick Dec 05 '16 at 12:33

1 Answers1

0

solution using a4j:poll :

<a4j:poll id="poll" interval="2000" enabled="#{pollBean.pollEnabled}" render="poll,grid" />

<rich:panel width="80%" id="grid">
    <f:facet name="header">Messages</f:facet>
    <h:outputText id="serverDate" escape="false" value="#{editorBean.msgReveive}" />
</rich:panel>