28:----------------spring 定时任务 scheduled

Posted on 2017-07-14 14:22  奇思妙想的香菜  阅读(178)  评论(0编辑  收藏  举报

1.配置spring.xml

 

openRateScheduledService :定时任务类
cron="0 49 12 * * ?" :时间设置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<task:executor id="taskExecutor" pool-size="10" />

<task:scheduled-tasks >
<task:scheduled ref="openRateScheduledService" method="doTask" cron="0 49 12 * * ?"/>
</task:scheduled-tasks>

</beans>

 

 

2.配置web.xml

classpath:spring/applicationContext-quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>dapail</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/applicationContext-core-spec.xml
classpath:spring/applicationContext-quartz.xml
classpath:spring/applicationContext-dao.xml
classpath:spring/applicationContext-service.xml
classpath:spring/applicationContext-transaction.xml
classpath:spring/applicationContext-redis.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/mvc-dispatcher.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
<context-param>
<param-name>spring.liveBeansView.mbeanDomain</param-name>
<param-value>dev</param-value>
</context-param>
</web-app>

 

3.Java

package com.dapail.manager.service.task;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dapail.common.IResult;
import com.dapail.common.ResultData;
import com.dapail.manager.cache.CmdCache;
import com.dapail.manager.mapper.WorkerMapper;
import com.dapail.util.MLogger;

/**
 * 开机率统计
 * 
 * @author Administrator
 *
 */
@Service
public class OpenRateScheduledService {

    // 指令
    @Autowired
    private CmdCache cmdCache;

    @Autowired
    private WorkerMapper workerMapper;

    // 每天凌晨两点执行 记录所有账户 上一天的机器使用率
    public void doTask() {
        long begin = System.currentTimeMillis();
        MLogger.info("/" + begin + "t_stats_open_rate start");
        // 往前推一天
        Calendar c1 = Calendar.getInstance();
        int paDay = c1.get(Calendar.DATE);
        c1.set(Calendar.DATE, paDay - 1);
        String daytime = new SimpleDateFormat("yyyyMMdd").format(c1.getTime());
        String cmdKey = daytime;
        // 循环账户 t_ratio
        List<Object> acc = workerMapper.getAccountList();
        IResult accRes = new ResultData(acc);
        for (int i = 0; i < accRes.size(); i++) {
            // 账户类别
            String account_type = accRes.get(i).get("ACCOUNT_TYPE").toString();

            // 账户id
            String account_id = accRes.get(i).get("ACCOUNT_ID").toString();
            // 账户下机器列表
            List<Object> maAccList = workerMapper.getRatioByAccId(account_id);
            IResult maAccIR = new ResultData(maAccList);
            
            // 名下所有机器
            int accAllNum = maAccIR.size();
            // 当日开机率
            String openPercent = "0";
            
            if (maAccIR.size()>0) {
                String[] machineId = new String[maAccIR.size()];
                // 账户下机器列表
                for (int j = 0; j < maAccIR.size(); j++) {
                    // 机器id
                    machineId[j] = maAccIR.get(j).get("MACHINE_ID").toString();
                }
                MLogger.info("/机器列表:t_stats_open_rate start+machineId:" + machineId + "");
                

                // 当日 机器数加1
                List<String> ma = cmdCache.getMaOpenTime(cmdKey, machineId);
                int maOpenNum = 0;
                for (int j = 0; j < ma.size(); j++) {
                    if (ma.get(j) != null && ma.get(j).equals("")) {
                        maOpenNum = maOpenNum + 1;
                    }
                }
                BigDecimal maOpenNumb1 = new BigDecimal(String.valueOf(maOpenNum));
                BigDecimal accAllNumb2 = new BigDecimal(String.valueOf(accAllNum));
                // 小数点保留0位
                openPercent = String.format("%.0f",
                        maOpenNumb1.divide(accAllNumb2, 2, BigDecimal.ROUND_HALF_UP).doubleValue() * 100);
            }
            
            
            

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

            // 经销商
            if (account_type.equals("3")) {
                account_type = "2";
            }
            // 棋牌室
            if (account_type.equals("2")) {
                account_type = "1";
            }

            map.put("other_id", account_id);
            map.put("type", account_type);
            map.put("open_time", daytime);
            map.put("open_rate", openPercent);
            map.put("open_details", "");
            map.put("timestamps", System.currentTimeMillis() / 1000);
            // 插入一条
            MLogger.info("/插入一条:t_stats_open_rate start+account_id:" + account_id + "+account_type:" + account_type
                    + "+daytime:" + daytime + "+openPercent:" + openPercent + "");
            workerMapper.insertStateOpen(map);

        }
        // 执行数据库操作了哦...
        long end = System.currentTimeMillis();
        MLogger.info("/" + end + "t_stats_open_rate start");
    }
}