Java Web 定时任务

1 篇文章 0 订阅

使用的java web框架是spring+springmvc,当web服务启动以后,会有一个定时的任务去完成某一些特定的功能。在定时任务中支持注解其他的service等。

1、首先,在applicationContext-mvc.xml(xml配置文件的名称可能不一样)文件中,需要有这样一段代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <mvc:annotation-driven/>

    <mvc:default-servlet-handler/>

    <context:component-scan base-package="com.xxx.logic"/>

    <bean class="com.xxx.SyncPro"></bean>

    <bean id="sessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.xxx.model"/>
        <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
        <property name="basePackage" value="com.xxx.mapper"/>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        <qualifier value="txm"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="freemarkerViewResolver"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="order" value="1"/>
        <property name="suffix" value=".html"/>
        <property name="contentType" value="text/html;charset=utf-8"/>
        <property name="exposeRequestAttributes" value="true"/>
        <property name="exposeSessionAttributes" value="true"/>
        <property name="exposeSpringMacroHelpers" value="true"/>
        <property name="requestContextAttribute" value="rc"/>
        <property name="redirectHttp10Compatible" value="false"/>
        <property name="viewClass">
            <value>com.xxx.FreeMarkerView
            </value>
        </property>
    </bean>

    <bean id="freemarkerConfig"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">5</prop>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="locale">UTF-8</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="time_format">HH:mm:ss</prop>
                <prop key="number_format">0.####</prop>
                <prop key="boolean_format">true,false</prop>
                <prop key="whitespace_stripping">true</prop>
                <prop key="tag_syntax">auto_detect</prop>
                <prop key="url_escaping_charset">UTF-8</prop>
            </props>
        </property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="104857600"/>
        <property name="maxInMemorySize" value="4096"/>
    </bean>

    <mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="com.xxx.AuthInterceptor">
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

</beans>

可以找到这样一段代码 :

<bean class="com.xxx.SyncPro"></bean>

2、其次,核心代码这样写

package com.xxx.main;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by niyl 2018/7/26
 */
public class SyncPro implements InitializingBean {

    private static final Logger logger = Logger.getLogger(SyncPro.class);

    @Override
    public void afterPropertiesSet() throws Exception {
        new GetWebSiteTH().start();
    }

    class GetWebSiteTH extends Thread {
        public void run() {
            while (true) {
                try {
                    logger.info("SYS|SyncPro|定时访问网站xxx.com|"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//                    HttpClient client = new HttpClient();
                    Thread.sleep(6 * 60 * 60 * 1000L);
                } catch (Exception e) {
                    logger.error("SYS|SyncPro|定时访问网站xxx.com出错|", e);
                }
            }
        }
    }

}

即可完成javaweb 的定时任务。其实可以在java代码中加入注解 

@Autowired

private UsrService usrService;

我在这里并没有写。

定时效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值