Nicky
Nicky

Reputation: 83

Primefaces push - not reaching endpoint

I have problems with my maven web application implementing primefaces push. I can't figure out why, but I'm not reaching my endpoint.

I'm running on,

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany</groupId>
<artifactId>feestnet_v3</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>feestnet_v3</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.atmosphere</groupId>
        <artifactId>atmosphere-runtime</artifactId>
        <version>2.1.3</version>
    </dependency>

    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>all-themes</artifactId>
        <version>1.0.10</version>
    </dependency>

</dependencies>





<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>6.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <url>http://repository.primefaces.org/</url>
        <id>PrimeFaces-maven-lib</id>
        <layout>default</layout>
        <name>Repository for library PrimeFaces-maven-lib</name>
    </repository>
    <repository>
        <id>prime-repo</id>
        <name>Prime Repo</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>

MailObserver.java (pushing the message)

package nv.messaging;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.push.EventBus;
import org.primefaces.push.EventBusFactory;

@ManagedBean
@SessionScoped
public class MailObserver implements Serializable{

    private int toId;
    private int fromId;
    private String title;
    private String message;

    public String inboxText;
    public String htmlMessage;

    public MailObserver(){
        inboxText = "Inbox";
        htmlMessage = "Test message";
    }

    public void pushMessage(ActionEvent event){
        EventBus eventBus = EventBusFactory.getDefault().eventBus();
        eventBus.publish("/message", htmlMessage);
        System.out.println("Message sent");
    }

    public void update() {

    }

    public String getInboxText() {
        return inboxText;
    }

    public void setInboxText(String inboxText) {
        this.inboxText = inboxText;
    }

    public int   getToId() {
        return toId;
    }

    public void setToId(int toId) {
        this.toId = toId;
    }

    public int getFromId() {
        return fromId;
    }

    public void setFromId(int fromId) {
        this.fromId = fromId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getHtmlMessage() {
        return htmlMessage;
    }

    public void setHtmlMessage(String htmlMessage) {
        this.htmlMessage = htmlMessage;
    }


}

MailEndpoint.java

import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.impl.JSONEncoder;
@PushEndpoint(value = "/message")
public class MailEndpoint {

    @OnMessage(encoders = {JSONEncoder.class})
    public String onMessage(String message) {
        System.out.println("Mail endpoint reached : " + message);
        return message;
    }

}

for completeness layout.xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

    <f:view contentType="text/html">
        <h:head>
            <style>
                .ui-layout-resizer{

                    background-color: black;
                }
            </style>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
                <title>PrimeFaces</title>
            </f:facet>
        </h:head>

        <h:body>
            <f:metadata>
                <f:event type="preRenderView" listener="#{security.securePage()}"/>
                <f:event type="preRenderView" listener="#{security.authorisation('ALL')}"/>
            </f:metadata>

            <p:layout fullPage="true">

                <p:layoutUnit position="north" size="100" resizable="true" closable="true" collapsible="true" gutter="1">
                    Header
                    <p:socket channel="/message" onMessage="handleMessage" ></p:socket>
                </p:layoutUnit>

                <p:layoutUnit position="south" size="100" closable="true" collapsible="true" gutter="1">
                    Footer
                </p:layoutUnit>

                <p:layoutUnit position="west" size="auto" header="Left" collapsible="true" gutter="1">
                    <p:menu>
                        <p:submenu label="Resources">
                            <p:menuitem value="Message" url="/secure/messages/message.xhtml" />
                            <p:menuitem value="Documentation" url="http://www.primefaces.org/documentation.html" />
                            <p:menuitem value="Forum" url="http://forum.primefaces.org/" />
                            <p:menuitem value="Themes" url="http://www.primefaces.org/themes.html" />

                        </p:submenu>

                    </p:menu>
                </p:layoutUnit>

                <p:layoutUnit position="center">
                    <ui:insert name="content">Put default content here, if any.</ui:insert>
                </p:layoutUnit>

            </p:layout>
            <script type="text/javascript" >
                function handleMessage(data) {
                    console.log(data);
                }
            </script>
        </h:body>

    </f:view>
</html>

I think I should see a message in the output from MailEndpoint but, I only get the message from MailObserver.pushMessage. Any ideas on what is going on?

Upvotes: 0

Views: 1527

Answers (1)

Nicky
Nicky

Reputation: 83

I found the problem. I changed the wrong web.xml. To make it work, you need to add this to web.xml

<servlet>
    <servlet-name>Push Servlet</servlet-name>
    <servlet-class>org.primefaces.push.PushServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>Push Servlet</servlet-name>
    <url-pattern>/primepush/*</url-pattern>
</servlet-mapping>

Upvotes: 1

Related Questions