Browse Source

添加计算专家平均星级的定时任务,每天晚上2点更新专家平均星级。

zzy.zhiyuan.foxmail 8 years ago
parent
commit
d1f934c1af

+ 32 - 0
src/main/java/com/ekexiu/portal/job/TaskJob.java

@ -0,0 +1,32 @@
1
package com.ekexiu.portal.job;
2

3
import java.text.SimpleDateFormat;
4
import java.util.Date;
5
import java.util.concurrent.Executors;
6
import java.util.concurrent.ScheduledExecutorService;
7
import java.util.concurrent.TimeUnit;
8

9
import org.jfw.apt.annotation.Bean;
10
import org.jfw.util.bean.AfterBeanFactory;
11
import org.jfw.util.bean.BeanFactory;
12

13
@Bean
14
public class TaskJob implements AfterBeanFactory {
15
	
16
	@Override
17
	public void handle(BeanFactory bf) throws Throwable {
18
	
19
		TaskJobEntry tje = (TaskJobEntry) bf.getBean("com_ekexiu_portal_job_TaskJobEntry");
20
		long delayTime = tje.getDelayTime();
21
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
22
		Date date = sdf.parse(tje.getTaskTime());
23
		long task = date.getTime();
24
		long taskTime = task - System.currentTimeMillis();
25
		taskTime = taskTime > 0 ? taskTime : delayTime + taskTime;
26
		
27
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
28
        // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间  
29
//        service.scheduleAtFixedRate(tje, tje.getStartTime(), tje.getIntervalTime(), TimeUnit.SECONDS);
30
        service.scheduleAtFixedRate(tje, taskTime, delayTime, TimeUnit.MILLISECONDS);
31
	} 
32
}

+ 69 - 0
src/main/java/com/ekexiu/portal/job/TaskJobEntry.java

@ -0,0 +1,69 @@
1
package com.ekexiu.portal.job;
2

3
import java.sql.Connection;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6

7
import javax.sql.DataSource;
8

9
import org.apache.log4j.Logger;
10
import org.jfw.apt.annotation.Autowrie;
11
import org.jfw.apt.annotation.Bean;
12

13
import com.ekexiu.portal.service.ProfessorService;
14
@Bean
15
public class TaskJobEntry implements Runnable{
16
	private static Logger logger = Logger.getLogger(TaskJobEntry.class);
17
	private String taskTime;
18
	private long delayTime;
19
	@Autowrie("dataSource")
20
	private DataSource dataSource;
21
	@Autowrie
22
	private ProfessorService professorService;
23
	public DataSource getDataSource() {
24
		return dataSource;
25
	}
26
	public void setDataSource(DataSource dataSource) {
27
		this.dataSource = dataSource;
28
	}
29
	public ProfessorService getProfessorService() {
30
		return professorService;
31
	}
32
	public void setProfessorService(ProfessorService professorService) {
33
		this.professorService = professorService;
34
	}
35
	public String getTaskTime() {
36
		return taskTime;
37
	}
38
	public void setTaskTime(String taskTime) {
39
		this.taskTime = taskTime;
40
	}
41
	public long getDelayTime() {
42
		return delayTime;
43
	}
44
	public void setDelayTime(long delayTime) {
45
		this.delayTime = delayTime;
46
	}
47
	@Override
48
	public void run() {
49
		try {
50
			Connection con =dataSource.getConnection();
51
			try{
52
				professorService.updateStarAvg(con);
53
                con.commit();
54
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
55
                logger.info("执行定时任务,更新专家平均星级"+df.format(new Date()));
56
            }catch(Exception e){
57
                try{con.rollback();
58
                }catch(Exception e1){e1.printStackTrace();}
59
            }finally{
60
                try{con.close();
61
                }catch(Exception e){e.printStackTrace();} 
62
            }
63
		} catch (Exception e) {
64
			e.printStackTrace();
65
		}
66
		
67
	}
68
	
69
}

+ 1 - 3
src/main/java/com/ekexiu/portal/service/ProfessorService.java

@ -258,7 +258,7 @@ public class ProfessorService {
258 258
		return this.professorDao.query(con);
259 259
	}
260 260
	
261
	public boolean updateStarAvg(Connection con) throws SQLException {
261
	public void updateStarAvg(Connection con) throws SQLException {
262 262
		try {
263 263
			List<Professor> professors = this.professorDao.queryStar(con);
264 264
			BigDecimal bigStar = this.professorDao.queryBigStar(con);
@ -271,11 +271,9 @@ public class ProfessorService {
271 271
				BigDecimal avgStar = this.calculate.calcAvgStar(bigStar, smallStar, bigConsult, smallConsult, star, consult);
272 272
				this.professorDao.updateStarAvg(con, professor.getId(), avgStar);
273 273
			}
274
			return true;
275 274
		} catch (Exception e) {
276 275
			con.rollback();
277 276
			e.printStackTrace();
278
			return false;
279 277
		}
280 278
	}
281 279
	

+ 4 - 3
src/main/java/com/ekexiu/portal/util/Calculate.java

@ -6,8 +6,8 @@ import org.jfw.apt.annotation.Bean;
6 6

7 7
@Bean
8 8
public class Calculate {
9
	private double starWeight = 0.5;
10
	private double consultWeight = 0.5;
9
	private double starWeight;
10
	private double consultWeight;
11 11
	
12 12
	public double getStarWeight() {
13 13
		return starWeight;
@ -57,7 +57,8 @@ public class Calculate {
57 57
	}
58 58
	
59 59
	public static BigDecimal div(BigDecimal d1,BigDecimal d2) {
60
		return d1.divide(d2);
60
		//指定除法计算结果的精度为2,四舍五入。
61
		return d1.divide(d2, 2, BigDecimal.ROUND_HALF_UP);
61 62
	}
62 63
	
63 64
}