配置方法:html
1. 在jfinal的Start.java中插件位置配置QuartzPluginjava
/** * 配置插件 */ 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",時間的格式請參照這裏mysql
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,下載地址sql
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("\nname(網絡設備名) = " + 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); } }