java 单例模式+ Executors.newScheduledThreadPool线程池实现定时更新公用部分缓存数据
实现这个功能为什么会带上单例模式, 单例用在这类功能实现,能降低内存消耗,所有涉及到的参数只需要申明一次。
public static AAAAYulebaoShareSingleton getInstance(){ if(instance == null){ instance = new AAAAYulebaoShareSingleton(); instance.infoFirstPageJSONObject=new JSONObject(); instance.shareJSONArray=new JSONArray(); //资源首页数据 instance.resourceFirstPageJSONObject=new JSONObject(); //渠道首页数据 instance.qudaoFirstPageJSONObject=new JSONObject(); instance.memberPriceSheetJSONArray=new JSONArray(); instance.userTokenObject=new JSONObject(); instance.alipayOnlineBuyDictionary=new JSONObject(); instance.EchoServer=new EchoServer(); //间隔1分钟更新一次公用缓存执行 instance. executor = Executors.newScheduledThreadPool(1); instance.executor.scheduleAtFixedRate( instance.EchoServer, 0, 60000, TimeUnit.MILLISECONDS); //下面两个参数作用是对比判断是否当前是第二天,因为有隔天更新的数据 instance. secondDayUpdateData=DateFormatUtils.format(new Date(), "yyyy-MM-dd"); instance.secondDayUpdateDataTemp=instance. secondDayUpdateData; return instance; } else { return instance; } } public static class EchoServer implements Runnable { @Override public void run() { synchronized (EchoServer.class) { if( !instance.infoFirstPageJSONObject.isNull("rows")) { instance.mysqlParameter=new String[]{}; instance.shareJSONArray=MySqlHepler2.getInstance() .executeQueryT("select count(*) as count from publishProductShareInfoSheet",instance.mysqlParameter); instance.resourceFirstPageJSONObject=instance.shareJSONArray.getJSONObject(0); instance.shareInt =instance.resourceFirstPageJSONObject.getInt("count"); instance.mysqlParameter=new String[]{"已通过","1", "10"}; instance.shareJSONArray=new JSONArray(); instance.shareJSONArray= MySqlHepler2.getInstance().executeQueryT("SELECT * FROM publishProductShareInfoSheet where verifyStatus=? ORDER BY sourceVip DESC, whetherSetTop DESC, id DESC limit ?,?",instance.mysqlParameter); instance.resourceFirstPageJSONObject=new JSONObject(); instance.resourceFirstPageJSONObject.put("results", instance.shareInt); instance.resourceFirstPageJSONObject.put("rows", instance.shareJSONArray); instance.resourceFirstPageJSONObject.put("status", "success"); instance.resourceFirstPageJSONObject.put("msg", "查询成功"); instance.mysqlParameter=new String[]{}; instance.shareJSONArray=MySqlHepler2.getInstance() .executeQueryT("select count(*) as count from qudaoShareSheet",instance.mysqlParameter); instance.qudaoFirstPageJSONObject=instance.shareJSONArray.getJSONObject(0); instance.shareInt =instance.qudaoFirstPageJSONObject.getInt("count"); instance.mysqlParameter=new String[]{"已通过","1", "10"}; instance.shareJSONArray=new JSONArray(); instance.shareJSONArray= MySqlHepler2.getInstance().executeQueryT("SELECT * FROM qudaoShareSheet where verifyStatus=? ORDER BY qudaoVip DESC, whetherSetTop DESC, id DESC limit ?,?",instance.mysqlParameter); instance.qudaoFirstPageJSONObject=new JSONObject(); instance.qudaoFirstPageJSONObject.put("results", instance.shareInt); instance.qudaoFirstPageJSONObject.put("rows", instance.shareJSONArray); instance.qudaoFirstPageJSONObject.put("status", "success"); instance.qudaoFirstPageJSONObject.put("msg", "查询成功"); //渠道首页数据 instance.mysqlParameter=new String[]{"最新资讯"}; instance.shareJSONArray=MySqlHepler2.getInstance() .executeQueryT("select count(*) as count from articleSheet where articleType=?",instance.mysqlParameter); instance.infoFirstPageJSONObject=instance.shareJSONArray.getJSONObject(0); instance.shareInt =instance.infoFirstPageJSONObject.getInt("count"); instance.mysqlParameter=new String[]{"最新资讯","0", "10"}; instance.shareJSONArray= MySqlHepler2.getInstance().executeQueryT("select * from articleSheet where articleType=? order by id desc limit ?,?",instance.mysqlParameter); instance.infoFirstPageJSONObject=new JSONObject(); instance.infoFirstPageJSONObject.put("results", instance.shareInt); instance.infoFirstPageJSONObject.put("rows", instance.shareJSONArray); instance.infoFirstPageJSONObject.put("status", "success"); instance.infoFirstPageJSONObject.put("msg", "查询成功"); //渠道首页数据 instance.mysqlParameter=new String[]{"常见问题"}; instance.shareJSONArray=MySqlHepler2.getInstance() .executeQueryT("select count(*) as count from articleSheet where articleType=?",instance.mysqlParameter); instance.shareJsonObject=instance.shareJSONArray.getJSONObject(0); instance.shareInt =instance.shareJsonObject.getInt("count"); instance.mysqlParameter=new String[]{"常见问题","0", "10"}; instance.shareJSONArray= MySqlHepler2.getInstance().executeQueryT("select * from articleSheet where articleType=? order by id desc limit ?,?",instance.mysqlParameter); instance.infoFirstPageJSONObject.put("results1", instance.shareInt); instance.infoFirstPageJSONObject.put("rows1", instance.shareJSONArray); //联系方式 instance.mysqlParameter=new String[]{}; instance.shareJSONArray=MySqlHepler2.getInstance() .executeQueryT("select * from officialContactSheet",instance.mysqlParameter); instance.infoFirstPageJSONObject.put("results2", instance.shareJSONArray.length()); instance.infoFirstPageJSONObject.put("rows2", instance.shareJSONArray); instance.mysqlParameter=new String[]{}; instance.memberPriceSheetJSONArray=MySqlHepler2.getInstance() .executeQueryT("select * from memberPriceSheet",instance.mysqlParameter); //置顶失效过期 instance. finishTimes = System.currentTimeMillis(); instance.mysqlParameter=new String[] {String.valueOf(instance. finishTimes)}; MySqlHepler2.getInstance().executeUpdate("update publishProductShareInfoSheet set whetherSetTop=0 where setTopFinishLongTimes<? and whetherSetTop=1",instance.mysqlParameter); MySqlHepler2.getInstance().executeUpdate("update qudaoShareSheet set whetherSetTop=0 where setTopFinishLongTimes<? and whetherSetTop=1",instance.mysqlParameter); //用户每间隔10分钟扫描一次,会员到期的用户 instance.mysqlParameter=new String[] {"none","none","none","0","资源用户", String.valueOf(instance. finishTimes),"1"}; MySqlHepler2.getInstance().executeUpdate("update userSheet set resourceMember=?,resourceMemberBeginData=?,resourceMemberFinishData=?,resourceMemberFinishDataLongData=?,qudaoMemberType=? where resourceMemberFinishDataLongData<? and resourceMember=?",instance.mysqlParameter); instance.mysqlParameter=new String[] {"none","none","none","0","渠道用户", String.valueOf(instance. finishTimes),"1"}; MySqlHepler2.getInstance().executeUpdate("update userSheet set qudaoMember=?,qudaoMemberBeginData=?,qudaoMemberFinishData=?,qudaoMemberFinishDataLongData=?,qudaoMemberType=? where qudaoMemberFinishDataLongData<? and qudaoMember=?",instance.mysqlParameter); String NowData=DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm"); //如果时间年月日不等于缓存字段中的年月日,说明到了第二天,执行隔天更新功能 instance.secondDayUpdateDataTemp=DateFormatUtils.format(new Date(), "yyyy-MM-dd"); if(!instance.secondDayUpdateDataTemp.equals(instance.secondDayUpdateData)) { AAAAYulebaoShareSingleton.getInstance().userRegisterIpDictionary=new JSONObject(); AAAAYulebaoShareSingleton.getInstance().userRegisterIpDictionary.put("192.168.0.1", "none"); for(int i=0;i<instance.memberPriceSheetJSONArray.length();i++) { instance.shareJsonObject=instance.memberPriceSheetJSONArray.getJSONObject(i); instance. limitPublishTimes=instance.shareJsonObject.getString("PublishEveryDay"); instance. limitLookTimes=instance.shareJsonObject.getString("lookTimesEveryDay"); instance. type=instance.shareJsonObject.getString("vipType"); instance.mysqlParameter=new String[]{ instance.limitLookTimes,instance.limitPublishTimes,instance.type}; if(instance.type.contains("资源")) { MySqlHepler2.getInstance().executeUpdateSingleton("update userSheet set todaySurplusResourceLookTimes=?,todaySurplusResourcePublishTimes=? where resourceMemberType=? ", instance.mysqlParameter); } if(instance.type.contains("渠道")) { MySqlHepler2.getInstance().executeUpdateSingleton("update userSheet set todaySurplusQudaoLookTimes=?,todaySurplusQudaoPublishTimes=? where qudaoMemberType=? ", instance.mysqlParameter); } } instance. mysqlParameter=new String[]{"第二天凌晨12:00执行",NowData}; MySqlHepler2.getInstance().executeUpdate("insert into tempDemoSheet(runmethord,runtimes) values (?,?)", instance. mysqlParameter); } else { instance. mysqlParameter=new String[]{"进入一次60秒执行一次",NowData}; MySqlHepler2.getInstance().executeUpdate("insert into tempDemoSheet(runmethord,runtimes) values (?,?)", instance. mysqlParameter); } } } } }
之前一直用timer+单例控制,在本机系统上间隔更新时间是准确的,但是上传服务器偶尔会出现1分钟跑两次,比如我间隔时间改为1分钟执行一次,偶尔会发现有两次执行记录,一直没管它,改成当前的方式正常了。