我正在尝试使用功能来制作简单的JSF示例,以更新页面的内容。不幸的是,它只有在第一次部署或重新运行服务器后才能工作。
如果我尝试重新部署应用程序,则所有推送请求都将被忽略,并且开发人员工具的网页和网络页面上没有更新,我会看到很多重复的请求,这些请求的类型为'websocket' em>,代码为 101交换协议。
此外,如果我试图停止它,我会在服务器日志中看到此错误
11:59:31,444 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 100) WELD-000019: Error destroying an instance com.sun.faces.push.WebsocketChannelManager$ViewScope@ff2ea8c of Managed Bean
[class com.sun.faces.push.WebsocketChannelManager$ViewScope] with qualifiers [@Any @Default]
由JSF创建的websocket很可能没有被正确销毁,但我不知道为什么发生。
我的命名bean代码
@Named
@ApplicationScoped
public class Bean implements Serializable {
private static Logger logger = LoggerFactory.getLogger(Bean.class);
private List<Message> notifications;
@Inject
@Push
private PushContext viewPush;
@PostConstruct
public void load() {
notifications = new ArrayList<>();
}
public List<Message> getNotifications() {
logger.info("Get notifications {}", notifications.size());
return notifications;
}
public void sendPush() {
notifications.add(new Message("!!!"));
viewPush.send("newMsg");
}
public static class Message {
private String text;
public Message(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
}
index.xhtml
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
>
<h:head>
<title>Primefaces product List</title>
</h:head>
<h:body>
<h:form>
<h:dataTable id="notifications" value="#{bean.notifications}" var="notification">
<h:column>
<f:facet name="header">Text</f:facet>
#{notification.text}
</h:column>
</h:dataTable>
<h:commandButton action="#{bean.sendPush}" value="Send push">
<f:ajax/>
</h:commandButton>
<f:websocket channel="viewPush">
<f:ajax event="newMsg" render="notifications"/>
</f:websocket>
</h:form>
</h:body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>faces-servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>faces-servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.ENABLE_WEBSOCKET_ENDPOINT</param-name>
<param-value>true</param-value>
</context-param>
</web-app>
我在pom.xml中具有这种依赖性
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-jsf</artifactId>
<version>15.0.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0.SP1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
<scope>provided</scope>
</dependency>