指定方法在指定的服务器运行

调用执行方法前先判断服务器是否有权限

@Transactional
@Service
public class FixedScanOverdueService implements IFixedScanOverdueService {
	//日志
	private Log log = LogFactory.getLog(FixedScanOverdueService.class);
	@Resource
	private IProjectRepayPlanDao projectRepayPlanDao;
	@Resource
	private IProjectDao projectDao;
	@Resource
	private IPlanZyEqInteEqPrinciService planZyEqInteEqPrinciService ;
	
	@Resource
	private ITaskExecutionDao taskExecutionDao;
	
	@Resource
	private ISequenceService sequenceService;
	
	/**定时逾期扫描*/
	public boolean process(String userId) {
		/** 指定服务器运行的方法 **/
		if(!ClusterUtils.hasExecAuthority()){
			return false;
		}
		
		log.info("******逾期扫描定时器任务启动开始******");
		TaskExecution taskExecution=new TaskExecution();
		try{
			taskExecution.setId(sequenceService.nextval(Sequence.SEQ_TASK_EXECUTION));
			taskExecution.setTaskType(TaskExecution.TaskType.overdueScan);
			taskExecution.setTaskName("逾期定时扫描");
			taskExecution.setInsertBy(userId);
			taskExecution.setSmsFlag("N");
			taskExecution.setEmailFlag("N");
			/**1.取出逾期列表数据*/
			List<ProjectRepayPlan> planlist= projectRepayPlanDao.getProjectRepayOverdueList(null, null);
			/**取出逾期的项目列表数据*/
			List<Long> projectList=getProjectIdList(planlist);
			/**还款逾期插入集合数据*/
			List<ProjectRepayPlan>  overdueList=projectRepayPlanDao.getProjectRepayOverdueInsertList(null, null);
			/**2.计算当期逾期罚息*/
			planlist=computeYqMoney(null,planlist);
			/**3.计算当期应还金额**/
			planlist=planZyEqInteEqPrinciService.step50(null,planlist);
			/**4.计算当期结清罚息*/
			planlist=computeJqYqMoneyList(planlist);
			/**5.计算当结清金额 */
			planlist=planZyEqInteEqPrinciService.step60(null,planlist);
			/**6.剩余实际应还金额(实际应还金额 - 实还金额) */
			planlist=computeSyMoney(planlist);
			/**7.更新还款计划中的逾期天数,逾期罚息,应还金额,结清罚息,当期结清金额,剩余实际应还金额*/
			if(null!=planlist&&planlist.size()>0){
				projectRepayPlanDao.batchUpdateOverdueRepayPlan(planlist);
			}
			/**8.更新项目逾期流程处理*/
			if(null!=projectList&&projectList.size()>0){
				 projectDao.batchUpdateProjectStatus(projectList,ChainStorePropertyLoanStatusEnum.OVERDUE.toString());
			}
			/**9.批量增加还款逾期数据*/
			if(null!=overdueList&&overdueList.size()>0){
				projectRepayPlanDao.batchInsertRepayOverdue(overdueList);
			}
			taskExecution.setStatus("SUCCESS");
			log.info("******逾期扫定时器任务启动完成******");
		}catch(Exception e){
			taskExecution.setStatus("FAIL");
			taskExecution.setRemark("逾期扫描定时任务执行失败:"+e.getMessage());
			log.info("******逾期扫定时器任务出现异常******");
			log.error("", e);
			throw new RuntimeException("逾期定时器执行失败");
		}finally{
			taskExecutionDao.insert(taskExecution);
		}
		return true;
	}

 

appliaction-frame.xml中配置

<!-- 集群环境配置 -->
	<bean class="com.shareinfo.commons.utils.ClusterUtils">
		<!-- 配置某方法只能在指定的服务器中调用 -->
		<property name="authorityMap">
			<map>
				<entry key="com.shareinfo.customer.service.impl.FixedScanOverdueService.process" value="192.168.1.100" />
			</map>
		</property>
	</bean>
	<!-- END 集群环境配置 -->

 集群工具类:

public class ClusterUtils {
	
	private static Log log = LogFactory.getLog(ClusterUtils.class);
	
	/**
	 * 存储服务器已授权的可执行方法(applicationContext-frame.xml中配置)
	 */
	private static Map<String, String> authorityMap;
	
	public static void setAuthorityMap(Map<String, String> authorityMap) {
		ClusterUtils.authorityMap = authorityMap;
	}

	/**
	 * 判断当前服务器是否有权限执行当前方法(true:有权限)
	 * (applicationContext-frame.xml中配置)
	 * @Title: hasExecAuthority
	 * @Description: 
	 * @author baolin.liu 
	 * @date 2015-8-31 下午04:12:09
	 * @return    
	 * @return boolean
	 */
	public static boolean hasExecAuthority() {
		StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace();
		String key = stackTraces[2].getClassName() + "." + stackTraces[2].getMethodName();
		String localHostAddress = NetworkUtils.localHostAddress();

		if (authorityMap.containsKey(key)) {
			String values = authorityMap.get(key);
			if (values != null && !values.contains(localHostAddress)) {
				log.info("当前服务器放弃执行:" + key + " 当前服务器ip[" + localHostAddress + "]" + " 目标服务器ip[" + values + "]");
				return false;
			}
		}

		log.info("当前服务器允许执行:" + key + " 当前服务器ip[" + localHostAddress + "]");
		return true;
	}
	
}

 

 

你可能感兴趣的:(指定方法在指定的服务器运行)