jfinal-QuartzPlugin后台任务调度

配置方法:

1. 在jfinal的Start.java中插件位置配置QuartzPlugin

/**
     * 配置插件
     */
    public void configPlugin(Plugins me) 
    {
        // 配置druid数据库连接池插件
        String driver = getProperty("driverClass");
        DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"), driver);
        
        //集成druid的插件
        druidPlugin.addFilter(new StatFilter());
        druidPlugin.setInitialSize(3).setMaxActive(10);        
        WallFilter wall = new WallFilter();
        wall.setDbType("mysql");
        druidPlugin.addFilter(wall);
        me.add(druidPlugin);
        
        // 配置ActiveRecord插件        
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        //显示执行的sql语句
        //arp.setShowSql(true);        
        me.add(arp);
//    
//        // 映射表到模型
//        arp.addMapping("t_sys_loginperson", Loginperson.class);        
//        arp.addMapping("t_sys_login", Login.class);
//        arp.addMapping("t_sys_organization", Organization.class);
//        arp.addMapping("t_sys_person", Person.class);
//        
//        arp.addMapping("t_sys_role", Role.class);
//        arp.addMapping("t_sys_role_menu", RoleMenu.class);
        
        QuartzPlugin quartzPlugin =  new QuartzPlugin("job.properties");
        me.add(quartzPlugin);
    }    

2. src目录下增加配置文件"job.properties",时间的格式请参照这里

a.job=com.dsideal.Util.JobA
a.cron=*/3 * * * * ?
a.enable=true

3. 需要引入的jar包包括 quartz-1.8.6.jar jfinal-ext-3.0.1.jar google-collect-1.0.jar,下载地址

4. JobA的代码如下,这里我加入了自己的业务逻辑,大家只需要把execute()里的方法改一下就行

package com.dsideal.Util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.jfinal.plugin.memcached.MemcachedKit;




public class JobA implements Job {
    private static long dfRxBytes = 0;
    private static long dfTxBytes = 0;
    public static long dfRxBytesMb = 0; //每秒接收的总字节数
    public static long dfTxBytesMb = 0; //每秒发送的总字节数
    static int callTime = 0;


    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        testNetIfList();
        callTime++;
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " JobA works,callTime is: " + callTime);
    }

    public void testNetIfList()  {  
        try {
            Sigar sigar = new Sigar();  
            String ifNames[] = sigar.getNetInterfaceList();  
            for (int i = 0; i < ifNames.length; i++) {  
                String name = ifNames[i];  
                if(name.equals("eth6")) {
                    NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);  
//                    print("
name(网络设备名) = " + name);// 网络设备名  
//                    print("Address(IP地址) = " + ifconfig.getAddress());// IP地址  
//                    print("Netmask(子网掩码) = " + ifconfig.getNetmask());// 子网掩码  
                    if ((ifconfig.getFlags() & 1L) <= 0L) {  
//                        print("!IFF_UP...skipping getNetInterfaceStat");  
                        continue;  
                    }  
                    try {  
                        NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);  
//                        print("RxPackets(接收的总包裹数) = " + ifstat.getRxPackets());// 接收的总包裹数  
//                        print("TxPackets(发送的总包裹数) = " + ifstat.getTxPackets());// 发送的总包裹数 
                        if (dfRxBytes == 0) {
                            dfRxBytes = ifstat.getRxBytes();
                        }
//                        print("RxBytes(接收到的总字节数) = " + ifstat.getRxBytes());// 接收到的总字节数  
//                        print("-------------------------------------------");
//                        print("RxBytes(每秒接收到的总字节数) = " + (ifstat.getRxBytes() - dfRxBytes) + "B");// 接收到的总字节数  
//                        print("RxBytes(每秒接收到的总字节数) = " + ((ifstat.getRxBytes() - dfRxBytes) /1024) + "KB");// 接收到的总字节数  
                        dfRxBytesMb = (ifstat.getRxBytes() - dfRxBytes) /1024 /1024;
//                        print("RxBytes(每秒接收到的总字节数) = " + dfRxBytesMb + "MB");// 接收到的总字节数  
//                        print("-------------------------------------------");
                        dfRxBytes = ifstat.getRxBytes();
//                        print("TxBytes(发送的总字节数) = " + ifstat.getTxBytes());// 发送的总字节数  
//                        print("-------------------------------------------");
//                        print("TxBytes(每秒发送的总字节数) = " + (ifstat.getTxBytes() - dfTxBytes) + "B");// 发送的总字节数    
//                        print("TxBytes(每秒发送的总字节数) = " + ((ifstat.getTxBytes() - dfTxBytes) /1024) + "KB");// 发送的总字节数    
                        if (dfTxBytes != 0) {
                             dfTxBytesMb = (ifstat.getTxBytes() - dfTxBytes) /1024 /1024 ;
                             MemcachedKit.set("dfTxBytesMb", 0, dfTxBytesMb);
                             System.out.println("dfTxBytesMb="+MemcachedKit.get("dfTxBytesMb"));
                        }
//                        print("TxBytes(每秒发送的总字节数) = " + dfTxBytesMb + "MB");// 发送的总字节数   
//                        print("-------------------------------------------");
//                        if (((ifstat.getTxBytes() - dfTxBytes) /1024 /1024) <= 8) {
//                            this.printA();
//                        } else {
//                            this.printB();
//                        }
                        dfTxBytes = ifstat.getTxBytes();
//                        print("RxErrors(接收到的错误包数) = " + ifstat.getRxErrors());// 接收到的错误包数  
//                        print("TxErrors(发送数据包时的错误数) = " + ifstat.getTxErrors());// 发送数据包时的错误数  
//                        print("RxDropped(接收时丢弃的包数) = " + ifstat.getRxDropped());// 接收时丢弃的包数  
//                        print("TxDropped(发送时丢弃的包数) = " + ifstat.getTxDropped());// 发送时丢弃的包数  
                    } catch (SigarNotImplementedException e) {  
                         print(e.getMessage());  
                    } catch (SigarException e) {  
                        print(e.getMessage());  
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }  
        } catch (Exception e) {
            // TODO: handle exception
        }
        
    }
    
    void print(String msg) {  
        System.out.println(msg);  
    }

}
原文地址:https://www.cnblogs.com/cczhoufeng/p/3520182.html