/**
*
* @author liuyuzhuo
* @createTime 2019年3月7日 下午3:40:32
*/
package com.bgi.presap.intf.service.sync;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import com.bgi.service.dataQC.PresapSyncService;
/**
* @author liuyuzhuo
*
*/
@Component
public class Quartz {
@Resource
PresapSyncService presapSyncService;
private int count = 1;
private int getCount(){
this.count += 1;
return this.count -1;
}
public void init() {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setAutoStartup(true);
List<Trigger> list = new ArrayList<Trigger>();
this.addJob(list,"shuJuZhiKongResultTable2Sap","0 0/3 * * * ?",getCount());
this.addJob(list,"test","0/3 * * * * ?",getCount());
Trigger[] triggers = new Trigger[list.size()];
list.toArray(triggers);
factory.setTriggers(triggers);
factory.setSchedulerName("cronScheduler");
factory.setSchedulerFactoryClass(StdSchedulerFactory.class);
try {
factory.afterPropertiesSet();
} catch (Exception e) {
e.printStackTrace();
}
factory.start();
}
/**
* @param list
* @param method 目标方法
* @param cronExpression 执行表达式
* @param count
*/
private void addJob(List<Trigger> list,String method,String cronExpression,int count){
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
jobDetail.setTargetObject(presapSyncService);
jobDetail.setTargetMethod(method);
jobDetail.setConcurrent(false);
jobDetail.setName("job"+count);
try {
jobDetail.afterPropertiesSet();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
}
jobDetail.getObject().setName("job"+count);
jobDetail.getObject().setGroup("jgroup"+count);
CronTriggerBean trigger = new CronTriggerBean();
trigger.setJobDetail(jobDetail.getObject());
try {
trigger.setCronExpression(cronExpression);
trigger.setStartDelay(0);
} catch (ParseException e) {
e.printStackTrace();
}
trigger.setName("hello"+count);
trigger.setJobName("job"+count);//jobName和我们前面jobDetail的的名字一致
trigger.setJobGroup("jgroup"+count);//jobGroup和我们之前jobDetail的组名一致
list.add(trigger);
}
}
package com.bgi.presap.intf.service;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.bgi.base.beans.PresapFileMap;
import com.bgi.base.beans.PresapMethod;
import com.bgi.base.beans.PresapResponse;
import com.bgi.base.beans.User;
import com.bgi.base.exception.PresapException;
import com.bgi.base.intf.IPresapMethodService;
import com.bgi.base.sapcon.RfcMethodExcuteService;
import com.bgi.presap.intf.beans.CachePool;
import com.bgi.presap.intf.service.sync.Quartz;
import com.bgi.presap.intf.utils.Constants;
import com.bgi.presap.intf.utils.JedisUtil;
import redis.clients.jedis.Jedis;
@Service
public class PresapMethodFactory implements ApplicationContextAware,ApplicationListener<ContextRefreshedEvent> {
private Logger logger = Logger.getLogger("intf");
private static ApplicationContext ctx = null;
private Map<String, Object[]> presapMethodMap = null;
@SuppressWarnings("static-access")
@Override
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
this.ctx = ctx;
}
public static ApplicationContext getApplicationContext() {
return ctx;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
Quartz bean = ctx.getBean(Quartz.class);
bean.init();
}
}
定时器目标类:
/**
*
* @author liuyuzhuo
* @createTime 2019年3月2日 下午2:17:03
*/
package com.bgi.service.dataQC;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bgi.base.beans.SapResponse;
import com.bgi.base.beans.User;
import com.bgi.base.intf.IPresapMethodService;
import com.bgi.base.sapcon.SapMapExcuteService;
import com.bgi.rabbitmq.beans.DbcCommonSapMapBaseBean;
/**
* @author liuyuzhuo
* presap定时服务
*/
@Service
public class PresapSyncService {
@Resource
private SqlSessionTemplate sqlSessionTemplate;
@Resource
private SapMapExcuteService sapMapExcuteService;
@SuppressWarnings("unchecked")
@Transactional
public void test(){
System.out.println(IPresapMethodService.DATE_FORMAT_NORMAL.format(new Date()));
List<Object> selectList = sqlSessionTemplate.selectList("PresapSqlMap.getAddTestList");
for(Object o : selectList){
Map<String,Object> map = (Map<String,Object>)o;
System.out.println(map.keySet());
}
}
/**
* presap数据质控Oracle结果表数据同步到sap
*/
public void shuJuZhiKongResultTable2Sap(){
//1.查询index维度数据
List<Map<String, Object>> indexList = sqlSessionTemplate.selectList("PresapSqlMap.getUnHandleDatasOfIndex");
syncShuJuZhiKong(indexList,"pfi_id","updateIndex");
//2.查询lane维度数据
List<Map<String, Object>> laneList = sqlSessionTemplate.selectList("PresapSqlMap.getUnHandleDatasOfLane");
syncShuJuZhiKong(laneList,"pfl_id","updateLane");
}
private void syncShuJuZhiKong(List<Map<String, Object>> list,String id,String sqlId){
if(list==null || list.size()<1){
return;
}
DbcCommonSapMapBaseBean dbcCommonSapMapBaseBean=new DbcCommonSapMapBaseBean();
dbcCommonSapMapBaseBean.setSapMapList(list);
SapResponse response = sapMapExcuteService.update("UPDTASK", dbcCommonSapMapBaseBean, null, new User());
if(response.getStatus()){
//sap更新成功,更新Oracle表的处理状态
List<String> ids = new ArrayList<>();
for(Map<String,Object> map : list){
ids.add((String)map.get(id.toUpperCase()));
}
int a = sqlSessionTemplate.update("PresapSqlMap."+sqlId,ids);
System.out.println(a);
}
}
}