设置权限码在一定时间内失效
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ConcurrentHashMap;
import javax.swing.Timer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.lamb.domain.Entity;
public class AuthCode {
private static Logger logger = LoggerFactory.getLogger(AuthCode.class);
public static boolean DEBUG = true;
private ConcurrentHashMap<String,Entity > changeAuthCodeMap = new ConcurrentHashMap<String, Entity>();
public static final int CHECH_INTERVAL = 60000; //定时器的时间间隔
public static final int SIGN_APPLY_TIMEOUT = 600000; //唯一码超时时间,10分钟
private static AuthCode instance = null;
private boolean running = false;
private boolean _excuting = false;
private Timer timer = null;
public static AuthCode getInstance(){
if (instance == null)
instance = new AuthCode();
return instance;
}
private AuthCode(){
init();
}
private void init()
{
this.timer = new Timer(CHECH_INTERVAL, new ActionListener()
{
public void actionPerformed(ActionEvent e) {
AuthCode.this.run();
}
});
}
public void run(){
// 极端情况下本次循环内可能执行时间超过了时间间隔,此处是防止在前一
// 次还没有运行完的情况下又重复执行,从而出现无法预知的错误
if (!this._excuting){
this._excuting = true;
try{
// if (DEBUG) {
// logger.info("====== 执照更换手机的检查线程运行中, 当前需要处理的列表长度为" + this.changeAuthCodeMap.size() + "...");
// }
for (String key : this.changeAuthCodeMap.keySet()){
Entity entity = (Entity)this.changeAuthCodeMap.get(key);
if(entity!=null){
long period = System.currentTimeMillis() - entity.getStartTime();
if(period >= SIGN_APPLY_TIMEOUT){
if (DEBUG) {
logger.info("=========== 唯一标识为"+key+"所对应的请求,等待移动端响应超时了...");
}
remove(key);
}
}else{
remove(key);
}
}
}catch (Exception eee){
eee.printStackTrace();
System.out.println("-------------------------------");
System.out.println("-------检查执照更换手机的定时器执行出错!!!--------");
System.out.println("-------------------------------");
}
this._excuting = false;
}
}
public AuthCode startup(boolean immediately)
{
stop();
if (immediately)
this.timer.setInitialDelay(0);
else
this.timer.setInitialDelay(CHECH_INTERVAL);
this.timer.start();
this.running = true;
return this;
}
public void stop()
{
if (this.timer != null) {
this.timer.stop();
}
this.running = false;
}
public boolean isRunning()
{
return this.running;
}
public boolean exist(String random)
{
return this.changeAuthCodeMap.get(random) != null;
}
public Entity getEntity(String random){
return this.changeAuthCodeMap.get(random);
}
public void put(String random, Entity s)
{
if (s == null)
{
if (DEBUG)
logger.warn("Invalid arg s==null.");
return;
}
if (s.getOrgCode() == null)
{
if (DEBUG)
logger.warn("Invalid arg s.getOrgCode() == null.");
return;
}
if (s.getOldMobileIdentify() == null)
{
if (DEBUG)
logger.warn("Invalid arg s.getMobileIdentify() == null.");
return;
}
if (this.changeAuthCodeMap.get(random) != null)
{
if (DEBUG) {
logger.warn("唯一码为" + random + "的更换手机请求已经放入了队列,该请求为何会重复?(生成的唯一码重复?还是重复put?)");
}
}
// save it
changeAuthCodeMap.put(random, s);
}
public void remove(String random){
try{
// remove it
Entity entity = changeAuthCodeMap.remove(random);
if(DEBUG)
logger.info("唯一码为"+random+"的更换手机请求已成功从队列中移除(可能是移动端已响应 或者 请求超时)," +
"更换手机请求的信息为:对应企业名称为:"+entity.getCompanyName()+" :企业统一代码:"+entity.getOrgCode());
}catch (Exception e){
if(DEBUG)
logger.info("删除map中的协同请求信息remove(random)时出错了:", e);
}
}
public ConcurrentHashMap<String, Entity> getentityileReqMap(){
return this.changeAuthCodeMap;
}
public int size()
{
return this.changeAuthCodeMap.size();
}
}