com.pureinfo.tgirls.utils.counts.CountsJob.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.tgirls.utils.counts.CountsJob.java

Source

/**
 * PureInfo TGirls
 * @(#)CountsJob.java   1.0 2009-3-23
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.tgirls.utils.counts;

import java.util.Iterator;
import java.util.Set;

import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.HashBag;
import org.apache.log4j.Logger;

import com.pureinfo.dolphin.context.LocalContextHelper;
import com.pureinfo.dolphin.persister.ISession;
import com.pureinfo.tgirls.model.Counts;
import com.pureinfo.tgirls.utils.script.job.IThreadJob;

public class CountsJob implements IThreadJob {

    private Logger logger = Logger.getLogger(this.getClass().getName());

    private String id;

    public CountsJob(String _string) {
        this.id = _string;
    }

    public void execute() {
        logger.debug("to process counts.");

        CountsProcess cache = CountsProcess.getInstance();
        Bag bag = new HashBag();
        synchronized (cache) {
            bag.addAll(cache.getBag());
            cache.clear();
        }

        ISession session = null;
        try {
            session = LocalContextHelper.currentSession();
            Set<String> bagKeySet = bag.uniqueSet();
            for (Iterator<String> iterator = bagKeySet.iterator(); iterator.hasNext();) {
                String type = null;
                try {
                    type = iterator.next();
                    Counts counts = new Counts();
                    counts.setType(type);
                    counts.setCounts(bag.getCount(type));
                    counts.setCreateTime();
                    session.save(counts);
                } catch (Exception e) {
                    logger.error("error when save counts:" + type, e);
                }

            }

        } catch (Exception e) {
            logger.error("error occur.", e);
        } finally {
            bag.clear();
            if (session != null)
                try {
                    session.close();
                } catch (Exception e) {
                    logger.error("error when close session.", e);
                }
        }

    }

    public String getId() {
        return this.id;
    }

}