StartUpListener类

1.web.xml

<listener>

<listener-class>com.jdd.sjw.spring.StartupListener</listener-class>

</listener>

2.StartUpListener类

package com.jdd.sjw.spring;


import java.io.File;

import java.io.FileInputStream;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import javax.servlet.ServletContext;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;


import org.apache.commons.io.IOUtils;


import com.jdd.sjw.service.impl.CommodityServiceImpl;

import com.jdd.sjw.util.JavaPathUtils;


public class StartupListener implements ServletContextListener {


public void contextInitialized(ServletContextEvent event) {

/**

* 获取正常活动配置时间

*/

ServletContext context = event.getServletContext();

Map<String, Date> timeMap = getActivityTimeMap();

context.setAttribute("timeMap", timeMap);

}

public Map<String,Date> getActivityTimeMap() {

Map<String,Date> map=new HashMap<String,Date>();

String classesPath = JavaPathUtils

.getClassLoaderPath(CommodityServiceImpl.class);

String memberCommodityPath = classesPath + "activity_time.txt";

List<String> lines = null;

FileInputStream fis = null;

try {

fis = new FileInputStream(new File(memberCommodityPath));

lines = IOUtils.readLines(fis);

} catch (Exception e) {

//logger.error("error read commodity_member.txt:{}",ExceptionUtil.exToString(e));

} finally {

IOUtils.closeQuietly(fis);

}

if (lines == null || lines.size() == 0) {

return map;

}

for (String line : lines) {

String comMem[] = line.split("-");

String startTime = null;

String endTime = null;

try {

startTime = String.valueOf(comMem[0].trim());

endTime = String.valueOf(comMem[1].trim());

SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");

SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

SimpleDateFormat sdf2=new SimpleDateFormat("HH");

Date start=null;

Date end=null;

String hh=sdf2.format(new Date());

Calendar calendar = Calendar.getInstance(); //得到日历

calendar.setTime(new Date());//把当前时间赋给日历

if(Integer.valueOf(hh)<20){

calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天

start = sdf1.parse(sdf.format(calendar.getTime())+" "+startTime);

end = sdf1.parse(sdf.format(new Date())+" "+endTime);

map.put("startTime", start);

map.put("endTime", end);

System.out.println("activity_time.txt------>"+String.valueOf(map));

return map;

}else{

calendar.add(Calendar.DAY_OF_MONTH, 1);  //设置为后一天

start=sdf1.parse(sdf.format(new Date())+" "+startTime);

end=sdf1.parse(sdf.format(calendar.getTime())+" "+endTime);

map.put("startTime", start);

map.put("endTime", end);

System.out.println("activity_time.txt------>"+String.valueOf(map));

return map;

}

} catch (Exception e) {

//logger.error("parse commodity specical member generate error:{}",ExceptionUtil.exToString(e));

continue;

}

}

return map;

}


public static void setupContext(ServletContext context) {

}


public void contextDestroyed(ServletContextEvent servletContextEvent) {

servletContextEvent.getServletContext().removeAttribute("timeMap");

}

}

3.

ServletContext context=message.getRequest().getSession().getServletContext();

Map<String,Date>map=(Map<String, Date>) context.getAttribute("timeMap");


你可能感兴趣的:(StartUpListener类)