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
                                pushlet推送引入SSM项目_第1张图片
项目结构  : 

                                    pushlet推送引入SSM项目_第2张图片

项目中的 ajax-pushlet-client.js  / pushlet.properties   /sources.properties 都是要从源码中拷贝进来的

接下来就是配置web.xml :

   
        pushlet  
        nl.justobjects.pushlet.servlet.Pushlet  
        1  
   
  
  
      
        pushlet  
        /pushlet.srv  

       


下面就是创建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 getDangerPeople(){
String cardId = "569874123963852";
RecogcardInfoService recogcardInfoService = SpringBeanFactory.getBean("recogcardInfoServiceImpl");
List 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 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"%>




Insert title here
 


hello world



   


 
到此,基本一切正常了启动程序:访问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,或你理解的更加深刻欢迎指出,共同学习。








                                           

你可能感兴趣的:(pushlet)