001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.q2.iso; 020 021import org.jdom2.Element; 022import org.jpos.core.Configurable; 023import org.jpos.iso.ISOUtil; 024import org.jpos.q2.QBeanSupport; 025import org.jpos.q2.QFactory; 026 027import java.util.Calendar; 028import java.util.Date; 029import java.util.GregorianCalendar; 030 031/** 032 * DailyTask Adaptor 033 * 034 * @author Alejandro Revilla 035 * @version $Revision$ $Date$ 036 */ 037public class DailyTaskAdaptor extends QBeanSupport implements Runnable { 038 Runnable task; 039 Thread thisThread = null; 040 041 public DailyTaskAdaptor () { 042 super (); 043 } 044 045 protected void initService () throws Exception { 046 QFactory factory = getServer().getFactory(); 047 Element e = getPersist (); 048 task = (Runnable) factory.newInstance (e.getChildTextTrim ("class")); 049 factory.setLogger (task, e); 050 } 051 protected void startService () throws Exception { 052 if (task instanceof Configurable) { 053 Element e = getPersist (); 054 QFactory factory = getServer().getFactory(); 055 ((Configurable)task).setConfiguration ( 056 factory.getConfiguration (e) 057 ); 058 } 059 (thisThread = new Thread(this)).start(); 060 } 061 protected void stopService () throws Exception { 062 if (thisThread != null) 063 thisThread.interrupt(); 064 } 065 public void run () { 066 while (running()) { 067 waitUntilStartTime(); 068 if (running()) { 069 Thread taskThread = new Thread(task); 070 taskThread.setDaemon (true); 071 taskThread.start(); 072 ISOUtil.sleep (1000); 073 } 074 } 075 } 076 public Date getWhen() { 077 String s = cfg.get ("start")+":00:00"; // NOPMD 078 int hh = Integer.parseInt(s.substring (0, 2)); 079 int mm = Integer.parseInt(s.substring (3, 5)); 080 int ss = Integer.parseInt(s.substring (6, 8)); 081 082 Date now = new Date(); 083 Calendar cal = new GregorianCalendar(); 084 085 cal.setTime (now); 086 cal.set (Calendar.HOUR_OF_DAY, hh); 087 cal.set (Calendar.MINUTE, mm); 088 cal.set (Calendar.SECOND, ss); 089 090 Date when = cal.getTime(); 091 if (when.before(now)) 092 when = new Date(when.getTime() + 24*60*60*1000); 093 094 return when; 095 } 096 protected void waitUntilStartTime() { 097 Date when = getWhen(); 098 while (running()) { 099 Date now = new GregorianCalendar().getTime(); 100 if (now.before (when)) { 101 long sleepTime = when.getTime() - now.getTime(); 102 if (sleepTime <= 0) { 103 ISOUtil.sleep(1000); 104 continue; 105 } 106 getLog().info ("sleeping", 107 sleepTime/1000 + " secs until " + when.toString() 108 ); 109 try { 110 Thread.sleep (sleepTime); 111 } catch (InterruptedException e) { 112 when = getWhen(); 113 } 114 } else 115 break; 116 } 117 } 118} 119