• 线程异步调用实例


    更新content时更新Lucene的索引

    Content newContent = contentService.update(content);
    afterContentStatusChange(newContent, ContentStatusChangeThread.OPERATE_UPDATE);

      private void afterContentStatusChange(Content content, Short operate){

      ContentStatusChangeThread afterThread = new ContentStatusChangeThread(
                    content,operate,
                    new LuceneListener());
      afterThread.start();
    }
    package com.jeecms.cms.staticpage;
    
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.transaction.TransactionDefinition;
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.support.DefaultTransactionDefinition;
    import org.springframework.util.Assert;
    import org.hibernate.Session;
    import org.springframework.orm.hibernate4.HibernateTransactionManager;
    import com.jeecms.cms.entity.main.Content;
    import com.jeecms.cms.manager.main.ContentMng;
    import com.jeecms.cms.service.ContentListener;
    import com.jeecms.cms.web.ApplicationContextUtil;
    
    public class ContentStatusChangeThread extends Thread {
        
        public static Short OPERATE_ADD=1;
        public static Short OPERATE_UPDATE=2;
        public static Short OPERATE_DEL=3;
    
        private Content content;
        //=1保存  =2修改 =3删除
        private Short operateType;
        private LuceneListener listener;public ContentStatusChangeThread(Content content,Short operateType,LuceneListener listener) {
            super();
            this.content=content;
            this.operateType=operateType;
            this.listener=listener;
        }
        
    
        @Override
        public void run() {
                HibernateTransactionManager transactionManager = (HibernateTransactionManager) ApplicationContextUtil
                .getBean("transactionManager"); DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务 TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态 try { //逻辑代码 if(operateType==1){ afterSave(content); }else if(operateType==2){ ContentMng contentMng=(ContentMng) ApplicationContextUtil.getBean("contentMng"); content=contentMng.findById(content.getId()); afterChange(content, listener); }else if(operateType==3){ afterDelete(content); } transactionManager.commit(status); } catch (Exception e) { transactionManager.rollback(status); } } private void afterSave(Content content) { if (listener != null) { listener.afterSave(content); } } private void afterChange(Content content, LuceneListener listener) { if (listener != null) { listener.afterChange(content); } } private void afterDelete(Content content) { if (listener != null) { listener.afterDelete(content); } } }
    @Override
    public void afterChange(Content content) {
        luceneContentService.updateIndex(content);
    }
  • 相关阅读:
    mysql 导入CSV数据 [转]
    Linux用户态程序计时方式详解[转]
    [转] Bash脚本:怎样一行行地读文件(最好和最坏的方法)
    第二次作业
    软件工程原理与方法 第一次作业
    2017-02-19,周日整理
    2017-02-12,周日整理
    cnblogs,第一次博客纪念。
    堆和栈的区别(转过无数次的文章)
    Flash Player版本相关问题
  • 原文地址:https://www.cnblogs.com/BINGJJFLY/p/7487615.html
Copyright © 2020-2023  润新知