pushlet推送引入SSM項目

第一次web項目使用pushlet的坑

簡單說一下項目背景(由於是政府項目不方便透露....請諒解)哈哈哈。。。。。。。。。

還是說一下pushlet的需求場景:

              當後臺需要主動推送給前臺信息時,可以採用。但現在推送業務的技術比較多可以選擇其他的技術,

               我這是根據公司上個項目的技術選擇的pushlet,好啦,言歸正傳;

1,下載jar包打包到倉庫   :   https://sourceforge.net/projects/pushlets/
這是源碼下載後解壓可在項目的lib目錄下看到 pushlet.jar ,複製出來隨便丟到一個文件夾下,
我這裏就丟到 : d 盤根目錄下

然後執行dos命令: C:\Users\Administrator>mvn install:install-file -Dfile=D:\pushlet.jar -DgroupId=nl.justobjects.pushlet -DartifactId=pushlet -Dversion=2.0.4 -Dpackaging=jar -DgeneratePom=true(打包前提要有maven的環境)

2, 項目中引用pushlet
                                
項目結構  : 

                                    

項目中的 ajax-pushlet-client.js  / pushlet.properties   /sources.properties 都是要從源碼中拷貝進來的

接下來就是配置web.xml :

 <servlet>  
        <servlet-name>pushlet</servlet-name>  
        <servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>pushlet</servlet-name>  
        <url-pattern>/pushlet.srv</url-pattern>  

    </servlet-mapping>   


下面就是創建pushlet對象了不多說看代碼:

public class Pushlet {
//核心代碼
public static class Hello extends EventPullSource{
@Override
protected long getSleepTime() {
return 50000;
}
@Override
protected Event pullEvent() {
Event event =Event.createDataEvent("/pushPeople");
String json = CommUtil.renderDeepJson(getDangerPeople().get(0));
System.out.println("json:"+json);
json = encode(json);
event.setField("mess", json);
return event;
}
}

// 解決中文編碼
public static String encode(String str) {
try {
str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
} catch (Exception e) {
e.printStackTrace();
}
return str;
}

public static List<RecogcardInfo> getDangerPeople(){
String cardId = "569874123963852";
RecogcardInfoService recogcardInfoService = SpringBeanFactory.getBean("recogcardInfoServiceImpl");
List<RecogcardInfo> list = recogcardInfoService.pushPeopleByCardId(cardId);
return list;
}
}

說一下上面代碼中我所踩的坑:

我本來是使用:

@Autowired

private static RecogcardInfoService recogcardInfoService; 注入接口的

但程序一運行就報空指針,接口注入不進來,

後來我直接手動:實例化接口實現類-----------回到new的時代,可惜還是空指針

就這樣卡了我一天多最後不斷網上查資料:

有了一絲明悟:因爲pushlet是在服務端做輪詢,並不經web容器,所以不能自動注入,要手動獲取

方法如下:

public class SpringBeanFactory{ 
    private static ApplicationContext ctx;  
    /** 通過ContextLoaderListener取得ctx */  
    
    public static void initApplicationContext(){  
        ctx=ContextLoaderListener.getCurrentWebApplicationContext();  
    }  
    /** 通過泛型方法取得bean實例 */  
    @SuppressWarnings("unchecked")
public static <T> T getBean(String name){  
        if(ctx==null){  
            initApplicationContext();  
        }  
        return (T) ctx.getBean(name);  
    }  

參考:https://blog.csdn.net/zollty/article/details/8710911

3:頁面接收  

配置:sources.properties 

註釋掉其他的source標籤,配置自己的標籤

source1=com.controller.Pushlet$Hello

關於$ 的使用:如果你不是使用的內部類推送就不要使用 $-------比如,直接創建類實現extends EventPullSource 就不需要這樣寫,可以這樣 source1=com.controller.Pushlet


下面就是頁面去接收pushlet的推送了:我這裏爲了方便測試直接寫在了index.jsp頁面了

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src='ajax-pushlet-client.js'></script> 
</head>
<body>
<h1>hello world</h1>
<div id = "aa"></div>
 <script type="text/javascript">  
            //啓動監聽  
            // PL._init();  
            // PL.setDebug(true);
             PL.joinListen('/pushPeople');  
            function onData(event) {   
               // alert(event.get("mess")); 
                aa.innerText=(event.get("mess"));
             }  

 </script>  
</body>
</html>

 
到此,基本一切正常了啓動程序:訪問localhost:8080/XXX(項目名)/

index頁面接收到如下數據:

{"address":"上海市徐家彙","birthday":"1995-6-6","cardId":"569874123963852","department":"上海市公安局","endDate":"2018-4-18 16:26:10","id":4,"name":"小紅","nation":"漢族","objName":"格爾額","orgnizationAddress":"徐家彙音浪KTV","orgnizationId":"123","personTag":1,"score":"1","sex":"2","startDate":"2018-4-18 16:25:23","time":"2018-4-18 16:25:30"}


第一篇博客可能有很多地方講的不足,我也是這樣採坑過來的,如果你也使用到了pushlet,或你理解的更加深刻歡迎指出,共同學習奮鬥








                                           
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章