Determine how the Spring timer obtains the deployment path of the target.
Recently, when I was using the timer, I used the timer path in Spring, but in the timer, there is no way to retrieve the request or session, and the method cannot be used to directly retrieve it. So how can this problem be solved? In fact, there are still some ideas. It's just a matter of writing a line around the horse. How can spring be overwhelmed by such small things.
Let me give you an example. We need to add such a timer. His requirements are as follows: one-time files generated by the project are split once every day.
The timer generation is as follows:
Package com. smartsoft. quartz; import java. io. file; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; import java. util. gregorianCalendar; import org. springframework. beans. factory. annotation. autowired; import org. springframework. scheduling. annotation. scheduled; import org. springframework. stereotype. component; import com. smartsoft. common. constant; import com. smartsoft. service. UserService; @ Componentpublic class AnnotationQuartz {@ AutowiredUserService UserService; @ Scheduled (cron = "0 0 0/1 **? ") Public void test () {int days = 2; // TODO Time Sequence File SimpleDateFormat sf = new SimpleDateFormat (" yyyyMMdd "); Date date = new Date (); calendar calendar = new GregorianCalendar (); calendar. setTime (date); for (int I = 1; I <= 30; I ++) {calendar. add (calendar. DATE,-1); date = calendar. getTime (); String dateStr = sf. format (date); String docx_Report = Constant. get_REALPATH () + "templetUpload \" + dateStr + "\ docx_Repor T "; String html_Report = Constant. get_REALPATH () + "templetUpload \" + dateStr + "\ html_Report"; String pai_report = Constant. get_REALPATH () + "templetUpload \" + dateStr + "\ cmd_report"; String xlsx_Report = Constant. get_REALPATH () + "templetUpload \" + dateStr + "\ xlsx_Report"; delFolder (docx_Report); delFolder (html_Report); delFolder (pai_report); delFolder (xlsx_Report );}} // Delete the folder // The complete absolute path pu Of The param folderPath folder Blic static void delFolder (String folderPath) {try {delAllFile (folderPath); // After deleting all content, String filePath = folderPath; filePath = filePath. toString (); java. io. file myFilePath = new java. io. file (filePath); myFilePath. delete (); // delete an empty folder} catch (Exception e) {e. printStackTrace () ;}}// delete all files in the specified folder // The full absolute path of the param path folder public static boolean delAllFile (String path) {boolean flag = false; file file = new Fi Le (path); if (! File. exists () {return flag;} if (! File. isDirectory () {return flag;} String [] tempList = file. list (); File temp = null; for (int I = 0; I <tempList. length; I ++) {if (path. endsWith (File. separator) {temp = new File (path + tempList [I]);} else {temp = new File (path + File. separator + tempList [I]);} if (temp. isFile () {temp. delete ();} if (temp. isDirectory () {delAllFile (path + "/" + tempList [I]); // first delete the file delFolder (path + "/" + tempList [I]) in the folder; // then delete the empty folder flag = true;} return flag ;} public static void main (String [] args) {System. out. println (AnnotationQuartz. class. getResource ("/"));}}
The above generation is very simple, that is, the pure division of files and files. Since the division of files is a division of files, the file must be known in the destination path, how can we achieve this? Let's take a look at the Token Generation.
First, create a new class as follows:
Public class InitialServlet extends HttpServlet {private static final long serialVersionUID = 1L; @ Override public void init () throws ServletException {super. init (); System. out. println ("START intitServlet"); String realPath = getServletContext (). getRealPath ("/"); Constant. set_REALPATH (realPath );}}
Obtain the project path and save it in the Constant of Constant.
Public class Constant {public final static int PAGE_SIZE_10 = 10; // initialize the paging // project path. You can use a singleton to store public static String _ REALPATH = ""; public static String get_REALPATH () {return _ REALPATH;} public static void set_REALPATH (String _ REALPATH) {Constant. _ REALPATH = _ REALPATH ;}}
Specifically, we provide him with get, set
The focus is to make InitialServlet Save the project path when the project is started, and initialize the project in the web. xml definition section.
InitialServlet
com.smartsoft.util.InitialServlet
0
In this way, whenever the project is started, we can use Constant. get_REALPATH () is used to obtain the project path. Here, we only use some little servlet knowledge. If you don't understand it, please first understand servlet.