0

I have issue with PrimeFaces 6.0 push, I have searched all questions here and no answer helped me. I'm building a internal email system and I'm trying to notify user if he has new email inserted in the DB based on back end event (observer pattern), here is my code:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>Test Foo Email system</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- primeFaces push declaration -->
  <servlet>
    <servlet-name>Push Servlet</servlet-name>
    <servlet-class>org.primefaces.push.PushServlet</servlet-class>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>Push Servlet</servlet-name>
    <url-pattern>/primepush/ *</url-pattern>
</servlet-mapping>
<!-- Ends here-->
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/ *</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

My foo.xhtml page code sample

<script type="text/javascript">
    function handleMessage(facesmessage) {
        facesmessage.severity = 'info';

        PF('growl').show([facesmessage]);
    }
</script>
<div class="New_Message_container ">
            <!-- primefaces Push component -->
             <p:growl widgetVar="growl" showDetail="true" />
            <p:socket onMessage="handleMessage" channel="/notify" />
            </div>

Controller class:

private final static String CHANNEL = "/notify";
.
.
.
private void notifyView(NewEmailEvent evt) {
    String CurrentUser = this.currentUser.getEmailID();
    String EventUserId = evt.getUserID();
    if (CurrentUser.equalsIgnoreCase(EventUserId)) {
    EventBus eventBus = EventBusFactory.getDefault().eventBus();
    eventBus.publish(CHANNEL, 
    new FacesMessage("New Mail", "You have new Mail"));
    }

}

NotifyResource class

import javax.faces.application.FacesMessage;
import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.impl.JSONEncoder;

@PushEndpoint("/notify")
  public class NotifyResource {

@OnMessage(encoders = {JSONEncoder.class})
  public FacesMessage onMessage(FacesMessage message) {
        return message;
   }

}

Included required libraries which i found was required for this code, I have downloaded these jars from the web which are used in my sample.

atmosphere-annotations-2.4.9.jar
atmosphere-compat-jbossweb-2.0.1.jar
atmosphere-compat-tomcat-2.0.1.jar
atmosphere-compat-tomcat7-2.0.1.jar
atmosphere-runtime-2.4.9.jar
primefaces-6.0.jar
slf4j-api-1.7.22.jar
slf4j-simple-1.7.22.jar

I have tired to debug my code and I noticed that the event bus it NULL when I try to use it (eventBus.publish()) so I got java.lang.NullPointerException.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Omar Adel
  • 3
  • 3
  • Several 'duplicates' in Stackoverflow. Please try to search for them. The 'related' column on the right already shows some of them – Kukeltje Jan 10 '17 at 13:04
  • Hi kukeltje, thanks for your comment, unfortunately I have tried all posted answer however I couldn't figure out where the issue i'm searching for this issue for. A week now, also i'm kind of a beginner in jsf. – Omar Adel Jan 10 '17 at 15:01
  • What is the server runtime? Tomcat? Tried adding cdi as a library – Kukeltje Jan 10 '17 at 20:27

1 Answers1

0
<servlet-mapping>
    <servlet-name>Push Servlet</servlet-name>
    <url-pattern>/primepush/ *</url-pattern>
</servlet-mapping>

There is a space between /primepush/ and *, try removing it.

FkJ
  • 1,609
  • 1
  • 19
  • 29