java定时任务代理配置

需要sqring-context-support.jar ,java-memcached-client-2.6.6jar 的包,用到quartz中的schedulerFactoryBean和CronTriggerFactoryBean,MethodInvokingJobDetailFactoryBean类


 <context:component-scan base-package="com.sf.teshi.job"/>

<!-- 抓取外部系统的订单的定时任务服务 -->


   <!-- 启动触发器的配置开始 -->  
    <bean name="schedulerFactory" lazy-init="false" autowire="no"  
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">
            <list>
                <ref bean="supplierReportJobTrigger" />  //映射
            </list>
        </property>
    </bean> 


   <bean id="supplierReportJobTrigger"  
        class="com.sf.teshi.job.util.TeshiReportCronTriggerFactoryBean">         //构造一个类代理
        <property name="jobDetail" ref="supplierReportJobDetail" />                //映射
        <property name="cronExpression">
            <value>0 0 0 1 * ?</value>
        </property>
    </bean>

<bean id="supplierReportJobDetail"  
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject"  ref="supplierReportJob" />                 //映射到具体的方法
        <property name="targetMethod"  value="supplierReport" />
    </bean>


<bean id="supplierReportJob" class="com.sf.teshi.job.SupplierReportJob" />




/**
 * 定时任务启动周期代理类,将时间控制代理到数据库配置
 * @author 870850
 *
 */
public class TeshiReportCronTriggerFactoryBeanextends CronTriggerFactoryBean{      //实际需要的是这个类

private static Logger logger = Logger.getLogger("teshi-report");            //打印日志这很重要

@Autowired
private SfvConfigManager sfvConfigManager;

//对定时任务的时间进行重写
public void setCronExpression(String cronExpression){
logger.info("**************定时任务的时间控制***************");
String express = sfvConfigManager.getInitSfvConfig("sfv.job.report.time");      //取数据库配置的时间
if(express == null || "".equals(express)){
logger.info("the xml file config will be executed...");
super.setCronExpression(cronExpression);                                           //为空取配置时间
}else{
logger.info("the db config will be executed...");
super.setCronExpression(express);                                                       //有值就赋值
}
}
}

public class SupplierReportJob {

private static final Logger logger = Logger.getLogger("teshi-report");     //业务代码
}

        public void supplierReport() {
logger.info("report job is started ....");

}

//定时任务开关
private boolean isConfigRight() {
if("0".equals(configManager.getInitSfvConfig(SfvConstents.SFV_JOB_AUDIT_TIME))){
logger.info("the audit setting had closed ...");
return false;
}
if(!"1".equals(Settings.get(SfvConstents.AUDIT_JOB_FLAG))){
logger.info("the ip setting is not right...");
return false; //如果audit.job.flag=no就不是同步服务器
}
return true;
}



     //缓存的配置和管理

package com.sf.teshi.manager.expand;


import java.util.List;


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


import com.sf.teshi.domain.expend.SfvConfig;
import com.sf.teshi.domain.expend.SfvConstents;
import com.sf.teshi.manager.base.CacheManager;
import com.sf.teshi.mapper.read.expend.SfvConfigMapper;


/**
 * sfvConfig配置的缓存管理
 * @author sfit0480
 *
 */
@Component
public class SfvConfigManager extends CacheManager{

@Autowired
private SfvConfigMapper sfvConfigMapper;

public String getInitSfvConfig(String key){
Object cache = getCache(key);
if(cache == null){
return addCache(key);
}else{
if(SfvConstents.SFV_JOB_TIME.equals(key) || 
  SfvConstents.SFV_JOB_REPORT_TIME.equals(key) ||
  SfvConstents.SFV_JOB_PRODUCTCOST_TIME.equals(key) ||
  SfvConstents.SFV_JOB_DELIVERY_TIME.equals(key) ||
  SfvConstents.SFV_JOB_CANAL_TIME.equals(key) ||
  SfvConstents.SFV_JOB_AUDIT_TIME.equals(key)){
return addCache(key);
}
if(SfvConstents.SYNC_JOB_NUM % 2 == 0){
return addCache(key);
}
return (String) getCache(key);
}
}


private String addCache(String key) {
String cacheVal = null;
List<SfvConfig> sfvConfigs = sfvConfigMapper.selectSfvConfigs();
if(sfvConfigs == null || sfvConfigs.size() <1){
return null;
}
for (SfvConfig f : sfvConfigs) {
setCache(f.getCode(), f.getValue());
if(key.equals(f.getCode())){
cacheVal = f.getValue();
}
}
return cacheVal;
}
}


//缓存的增删改


package com.sf.teshi.manager.base;


import java.util.Date;


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


import com.danga.MemCached.MemCachedClient;
import com.sf.teshi.common.Settings;


public class CacheManager {

private static boolean OPEN = true;

static {
String isOpen = Settings.get("openCache");
if(isOpen == null || !isOpen.equals("1")) {
OPEN = false;
}
}

@Autowired
private MemCachedClient memCachedClient;

/**
* 默认缓存1小时
*/
protected boolean setCache(String key, Object value) {
if(!OPEN) {
return false;
}
return memCachedClient.set(key, value, new Date(60 * 60 * 1000));
}

protected Object getCache(String key) {
if(!OPEN) {
return null;
}
return memCachedClient.get(key);
}

protected boolean deleteCache(String key) {
return memCachedClient.delete(key);
}
}


//settings的配置

package com.sf.teshi.common;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Settings {


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

private static Properties prop = new Properties();
static {
try {
String location = TeshiHelper.getConfigPath()+"/settings.properties";    //settings的路径配置
   InputStream in = new BufferedInputStream(new FileInputStream(location)); 
    prop.load(new InputStreamReader(in));
} catch (UnsupportedEncodingException e) {
logger.error("==load setting file error==", e);
} catch (IOException e) {
logger.error("==load setting file error==", e);
}
}


public static String get(String key) {
if (prop != null && prop.containsKey(key)) {
String object = (String) prop.get(key.trim());
return object;
} else {
return null;
}
}

/*public static void main(String args []) {
System.out.println();
}*/
}


很实用很简单的小技能,总结起来,忘了的时候直接就可以用啦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值