View Javadoc

1   package org.kuali.mobility.bus.dao;
2   
3   import org.kuali.mobility.bus.dao.BusDao;
4   import org.kuali.mobility.bus.service.BusService;
5   
6   
7   /**
8    * @author xinfeng
9    *
10   */
11  public class BusInitBean {
12  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusInitBean.class);
13  	private BusService busService;
14  	private int timePeriodInSec1;
15  	private int timePeriodInSec2;
16  	private static Thread backgroundThread1 = null;
17  	private static Thread backgroundThread2 = null;
18  
19  	public void init() {
20  		
21  		// Only start if we requested
22  		if (timePeriodInSec1 > 0){
23  			backgroundThread1 = new Thread(new BackgroundThread1());
24  	    	backgroundThread1.setDaemon(true);
25  	    	backgroundThread1.start();
26  		}
27  		
28  		// Only start if we requested
29  		if (timePeriodInSec2 > 0){
30  	    	//
31  	    	backgroundThread2 = new Thread(new BackgroundThread2());
32  	    	backgroundThread2.setDaemon(true);
33  	    	backgroundThread2.start();
34  		}
35  	}
36  
37      public void cleanup() {
38      	LOG.info("Cleaning up bus data.");
39      }
40  
41  	public BusService getBusService() {
42  		return busService;
43  	}
44  
45  	public void setBusService(BusService busService) {
46  		this.busService = busService;
47  	}
48  
49      public int getTimePeriodInSec1() {
50  		return timePeriodInSec1;
51  	}
52  
53  	public void setTimePeriodInSec1(int timePeriodInSec1) {
54  		this.timePeriodInSec1 = timePeriodInSec1;
55  	}
56  
57  
58  	 public int getTimePeriodInSec2() {
59  			return timePeriodInSec2;
60  		}
61  
62  		public void setTimePeriodInSec2(int timePeriodInSec2) {
63  			this.timePeriodInSec2 = timePeriodInSec2;
64  		}
65  
66  	// TODO - Use spring timers instead of managing own spawned threads
67  	private class BackgroundThread1 implements Runnable {
68  
69          public void run() {
70          	busService.getDao().loadBusLocations();
71          	LOG.info("Finished initializing bus locations data ..");
72          	while (true) {
73          		try {
74          			LOG.debug("Bus sleeping...");
75          			try {
76          				Thread.sleep(1000 * timePeriodInSec1);
77          			} catch (InterruptedException e) {
78          				LOG.error(e.getMessage(), e);
79          			}
80  
81          			//LOG.info("Refreshing bus location data...");
82          			busService.getDao().loadBusLocations();
83          			busService.getDao().loadAlerts();
84          			LOG.debug("Finished refreshing bus location data.");
85  
86          		} catch (Exception e) {
87          			LOG.error(e.getMessage(), e);
88          		}
89          	}
90          }
91  
92      }
93  	
94  	public void updateBusLocations(){
95      	busService.getDao().loadBusLocations();
96  		busService.getDao().loadAlerts();
97  		LOG.info("SPRING Task: Finished refreshing bus location data.");
98  	}
99  	
100 	// TODO - Use spring timers instead of managing own spawned threads
101 	private class BackgroundThread2 implements Runnable {
102         public void run() {
103         	LOG.info("Initializing bus routes data...");
104         	busService.getDao().loadRoutes();
105         	LOG.info("Finished initializing bus Routes data.");
106         	while (true) {
107         		try {
108         			LOG.debug("Bus sleeping...");
109         			try {
110 						Thread.sleep(1000 *  timePeriodInSec2);
111         			} catch (InterruptedException e) {
112         				LOG.error(e.getMessage(), e);
113         			}
114         			//LOG.info("Refreshing bus Routes data...");
115         			busService.getDao().loadRoutes();
116         			LOG.debug("Finished refreshing bus Routes data.");
117 
118         		} catch (Exception e) {
119         			LOG.error(e.getMessage(), e);
120         		}
121         	}
122         }
123 
124     }
125 
126 	public void updateUpdateRoutes(){
127     	busService.getDao().loadRoutes();		
128 		LOG.info("SPRING Task: Finished refreshing bus Routes data.");
129 	}
130 	
131 }