使用pushlet实现消息服务端推送至客户端

1、基础概念:Pushlet是一种comet实现:在Servlet机制下,数据从server端的Java对象直接推送(push)到(动态)HTML页面,而无需任何Java applet或者插件的帮助。

2、一个简单的demo:

1)导入所需要的jar包:pushlet.jar

2)将配置文件pushlet.properties、sources.properties导入到src目录下

pushlet.properties:

config.version=1.0.2
controller.class=nl.justobjects.pushlet.core.Controller
dispatcher.class=nl.justobjects.pushlet.core.Dispatcher
logger.class=nl.justobjects.pushlet.util.Log4jLogger
sessionmanager.class=nl.justobjects.pushlet.core.SessionManager
session.class=nl.justobjects.pushlet.core.Session
subscriber.class=nl.justobjects.pushlet.core.Subscriber
subscription.class=nl.justobjects.pushlet.core.Subscription
log.level=4
sources.activate=true
session.id.generation=randomstring
session.timeout.mins=5
queue.size=24
queue.read.timeout.millis=20000
queue.write.timeout.millis=20
listen.force.pull.all=false
listen.force.pull.agents=safari
pull.refresh.timeout.millis=45000
pull.refresh.wait.min.millis=2000
pull.refresh.wait.max.millis=6000
poll.refresh.timeout.millis=60000
poll.refresh.wait.min.millis=6000
poll.refresh.wait.max.millis=10000


source.properties:

source6=cn.ffcs.pushmessage.plushlet.MessagePlushlet$Plushlet

3)修改web.xml文件如下(url-pattern暂时设置为/pushlet.srv

Xml代码   收藏代码
  1. <servlet-name>pushletservlet-name>  
  2.     <servlet-class>nl.justobjects.pushlet.servlet.Pushletservlet-class>  
  3.     <load-on-startup>1load-on-startup>  
  4. servlet>  
  5. <servlet-mapping>  
  6.     <servlet-name>pushletservlet-name>  
  7.     <url-pattern>/pushlet.srvurl-pattern>  
  8. servlet-mapping>  

   4)编写一个java类如下

Java代码  
 
package cn.ffcs.pushmessage.plushlet;

import java.io.Serializable;

import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;
import nl.justobjects.pushlet.core.Session;
import nl.justobjects.pushlet.core.SessionManager;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import cn.ffcs.pushmessage.domain.PushMessage;
import cn.ffcs.pushmessage.service.api.IPushMessageService;

@SuppressWarnings("serial")
public class MessagePlushlet implements Serializable, BeanFactoryAware{
	
	private IPushMessageService pushMessageService;
	
	public static class Plushlet extends EventPullSource{
		
		/**
		 * 设置休眠时间
		 */
		@Override
		protected long getSleepTime() {
			return 4000;
		}

		/**
	     * 创建事件
	     * 
	     * 业务部分写在pullEvent()方法中,这个方法会被定时调用。
	     */
		@Override
		protected Event pullEvent() {
			Event event = Event.createDataEvent("/message/plushlet");
			IPushMessageService pushMessageService = (IPushMessageService) getBean("cn.ffcs.pushmessage.service.impl.PushMessageServiceImpl");
			PushMessage pushMessage = pushMessageService.getById(1);
			System.out.println(event.getField("p_id") + "==================");
			System.out.println(event.getField("userId") + "==================");
			System.out.println(event.getField("p_format") + "==================");
			System.out.println(event.getSubject());
			Session[] sessions = SessionManager.getInstance().getSessions(); 
			//sessions[0].getEvent().getField("userId");
			System.out.println(sessions[0].getEvent().getField("userId"));
			event.setField("id", "1");
			return event;
		}
		
	}

	public IPushMessageService getPushMessageService() {
		return pushMessageService;
	}

	public void setPushMessageService(IPushMessageService pushMessageService) {
		this.pushMessageService = pushMessageService;
	}

	private static BeanFactory beanFactory;  
    
    public static Object getBean(String beanName) {  
         return beanFactory.getBean(beanName);  
    }  
      
    public static  T getBean(String beanName, Class clazs) {  
         return clazs.cast(getBean(beanName));  
    }  
      
    @SuppressWarnings("static-access")
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {  
    	this.beanFactory = beanFactory;  
    }

	
	
}

   还有就是注意Event event = Event.createDataEvent("/message/plushlet");// 事件标识

 

5)编写jsp文件如下<%@ page language="java" contentType="text/html; charset=UTF-8"

Java代码   收藏代码
  1.     






  1.  

   PL.joinListen('/message/plushlet');//事件标识 在数据源中引用,注意跟上面的红色部分保持一致。

   调用getRingData()为弹窗显示后的声音提醒。





6)下面说一下web.xml中的配置问题,查看ajax-pushlet-client.js源码会发现有下面这段代码_getWebRoot: function() {

Js代码   收藏代码
  1.     /** Return directory of this relative to document URL. */  
  2.     if (PL.webRoot != null) {  
  3.         return PL.webRoot;  
  4.     }  
  5.     //derive the baseDir value by looking for the script tag that loaded this file  
  6.     var head = document.getElementsByTagName('head')[0];  
  7.     var nodes = head.childNodes;  
  8.     for (var i = 0; i < nodes.length; ++i) {  
  9.         var src = nodes.item(i).src;  
  10.         if (src) {  
  11.             var index = src.indexOf("lib/ajax-pushlet-client.js");  
  12.             if (index >= 0) {  
  13.                 PL.webRoot = src.substring(0, index);  
  14.                 break;  
  15.             }  
  16.         }  
  17.     }  
  18.     return PL.webRoot;  
  19. },  
 注意var index = src.indexOf("lib/ajax-pushlet-client.js");这句话 这个方法就是取webRoot跟路径的,所以的设置跟这里有关系,还有就是跟你使用ajax-pushlet-client.js的jsp页面有关系,我上面的jsp页面中src="js/ajax-pushlet-client.js"是这样导入js包的,所以我把ajax-pushlet-client.js中的代码改为 var index = src.indexOf("js/ajax-pushlet-client.js"); 取根路径了。

你可能感兴趣的:(使用pushlet实现消息服务端推送至客户端)