JAVA-向页面定时推送信息

使用Quartz触发器加dwr推送技术(服务器推送技术,Server push)完成,直接用实例说明:

1、先看配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <!-- 设定自动执行任务的方法体 -->

   <!-- warnEventPushScanAction是Spring配置的java类名,scanWarnEvent为类里面的方法 -->

    <bean id="warnEventPushScanJob"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="
warnEventPushScanAction" />
        <property name="targetMethod" value="
scanWarnEvent" />
        <property name="concurrent" value="false" />
<!--将并发设置,false为执行完本次任务再执行下一次的任务-->
    </bean>
    <!-- 设定自动执行任务的时间 每到一个时间点运行 -->
    <bean id="warnEventPushScanTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="
warnEventPushScanJob" />
        <property name="cronExpression" value="
0/5 * * * * ?" />
    </bean>

    
     <!-- 进行动态的任务注册和调度 -->
     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
        <property name="triggers">
            <list>
                <ref bean="
warnEventPushScanTrigger" />
            </list>
        </property>
    </bean> 
</beans>

2、看进入的方法及数据推送

public class WarnEventPushScanAction extends QueryManageAction { 

 private AlarmVideoCirculateAction alarmVideoCirculateAction;  

 public void scanWarnEvent() {

/*这里是你的逻辑*/

            alarmVideoCirculateAction.callDWRPageJSFunctionForIndexPage(list);

 }

}

public class AlarmVideoCirculateAction extends WebPushAction{
    @SuppressWarnings("deprecation")
    public static int callDWRPageJSFunctionForIndexPage(List smsJsons) {

//参数1是接收数据的页面,参数2是页面的js方法,参数三是传过去的数据
        callDWRPageJSFunction("monitorVideo/HCVSM/alarmVideoCirculateH.jsp", "setAlarmVideoData", smsJsons);
        return 0;
    }
    /**
     * pushPage为推送的页面
     * callBackMethod为js方法
     * @param pushPage
     * @param callBackMethod
     * @param smsJsons
     * @return
     */
    @SuppressWarnings("deprecation")
    public static int callDWRPageJSFunction(String pushPage, String callBackMethod, List smsJsons) {
        int result = -1;
        if (contex != null) {
            //得到要推送到 的页面 dwr3为项目名称 , 一定要加上。
            if (projectName == null || projectName.equals("")) {
                projectName = getProjectName();
            }
            String page = projectName + pushPage;
            Collection<ScriptSession> sessions = contex.getScriptSessionsByPage(page);    
            Iterator it = sessions.iterator(); 
            while (it.hasNext()) {
                ScriptSession session = (ScriptSession) it.next();
                Util util = new Util(session);
             
   //回调js方法,callBackMethod:方法名,callBackParameter:回调参数
                util.addFunctionCall(callBackMethod, smsJsons);
            }
            result = 1;
        }
        return result;
    }    

    }

猜你喜欢

转载自blog.csdn.net/qq_38644907/article/details/88287052
今日推荐