spingmvc实现在程序启动时调用数据库数据

直接上代码:

package com.java.zxf.servlet;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ServletContextAware;
import com.java.zxf.domain.Backvisit;
import com.java.zxf.domain.Orders;
import com.java.zxf.service.BackvisitService;
import com.java.zxf.service.OrdersService;
import com.java.zxf.util.ShowString;
import com.java.zxf.util.SystemOut;
import com.java.zxf.util.TimeUtil;
/**
 * 默认好评实现了类
 * @author lenovo
 *
 */
@Controller
public class EvaluateServlet extends HttpServlet implements ServletContextAware{



    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Autowired
    private OrdersService orderService;
    @Autowired
    private BackvisitService backvisitService;
    private SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Logger log = Logger.getLogger(EvaluateServlet.class);
    /**
     * 
     */

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    Timer timer = new Timer();
    private String className = Thread.currentThread().getStackTrace()[1].getClassName();
    private Orders order;
    Calendar calender = new GregorianCalendar();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            order = orderService.selecttoevaluate();
            String t = order.getExpiretime();
            long during = 0;
            try {
                during = formater.parse(t).getTime();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            calender.setTime(new Date(during));
            //默认订单过期后7天后自动好评
            calender.add(Calendar.DATE, ShowString.defaultEvaluatetime);
            long remain = calender.getTimeInMillis()-during;
            try {
                Thread.currentThread().sleep(remain-10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Backvisit backvisit = new Backvisit();
            String times = TimeUtil.transtolinesep(new Date(during));
            List<Orders> orderlist = orderService.findorderbyexpiretime(times);
            for (Orders orders : orderlist) {
                backvisit.setOrderId(orders.getOrderId());
                backvisit.setAdvice("默认好评");
                backvisit.setDos(5);
                backvisit.setBackvisitMethod("线上评论");
                backvisit.setCompleteTime(TimeUtil.transtolinesep(new Date()));
                backvisit.setIscomplte("y");
                int iflag = backvisitService.insert(backvisit);
                if (iflag == 1){
                    SystemOut.classSystemOut(className, "好评完成,将执行下一轮任务");
                }
            }
        }
    };
    
    //在重写该方法后就可以在程序启动时候去调用数据库的数据了。
    @Override
    public void setServletContext(ServletContext servletContext) {
        // TODO Auto-generated method stub
        System.out.println("--------默认好评任务启动成功--------"); 
        Date time = new Date();
        timer.schedule(task,time,24*60*60*1000L);
    }
    
    
    
}

 

posted @ 2018-09-10 17:00  漂渡  阅读(305)  评论(0编辑  收藏  举报