由于本人在实际项目开发中有涉及到流程跑批,每个节点顺序点击执行后必须刷新页面才能看到状态是否变更。从客户体验上来说,这点是非常差的,于是想到dwr和websocket实现实时消息推送浏览器,前者是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA方法,就像它就在浏览器里一样,但存在一定的java代码泄露风险,而后者是一种网络双工通讯TCP协议,长连接,需要客户端发起连接。两者实现起来都是相对比较简单的,下面来简单实现下dwr!
实现步骤如下:
1、maven引jar
org.directwebremoting dwr 3.0.2-RELEASE
2、dwr无xml配置部分
package com.paic.commcc.support.dwr;import org.directwebremoting.Container;import org.directwebremoting.create.NewCreator;import org.directwebremoting.extend.Configurator;import org.directwebremoting.extend.CreatorManager;/** * @author * @Package com.paic.commcc.support.dwr * @Description: dwr的xml配置 * @date 2018/12/11 19:19 */public class DwrXml implements Configurator { @Override public void configure(Container container) { CreatorManager creatorManager = container.getBean(CreatorManager.class); NewCreator creator = new NewCreator(); creator.setClass("com.paic.commcc.support.dwr.DwrPush"); creator.setJavascript("DwrPush"); creatorManager.addCreator(creator); }}
3、dwr核心配置,注意标色代码部分是第2点的配置类路径
package com.paic.commcc.support.dwr;import org.directwebremoting.servlet.DwrListener;import org.directwebremoting.servlet.DwrServlet;import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.HashMap;import java.util.Map;/** * @author * @Package com.paic.commcc.support.dwr * @Description: 远程服务端ajax * @date 2018/12/11 19:17 */@Configurationpublic class DwrConfig { @Bean public ServletRegistrationBean dwr() { ServletRegistrationBean servlet = new ServletRegistrationBean(new DwrServlet(), "/dwr/*"); MapinitParam = new HashMap<>(); initParam.put("crossDomainSessionSecurity", "false"); initParam.put("allowScriptTagRemoting", "true"); initParam.put("classes", "java.lang.Object"); initParam.put("activeReverseAjaxEnabled", "true"); initParam.put("initApplicationScopeCreatorsAtStartup", "true"); initParam.put("maxWaitAfterWrite", "60000"); initParam.put("debug", "true"); initParam.put("logLevel", "WARN"); //自定义配置,org.directwebremoting.impl.StartupUtil#configureFromInitParams name.equals("customConfigurator") //DwrServlet#init 初始化this.container initParam.put("customConfigurator", "com.paic.commcc.support.dwr.DwrXml"); servlet.setInitParameters(initParam); return servlet; } @Bean public ServletListenerRegistrationBean dwrListener() { return new ServletListenerRegistrationBean(new DwrListener()); }}
4、dwr推送消息类
package com.paic.commcc.support.dwr;import org.directwebremoting.Browser;import org.directwebremoting.ScriptSession;import org.directwebremoting.ScriptSessionFilter;import org.directwebremoting.ScriptSessions;/** * @author * @Package com.paic.commcc.support.dwr * @Description: dwr推送消息类 * @date 2018/12/11 19:23 */public class DwrPush { public static void init(String msg) { DwrPush.sendMsgToPage("/dwr.html", msg); } /** * @Description: 推送消息至页面,默认onDwrData函数 * @Param: [html, msg] * @return: void * @Author: * @Date: 2018/12/12 */ public static void sendMsgToPage(final String html, final String msg) { sendMsgToPage(html, "onDwrData", msg); } public static void sendMsgToPage(final String html, final String function, final String msg) { Browser.withPage(html, new Runnable() { public void run() { ScriptSessions.addFunctionCall(function, msg); } }); } public static void sendMsgWithAllSessions(final String msg) { Browser.withAllSessions(new Runnable() { public void run() { ScriptSessions.addFunctionCall("onDwrData", msg); } }); } public static void sendMsgWithFilter(final String msg) { Browser.withAllSessionsFiltered(new ScriptSessionFilter() { @Override public boolean match(ScriptSession scriptSession) { // 可以在这里做一些session过滤 return true; } }, new Runnable() { public void run() { ScriptSessions.addFunctionCall("onDwrData", msg); } }); }}
5、dwr.html页面放static目录下,3个js,其中DwrPush.js的名字跟推送消息类一致,其他两个为框架本身的,必须要引用
dwr html dwr html
然后就可以实现后台有节点状态变更时,调用dwr消息推送类刷新浏览器节点状态了,怎么去刷新就看具体实现。
希望对你有所帮助,谢谢~