package com.yun.modules.MyTest;
import com.yun.modules.base.ServletInit;
import com.yun.modules.entity.GoodsClassify;
import com.yun.modules.service.GoodsClassifyService;
import com.yun.utils.DateUtils;
import com.yun.utils.HttpRequest;
import com.yun.utils.MD5;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletContextAware;
import javax.servlet.ServletContext;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import static java.lang.Thread.sleep;
/**
* Created by yun_lian on 2016/10/19.
* 服务器初始化时完成
*/
@Controller
@RequestMapping(value = "startInit")
public class StartInitTest implements InitializingBean,ServletContextAware {
@Autowired
private GoodsClassifyService goodsClassifyService;
/**
* 处理服务器启动后的数据
*/
@RequestMapping(value = "test")
public void initPart(){
while (true){
GoodsClassify goodsClassify = new GoodsClassify();
goodsClassify.setBirthday(new Date());
goodsClassify.setName(((int)(Math.random()*200)+1)+"号名字");
goodsClassify.setPassword(MD5.getMD5Str(goodsClassify.getName()));
goodsClassify.setSex(1);
goodsClassifyService.save(goodsClassify);
try {
sleep(2000);//睡2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void afterPropertiesSet() throws Exception {
}
@Override
public void setServletContext(ServletContext servletContext) {
TimerTask task = new TimerTask() {
@Override
public void run() {
//处理业务逻辑
String url = ServletInit.getAdminUrl()+"startInit/test";
System.out.println(url);
HttpRequest.sendPost(url,"");
}
};
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.add(13,5);//延迟5秒时间
Date date = calendar.getTime();
timer.schedule(task, date);
}
}