1 package org.kuali.mobility.news.dao;
2
3 import org.kuali.mobility.news.entity.NewsSource;
4 import org.kuali.mobility.news.service.NewsService;
5
6 public class NewsInitBean {
7
8 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(NewsInitBean.class);
9
10 private NewsDao dao;
11 private NewsCache cache;
12
13 private static Thread backgroundThread = null;
14
15 public void init() {
16 backgroundThread = new Thread(new BackgroundThread());
17 backgroundThread.setDaemon(true);
18 backgroundThread.start();
19 }
20
21 public void cleanup() {
22 LOG.info("Cleaning up news.");
23 }
24
25
26
27
28 public NewsDao getDao() {
29 return dao;
30 }
31
32
33
34
35 public void setDao(NewsDao dao) {
36 this.dao = dao;
37 }
38
39
40
41
42 public NewsCache getCache() {
43 return cache;
44 }
45
46
47
48
49 public void setCache(NewsCache cache) {
50 this.cache = cache;
51 }
52
53 private class BackgroundThread implements Runnable {
54 public void run() {
55 try {
56 LOG.info("Initializing news...");
57 getDao().findAllActiveNewsSources();
58 LOG.info("Finished initializing news.");
59 } catch( Exception e ) {
60 LOG.error( "Failed to load news feeds.", e );
61 }
62 while (true) {
63 try {
64 LOG.info("News sleeping...");
65 try {
66 Thread.sleep(1000 * 60 * 15);
67 } catch (InterruptedException e) {
68 LOG.error(e.getMessage(), e);
69 }
70 LOG.info("Refreshing news...");
71 for (NewsSource newsSource : getDao().findAllActiveNewsSources()) {
72 getCache().updateCache(newsSource);
73 }
74 LOG.info("Finished refreshing news.");
75 } catch (Exception e) {
76 LOG.error(e.getMessage(), e);
77 }
78 }
79 }
80 }
81 }
82
83
84