In the previous section, we finished the home page UI interface, but there is a problem: if I add a product in the background, then I have to restart the server to resync the background data, and then refresh the home page to synchronize the data. This is obviously not the effect we want. Generally, this kind of online mall homepage is definitely not manually synchronized data, so how to solve it? We need to use threads and timers to automatically synchronize homepage data regularly.
1. Timer and TimerTask
We need to use the Timer and TimerTask classes. Let’s introduce these two categories first.
Timer is a tool class. In the java.util package, threads use it to arrange tasks that will be executed in background threads later. Tasks can be executed once, or they can be executed regularly. It has a constructor:
Timer(boolean isDaemon) //Create a new timer that can specify its associated thread to run as a daemon.
After the daemon thread, that is, the main thread ends, the thread also ends, and after the non-daemon thread, that is, the main thread ends, the thread continues to execute. When isDaemon is true, it is the daemon thread. The Timer class has a schedule method that can create a task, as follows:
void schedule(TimerTask task, Date firstTime, long period) //Schedule the specified task to perform repeated fixed delay execution at the specified time. //The first parameter is the specified task, that is, the TimerTask object; the second parameter is the first task opening time; the third parameter is the time interval, that is, how long does it take to execute every time
Let's take a look at TimerTask. TimerTask is used to create a new thread task. It implements the Runnable interface. If we want to create a new thread task, we only need to inherit TimerTask and rewrite the run method.
2. Create a new thread task
Let's create a new thread task to update the background data:
@Component //Leave the object to Spring management public class ProductTimerTask extends TimerTask { @Resource private ProductService productService = null; //Inject productService @Resource private CategoryService categoryService = null; //Inject categoryService private ServletContext application = null; //Define a ServletContext object, because after we update the background data, we need to store it in the application domain public void setApplication(ServletContext application) { this.application = application; //Set this application object through the listener, because the application object cannot be taken here} @Override //The same logic as the listener's data initialization when the project starts public void run() { System.out.println("--------"); List<List<Product>> bigList = new ArrayList<List<Product>>(); //Storage a list with the Category class in bigList // 1. Query out the hot category for(Category category: categoryService.queryByHot(true)) { //Get recommended product information based on the hot category id List<Product> lst = productService.querByCategoryId(category.getId()); bigList.add(lst); //Put the list with category in bigList} // 2. Leave the query bigList to the application built-in object application.setAttribute("bigList", bigList); //Suppose we have got the application object} }Next, we modify the contents of the listener when the project is started. The original query operation above was placed in the listener. When the project is started, the listener starts to execute, obtains background data, and stores it in the application domain. Then the foreground gets the data from the application domain through the jstl tag. Now we hand over these things to the ProductTimerTask we defined, so just set a timer in the listener and let the ProductTimerTask update the background data regularly. Check out the modified code in the listener:
3. Start the timer in the listener
//@Component // Listener is a component of the web layer. It is instantiated by tomcat, not Spring. Can't be put in Spring public class InitDataListener implements ServletContextListener { private ProductTimerTask productTimerTask = null; //Define a ProductTimerTask object private ApplicationContext context = null; @Override public void contextDestroyed(ServletContextEvent event) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent event) { context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); productTimerTask = (ProductTimerTask) context.getBean("productTimerTask");//Get ProductTimerTask object from the configuration file//Talk the built-in object to productTimerTask, because the productTimerTask cannot get the application, you can only give it through the listener set productTimerTask.setApplication(event.getServletContext()); //Set the timer to synchronize the data on the homepage once every hour (configured as a daemon thread) new Timer(true).schedule(productTimerTask, 0, 1000*60*60);//Execute the productTimerTask task once every hour, that is, update the background data} } Regarding the original operation code in the InitDataListener listener, you can compare the content in the previous section. In fact, it is the update background data in ProductTimerTask, but it is just put into TimerTask for now. In this way, we complete the use of threads and timers to regularly synchronize homepage data, and this time interval can be set by ourselves.
In fact, some homepage data in CSDN blogs are not updated in real time. There will be a time to update every night, such as the blog ranking in the left column, the number of views displayed after reading ranking, etc. These are updated every night, and it should be set in the background to update once a day. The principle should be the same as here. This also reduces the pressure on the server.
Link to this article: http://blog.csdn.net/eson_15/article/details/51387378
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.