Java tutorial
/** * Copyright mysoft Limited (c) 2014. All rights reserved. * This software is proprietary to and embodies the confidential * technology of mysoft Limited. Possession, use, or copying * of this software and media is authorized only pursuant to a * valid written license from mysoft or an authorized sublicensor. */ package com.mysoft.b2b.event.scheduler.job; import java.util.Date; import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import com.mysoft.b2b.commons.scheduler.MysoftJob; import com.mysoft.b2b.event.api.app.App; import com.mysoft.b2b.event.api.app.AppCriteria; import com.mysoft.b2b.event.api.app.AppService; import com.mysoft.b2b.event.api.app.ProtocolType; import com.mysoft.b2b.event.api.event.Event; import com.mysoft.b2b.event.api.event.EventLog; import com.mysoft.b2b.event.api.event.EventLogCriteria; import com.mysoft.b2b.event.api.event.EventLogStatus; import com.mysoft.b2b.event.api.event.EventService; import com.mysoft.b2b.event.scheduler.protocol.rmi.RMIClientHelper; import com.mysoft.b2b.event.scheduler.util.CustomizedPropertyConfigurer; /** * chengp: ?? * @version Revision History * <pre> * Author Version Date Changes * chengp 1.0 2014828 Create * hedx 1.0 2014918 Change * </pre> * @since b2b 2.0.0 */ public class EventLogJob extends MysoftJob { private static Logger log = Logger.getLogger(EventLogJob.class); @Autowired private EventService eventService; @Autowired private AppService appService; @Override public void run() { log.info("??-->......."); List<EventLog> eventLogs = this.getEventLogs(); int total = CollectionUtils.isEmpty(eventLogs) ? 0 : eventLogs.size(); log.info("??-->???:" + total); if (0 == total) { log.info("??-->???"); return; } for (int i = 0; i < eventLogs.size(); i++) { log.info("??-->" + (i + 1) + "?"); EventLog eventLog = eventLogs.get(i); try { //EventLog? this.startToDealJob(eventLog); //?? App app = this.getAppById(eventLog.getAppId()); if (null == app) { continue; } if (ProtocolType.RMI.getValue() == app.getProtocolType()) { Event event = eventService.getEvent(eventLog.getEventId()); RMIClientHelper.getRMIProtocolService(app.getProtocolAddr()).dealEvent(event); } this.finishToDealJob(true, eventLog, null); } catch (Exception e) { this.finishToDealJob(false, eventLog, e); } } } /** * ????? * @return list */ public List<EventLog> getEventLogs() { EventLogCriteria criteria = new EventLogCriteria(); criteria.setLastDealtTime(new Date()); //tryTime String tryTimes = (String) CustomizedPropertyConfigurer.getContextProperty("tryTimes"); try { Integer tryTime = Integer.valueOf(tryTimes); criteria.setTryTimes(tryTime); } catch (Exception e) { // TODO: handle exception criteria.setTryTimes(10); } return eventService.getNotExecuteEventLog(criteria); } /** * ?app * @param appid */ private App getAppById(String appId) { if (StringUtils.isNotEmpty(appId)) { AppCriteria criteria = new AppCriteria(); criteria.setAppId(appId); criteria = appService.getAppList(criteria); if (!CollectionUtils.isEmpty(criteria.getList())) { return criteria.getList().get(0); } } return null; } /** * log? * @param */ private void updateEventLog(EventLog eventLog, EventLogStatus status) { if (null != eventLog && null != status) { eventLog.setStatus(status.getValue()); eventLog.setLastDealtTime(new Date()); eventService.updateEventLog(eventLog); } } /** * ??log?? * @param event */ private void startToDealJob(EventLog eventLog) { if (null != eventLog) { this.updateEventLog(eventLog, EventLogStatus.DEALING); } } /** * ??log?? * @param flag * @param event * @param e */ private void finishToDealJob(boolean flag, EventLog eventLog, Exception e) { if (null != eventLog) { if (flag) { eventLog.setRemark("???!"); this.updateEventLog(eventLog, EventLogStatus.SUCCESS); } else { if (e != null) { String message = e.getMessage(); if (message.length() > 200) { message = message.substring(0, 200); } eventLog.setRemark(message); } else { eventLog.setRemark("??!"); } eventLog.setTryTimes(eventLog.getTryTimes() + 1); this.updateEventLog(eventLog, EventLogStatus.FAILED); } } } }