PackageCom.ly.fw.util;ImportJava.io.Reader;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Timer;Importorg.apache.ibatis.io.Resources;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder; Public classTimermanager {//time interval Private Static Final LongPeriod_day =24*60*60*1000; PublicTimermanager () {Calendar Calendar=calendar.getinstance (); /*** Customized Daily 2:00 execution method * **/Calendar.set (Calendar.hour_of_day,17);//hoursCalendar.set (calendar.minute,45);//minutesCalendar.set (Calendar.second, 10);//secondsDate Date=calendar.gettime ();//time of the first execution of a scheduled task//if the time to perform the first scheduled task is less than the current time//At this point, the time for the first scheduled task is added to the day so that the task executes at the next point in time. If one day is not added, the task executes immediately. if(Date.before (NewDate ())) {Date= This. Addday (Date, 1); } Timer Timer=NewTimer (); Vipdatatimertask Task=NewVipdatatimertask (); //schedules the specified task to begin repeating fixed deferred execution at a specified time. Timer.schedule (Task,date,period_day); } //increase or decrease the number of days PublicDate Addday (date date,intnum) {Calendar Startdt=calendar.getinstance (); Startdt.settime (date); Startdt.add (Calendar.day_of_month, num); returnStartdt.gettime (); } Public Staticsqlsession getsqlsession () {sqlsession sqlsession=NULL; Try{ //generating sqlsessionfactory from a configuration instanceSqlsessionfactory sqlsessionfactory =NULL; //generating sqlsessionfactory from an XML configuration fileString resource = "Com/ly/fw/service/configuration.xml"; Reader Reader=Resources.getresourceasreader (Resource); Sqlsessionfactory=NewSqlsessionfactorybuilder (). build (reader); Sqlsession=sqlsessionfactory.opensession (); }Catch(Exception e) {e.printstacktrace (); } returnsqlsession; } }
PackageCom.ly.fw.util;Importjava.util.List;ImportJava.util.TimerTask;ImportCom.ly.fw.entity.User;ImportCom.ly.fw.entity.Vip;ImportCom.ly.fw.service.ScoreLogService;ImportCom.ly.fw.service.UserService;ImportCom.ly.fw.service.VipService; Public classVipdatatimertaskextendsTimerTask {@Override Public voidrun () {System.out.println ("***************** thread is called *****************************"); Try { //Circle all the membersList<user> uList =Userservice.vipusertask (); for(inti = 0; I < ulist.size (); i++) {User u=Ulist.get (i); VIP VIP=Vipservice.viptask (U.getvipid ()); //Bonus points for the respective membership levelUserservice.scorechangetask (Vip.getdayscore (), U.getuserid ()); Scorelogservice.addbytask (Vip.getdayscore (), U.getuserid ()); //Write a change log for member points} System.out.println ("++++++++++++++++++++ Execution Complete"); } Catch(Exception e) {e.printstacktrace (); } } }
The last Listener
PackageCom.ly.fw.util;Importjavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener; Public classVipdatatasklistenerImplementsServletcontextlistener { Public voidcontextinitialized (Servletcontextevent event) {NewTimermanager (); } Public voidcontextdestroyed (Servletcontextevent event) {}}
Integrate the listener into the program when the system is started in Web. xml
<listener> <listener-class>com.ly.fw.util.vipdatatasklistener</listener-class ></listener>
Initially lazy directly to the new Timermanager () put on the first page to execute, the results found that each time more than a few times, and more and more, immediately wake up call to add a task to the program, called many times there are multiple scheduled task execution . To record it.
Java Timer performs tasks regularly