package com.eactive.eai.rms.onl.common.service;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommonCommand;

public class AccessTokenSyncJob implements Job{
    /**
    * Logger for this class
    */
    private static final Logger logger = Logger.getLogger(AccessTokenSyncJob.class);

    /** The agentUtil service. */
    @Autowired
    @Qualifier("onlAgentUtilService")
    public AgentUtilService agentUtilService;

    public AccessTokenSyncJob (){
    	
    }
    
    //@ScheduleMethod(type = ScheduleType.CRON, cronExpression = "0/10 * * * * ?")
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
       // JobKey jobKey = context.getJobDetail().getKey();

		ApplicationContext appContext = null;
		try {
			appContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
		} catch (SchedulerException e) {
			logger.error("applicationContext get module error",e);
			return ;
		}
		agentUtilService = (AgentUtilService)appContext.getBean("onlAgentUtilService");
	
		try {
			// 스케줄러에서는 각 ONL 서버로 토큰 확인을 위한 호출만 한다. 
			Command command = new CommonCommand("com.openbanking.eai.agent.token.ReloadAccessTokenCommand");
			agentUtilService.broadcast(command);
		} catch(Exception e) {
			logger.error("토큰 동기화 스케쥴 실패 : " + e.toString());
		}
	}    
}
