job接口
***************************************************
package com.sangame.cms.job.base;
public abstract class BaseJob {
public abstract void execute() throws Exception ;
}
session 事務(wù)
***************************************************
package com.sangame.cms.job.base;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import com.sangame.cms.web.listener.SpringBeanManager;
public abstract class HibernateAbstractJob extends BaseJob {
public abstract void executeJob() throws Exception;
@Override
public void execute() throws Exception {
try {
SessionFactory sessionFactory = (SessionFactory) SpringBeanManager.getBean("cmsSessionFactory");
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.MANUAL);
if(!TransactionSynchronizationManager.hasResource(sessionFactory)) {
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
executeJob();
session.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(session);
} catch (Exception e) {
e.printStackTrace();
}
}
}
executeJob(); 功能實(shí)現(xiàn)
***************************************************
package com.sangame.cms.job;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import com.sangame.cms.constant.EnumConstants.Flag;
import com.sangame.cms.job.base.HibernateAbstractJob;
import com.sangame.cms.persistence.model.core.Doc;
import com.sangame.cms.persistence.model.plus.IncludeCheck;
import com.sangame.cms.service.core.DocService;
import com.sangame.cms.service.plus.IncludeCheckService;
import com.sangame.cms.util.DateUtil;
import com.sangame.cms.util.StrUtils;
/**
*
* @author hasau
* 2010-9-15 下午07:17:10
*/
@Service
public class IncludeCheckJob extends HibernateAbstractJob {
private static final int PAGESIZE =10;
@Resource
private DocService docService;
@Resource
private IncludeCheckService includeCheckService;
@Override
public void executeJob() throws Exception {
int pageCount = docService.getDocTotalPageCountNum( PAGESIZE).intValue();
if (pageCount <1) return ;
List<Doc> docs=null;
for(int i=1;i<pageCount+1 ;i++){
docs = docService.getPageDocs(i, PAGESIZE);
for(Doc doc: docs){
try {
includeCheck(doc);
} catch (Exception e) {
continue;
}
}
}
}
private void includeCheck(Doc doc ) {
if(doc==null || doc.getId()==null) return ;
IncludeCheck check = includeCheckService.getByDocId(doc.getId());
if(StringUtils.isEmpty(doc.url())){
saveOrUpdateIncludeCheck( null,Flag.FALSE.getValue(), doc,check);
}
try {
String url = URLEncoder.encode(doc.url(), "GBK");
Document page = Jsoup.parse(new URL("
Elements els = page.select("table[class=result]");
if(els.isEmpty()){
saveOrUpdateIncludeCheck(null, Flag.FALSE.getValue(), doc,check);
}
Elements fonts = els.get(0).select("font[color=#008000]");
if(fonts.isEmpty()){
saveOrUpdateIncludeCheck(null, Flag.FALSE.getValue(), doc,check);
}
String text = fonts.get(0).text().trim();
if(text.lastIndexOf(StrUtils.getHttpString(doc.url(),"http://","/"))!=-1){
String dateString = text.substring(text.lastIndexOf(" ")).trim();
saveOrUpdateIncludeCheck(dateString, Flag.TRUE.getValue(), doc,check);
}else{
saveOrUpdateIncludeCheck(null, Flag.FALSE.getValue(), doc,check);
}
} catch (Exception e) {
saveOrUpdateIncludeCheck(null,Flag.FALSE.getValue(), doc,check);
}
}
private void saveOrUpdateIncludeCheck(String dateString,Long flag,Doc doc,IncludeCheck check){
if(check==null || check.getId()==null) check = new IncludeCheck();
check.setBaiduIncludeTime(DateUtil.getDate(dateString));
check.setBaiduCheckTime(new Date());
check.setDoc(doc);
check.setBaiduIncludeFlag(flag);
includeCheckService.save(check);
}
}
***************************************************