📄 crontab.java
字号:
try { File filez = new File(strFileName); FileInputStream input = new FileInputStream(filez); prop.load(input); input.close(); for (Enumeration e = prop.propertyNames() ; e.hasMoreElements() ;) { String ss = (String)e.nextElement(); Log.debug(ss + " : " + prop.getProperty(ss)); } } catch (FileNotFoundException fnfe ) { if (isInternalConfig) { org.jcrontab.data.DefaultFiles.createJcrontabDir(); org.jcrontab.data.DefaultFiles.createCrontabFile(); org.jcrontab.data.DefaultFiles.createPropertiesFile(); loadConfig(); } else { throw new FileNotFoundException("Unable to find: " + strFileName); } } prop.setProperty("org.jcrontab.config", strFileName); prop.setProperty("org.jcrontab.version", version); } /** * This method returns all the properties basically to show them * @return Properties prop */ public Properties getConfig() { if (prop == null) { try { loadConfig(); } catch (Exception e) { Log.error(e.toString(), e); } } return prop; } /** * This method sets the right values for the configuration * @param String properties */ public void setConfig(String properties) { strFileName = properties; } /** * This method gets the value of the given property * @param property * @return value */ public String getProperty(String name) { return prop.getProperty(name); } /** * This method sets the given property * @param property * @param value */ public void setProperty(String name, String value) { prop.setProperty(name, value); } /** * This method removes the given property * @param property * @param value */ public void removeProperty(String name) { prop.remove(name); try { File filez = new File(strFileName); filez.delete(); OutputStream out = new FileOutputStream(filez); prop.store(out, "#"); out.close(); } catch (Exception e){ Log.error(e.toString(), e); } } /** * This method Stores in the properties File the given property and all the * "live" properties * @param property * @param value */ public void storeProperty(String property, String value) { prop.setProperty(property, value); try { File filez = new File(strFileName); filez.delete(); OutputStream out = new FileOutputStream(filez); prop.store(out, "#"); out.close(); } catch (Exception e){ Log.error(e.toString(), e); } } /** * This method says if today is a holiday or not * @return true if today is holiday false otherWise * @throws Exception */ public boolean isHoliday() throws Exception { if (getProperty("org.jcrontab.data.holidaysource") == null || getProperty("org.jcrontab.data.holidaysource") == "") return false; Calendar today = Calendar.getInstance(); HoliDay[] holidays = HoliDayFactory.getInstance().findAll(); for (int i = 0; i< holidays.length; i++) { Calendar holiday = Calendar.getInstance(); holiday.setTime(holidays[i].getDate()); if (holiday.get(Calendar.MONTH) == today.get(Calendar.MONTH) && holiday.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH) && holiday.get(Calendar.YEAR) == today.get(Calendar.YEAR)) { return true; } } return false; } /** * Creates and runs a new task * @param strClassName Name of the task * @param strMethodName Name of the method that will be called * @param strExtraInfo Extra Information given to the task * @return The identifier of the new task created, or -1 if could not create * the new task (maximum number of tasks exceeded or another error) */ public synchronized int newTask(String strClassName, String strMethodName, String[] strExtraInfo) { CronTask newTask; Class cl; int iTaskID; // Do not run new tasks if it is uninitializing if(stoping) { return -1; } String params = ""; try { iTaskID = iNextTaskID; cl = (Class)(loadedClasses.get(strClassName)); // Creates the new task newTask = new CronTask(); newTask.setParams(this, iTaskID, strClassName, strMethodName, strExtraInfo); // Aded name to newTask to show a name instead of Threads whe // logging // Thanks to Sander Verbruggen int lastDot = strClassName.lastIndexOf("."); if (lastDot > 0 && lastDot < strClassName.length()) { String classOnlyName = strClassName.substring(lastDot + 1); newTask.setName(classOnlyName); } synchronized(tasks) { tasks.put(new Integer(iTaskID), new TaskTableEntry(strClassName, newTask)); } // Starts the task execution newTask.setName("Crontask-"+iTaskID); newTask.start(); if (strExtraInfo!=null && strExtraInfo.length > 0) { for (int i = 0; i < strExtraInfo.length;i++) { params+=strExtraInfo[i] + " "; } } Log.info(strClassName + "#" + strMethodName + " " + params); // Increments the next task identifier iNextTaskID++; return iTaskID; } catch(Exception e) { Log.error("Smth was wrong with" + strClassName + "#" + strMethodName + " " + params, e); } return -1; } /** * Removes a task from the internal arrays of active tasks. This method * is called from method run() of CronTask when a task has finished. * @return true if the task was deleted correctly, false otherwise * @param iTaskID Identifier of the task to delete */ public boolean deleteTask(int iTaskID) { synchronized(tasks) { if( tasks.remove(new Integer(iTaskID)) == null) return false; return true; } } /** * Returns an array with all active tasks * @return An array with all active tasks * NOTE: Does not returns the internal array because it is synchronized, * returns a copy of it. */ public CronTask[] getAllTasks() { CronTask[] t; synchronized(tasks) { int i = 0; t = new CronTask[tasks.size()]; Iterator iter = tasks.values().iterator(); while(iter.hasNext()) { t[i] = ((TaskTableEntry)(iter.next())).task; i++; } } return t; } /** * Internal class that represents an entry in the task table */ private class TaskTableEntry { String strClassName; CronTask task; /** Constructor of an entry of the task table * @param strClassName Name of the class of the task * @param task Reference to the task */ public TaskTableEntry(String strClassName, CronTask task) { this.strClassName = strClassName; this.task = task; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -