Quartz+DWR实现定时从后台向前台推送消息
具体的应用场景:前台页面需要定时刷新数据,以形成动态的数据变化或者图表变化
具体的实现过程:Quartz一直读取后台的数据,并可以产生一些数据缓存;
DWR推送内容设置在Quartz读取数据的地方(任务类的execute方法中);
当页面发送一个请求,生成一个scriptsession,传到后台;
根据读取scriptsession是否为空作判定,不为空则由DWR向前台推送。
spring配置文件
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="durability" value="true" />
<property name="jobClass" value="com.sbr.fort.quartz.state2.AllsMessageQuartzJobImpl">property>
bean>
<bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<property name="cronExpression" value="0/5 * * * * ?"/>
bean>
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="cornTrigger"/>
property>
bean>
beans>
调用Schedular的调度类
public class AllsMessageQuartz{
public static Collection
public static Collection
return sessions2;
}
public static void setSessions2(Collection
AllsMessageQuartz.sessions2 = sessions2;
}
public void addMessage() throws SchedulerException {
Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
// 实现match方法,条件为真为筛选出来的session(在所有相关的访问session中找到匹配接收方的,设置为ture则锁定所有的请求页面session)
public boolean match(ScriptSession session) {
return true;
}
}, new Runnable() {
public void run() {
sessions2 = Browser.getTargetSessions();
}
});
}
@SuppressWarnings("resource")
public static void init() {
//静态调用
new ClassPathXmlApplicationContext("spring/applicationContext-quartz.xml");
}
实现Job接口的任务类
public class AllsMessageQuartzJobImpl implements Job {
static Listlists = new ArrayList
@Override
Public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("执行JOB"+context.getFireTime());
//SystemState是自定义的类
需要传的内容自定义,这里只是示例,
SystemState ssc=new SystemState();
Object obj=ssc.getallsMessage();
if(lists.size()>=10){
lists.remove(0);
}
lists.add(obj);
Collection
if(ss!=null&&ss.size()>0){
ScriptBuffer script = new ScriptBuffer();
script.appendCall("receiveMessages",lists);
// 向所有符合条件的页面推送消息
for (ScriptSession scriptSession : ss) {
scriptSession.addScript(script);
}
}
}
}
Spring-mvc.xml配置
<bean id="allsMessageQuartz" class="com.sbr.fort.quartz.state2.AllsMessageQuartz">
bean>
Dwr.xml配置
xml version="1.0" encoding="UTF-8"?>
DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="spring" javascript="allsMessageQuartz">
<param name="beanName" value="allsMessageQuartz"/>
<param name="location" value="spring/mvc/spring-mvc.xml" />
create>
allow>
dwr>
Web.xml配置
<servlet>
<servlet-name>dwr-invokerservlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
servlet-class>
<init-param>
<param-name>crossDomainSessionSecurityparam-name>
<param-value>falseparam-value>
init-param>
<init-param>
<param-name>allowScriptTagRemotingparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>classesparam-name>
<param-value>java.lang.Objectparam-value>
init-param>
<init-param>
<param-name>activeReverseAjaxEnabledparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartupparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>maxWaitAfterWriteparam-name>
<param-value>3000param-value>
init-param>
<init-param>
<param-name>debugparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>logLevelparam-name>
<param-value>WARNparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>dwr-invokerservlet-name>
<url-pattern>/dwr/*url-pattern>
servlet-mapping>