SSH Framework Online Mall project 15th war thread, Timer sync home data _java

Source: Internet
Author: User
Tags time interval ssh

In the last section we finished the UI interface, but there is a problem: if I added a product in the background, then I have to restart the server to resynchronize the background data, and then refresh the home page to synchronize data. This is obviously not the effect we want, generally this online mall home page is certainly not manual synchronization data, then how to solve it? We need to use the thread and timer to automatically synchronize the first page data.
1. Timer and TimerTask
we need to use timer and timertask two classes. Let's introduce the two classes first.
A timer is a tool class that, in a java.util package, is used by a thread to schedule tasks to be performed later in a background thread. You can schedule tasks to execute once, or repeat them periodically. It has a constructor function:

Timer (Boolean Isdaemon) 
//Create a new timer that can specify its associated thread to run as a daemon. 

The daemon thread ends when the main thread is finished, and the thread continues execution after the non-daemon thread ends. Isdaemon is true for the daemon thread. The Timer class has a schedule method to create a task, as follows:

void Schedule (timertask task, Date Firsttime, long Period)  
//Schedule a specified task to begin a recurring fixed-delay execution at a specified time. 
//The first parameter is the specified task, that is, the TimerTask object, the second parameter is the first time to open the task,

and the third parameter is the time interval, that is, how often each time is executed 

Let's see Timertask,timertask is used to create a new thread task, it implements the Runnable interface, if we want to create a new thread task, just 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//give this object to spring Admin 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 deposit the application domain public void setapplication (ServletContext application) { this.application = Application; Set the Application object in by the listener because it is not possible to get the Application object's} @Override//And the listener's logic of data initialization when the project starts is public void run ( 
    ) {System.out.println ("----run----"); list<list<product>> biglist = new arraylist<list<product>> (); Biglist contains a list//1 containing the category class. Query out hotspot category for (Category Category:categoryService.queryByHot (True)) {//Get recommended product information based on hotspot category ID list<product& Gt 
      LST = Productservice.querbycategoryid (Category.getid ()); Biglist.add (LST); 
Place the list containing category in Biglist}     2. The biglist of the query is handed over to application built-in object Application.setattribute ("Biglist", biglist); 
 Let's say we've got the Application object}}

Next, we modify the contents of the listener inside the start of the project, the original above the query operation is placed in the listener, when the project started, the listener began to execute, get the background data, save to the application domain, and then the foreground through the JSTL tag from the application domain to get the data. Now we put these things to our definition of the producttimertask to do, then the listener as long as set the timer, so that producttimertask regularly to update the background data can. Look at the modified code in the listener:

3. Start the timer in the listener

@Component//listener is a component of the Web layer that is tomcat-instantiated, not spring-instantiated. cannot be placed in spring public class Initdatalistener implements Servletcontextlistener {private Producttimertask Producttime Rtask = null; 
   
  Defines 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.GETW         
    Ebapplicationcontext (Event.getservletcontext ()); Producttimertask = (producttimertask) context.getbean ("Producttimertask")//Get Producttimertask object from config file//put the built-in object to PR Oducttimertask, because the producttimertask inside is not get application, only through the listener set to it producttimertask.setapplication ( 
 
    Event.getservletcontext ()); By setting the timer, the data for the first page is synchronized once per hour (configured as a daemon) new timer (true). Schedule (producttimertask, 0, 1000*60*60);  Perform a producttimertask task one hour at a time, updating the background data}

About the Initdatalistener listener in the original operation code, you can compare the contents of the previous section, in fact, is the Producttimertask update background data, but now put into the timertask to do it. This completes the use of threads and timers to synchronize the first page data periodically, this time interval can be set itself.
In fact, CSDN blog part of the home page data is not updated in real time, every night there will be a time to update, such as the left column in the blog ranking, reading the number of readings after the display, these are updated every night, should be in the background set up every day update, the principle and here should be the same. This also eases the pressure on the server.

This article link: http://blog.csdn.net/eson_15/article/details/51387378

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.