线程定时刷新缓存
package com.banksteel.web.index.thread;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.cache.CacheService;
import cn.mysteel.util.page.PageSet;
import com.banksteel.report.service.OrderBaseDataWService;
import com.banksteel.web.index.comparator.ComfirmTimeComparator;
/**
*
* @description: 成交动态线程
* @projectName:banksteel-mall-wwwapp
* @className:RefreshDynamicOfDealThread.java
* @see: com.banksteel.web.index.thread
* @author: zhaiang
* @createTime:2015年12月30日 下午1:17:57
* @version 3.0.0
*/
@Component("refreshDynamicOfDealThread")
public class RefreshDynamicOfDealThread
{
@Autowired
private OrderBaseDataWService orderBaseDataWService; // 12.30添加 成家动态查报表
@Autowired
private CacheService cacheService;
private Thread thread = null;
public static final String DYNAMIC_DEAL_INDEXPAGE_V5 = "dynamic.deal.indexpageV5"; // 首页成交动态资源key
/**
* 初始化方法
*/
@PostConstruct
public void init()
{
thread = new ResDynamicOfDealThread();
thread.start();
}
class ResDynamicOfDealThread extends Thread
{
/**
* 使用线程保存相应的操作日志
*/
@Override
public void run()
{
while (true)
{
try
{
PageSet<Map<String, Object>> pageSet = orderBaseDataWService.querySuccessOrderDetail(null, "", "", "", "", "", 1, null, 1, 100);
if (ObjectUtils.notNull(pageSet) && ObjectUtils.notNull(pageSet.getPageData()))
{
// 根据确认时间升序进行排列
Comparator<Map<String, Object>> comparator = new ComfirmTimeComparator();
Collections.sort(pageSet.getPageData(), comparator);
cacheService.set(DYNAMIC_DEAL_INDEXPAGE_V5, pageSet.getPageData());
Thread.sleep(5000 * 60);
}
}
catch (Exception e)
{
}
}
}
}
}
ComfirmTimeComparator类
package com.banksteel.web.index.comparator;
import java.util.Comparator;
import java.util.Map;
public class ComfirmTimeComparator implements Comparator<Map<String, Object>>
{
@Override
public int compare(Map<String, Object> obj1, Map<String, Object> obj2)
{
Object confirmTime1 = obj1.get("creatTime");
Object confirmTime2 = obj2.get("creatTime");
if (confirmTime1 != null && confirmTime2 != null)
{
return confirmTime1.toString().compareTo(confirmTime2.toString());
}
return 0;
}
}
IndexTool工具(velocity)
/**
* 首页成交动态资源数据
*
* @return
*/
public List<Map<String, Object>> dynamicDeal()
{
List<Map<String, Object>> dynamicDealRes = null;
if (cacheService.exists(DYNAMIC_DEAL_INDEXPAGE_V5))
{
dynamicDealRes = cacheService.get(DYNAMIC_DEAL_INDEXPAGE_V5);
if (dynamicDealRes.size() == 0)
{
PageSet<Map<String, Object>> pageSet = orderBaseDataWService.querySuccessOrderDetail(null, "", "", "", "", "", 1, null, 1, 100);
if (ObjectUtils.notNull(pageSet) && ObjectUtils.notNull(pageSet.getPageData()))
{
// 成交动态向上滚动,先成交的要放在上面,所以根据确认时间升序进行排列
Comparator<Map<String, Object>> comparator = new ComfirmTimeComparator();
Collections.sort(pageSet.getPageData(), comparator);
cacheService.set(DYNAMIC_DEAL_INDEXPAGE_V5, pageSet.getPageData());
dynamicDealRes = pageSet.getPageData();
}
}
}
return dynamicDealRes;
}
页面
<div class="wrap-dynamic">
<div class="trend sd-topLoop">
<div class="tit hd"> <strong>成交动态</strong>
<!-- <p><span class="btn1"></span><span class="btn2"></span></p> -->
<a class="next"><i></i></a> <a class="prev"><i></i></a> </div>
<div class="ct bd">
<div class="tempWrap" style="overflow:hidden; position:relative; height:219px">
<ul style="height: 730px; position: relative; padding: 0px; margin: 0px; top: -73px;">
#set($listDynamicDealRess = $indexTool.dynamicDeal())
#foreach($data in $!listDynamicDealRess)
<li>
<span></span>
<div>
<p title="$!{data.breed} {$!data.spec} {$!data.material}">$!{data.breed} $!{data.spec} $!{data.material}</p>
<p>
<em>$!velocityTool.formatNum($!{data.price})元/吨</em>
#if($!{data.status} == 1 || $!{data.status} == 2)
<b class="red">下单</b>
#else
<b>成交</b>
#end
<label>$!velocityTool.formatTime($!{data.creatTime}, "HH:mm")</label>
</p>
</div>
</li>
#end
</ul>
</div>
</div>
</div>