View Javadoc

1   /**
2    * Copyright 2011-2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.weather.dao;
17  
18  import org.kuali.mobility.weather.service.WeatherService;
19  
20  public class WeatherInitBean {
21  
22  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WeatherInitBean.class);
23  
24  	private WeatherService weatherService;
25  	private int minutesToRefresh;
26  	
27  	private static Thread backgroundThread = null;
28  
29  	public void init() {
30  		backgroundThread = new Thread(new BackgroundThread());
31  		backgroundThread.setDaemon(true);
32  		backgroundThread.start();
33  	}
34  
35  	public void cleanup() {
36  		LOG.info("Cleaning up weather.");
37  	}
38  
39  	public WeatherService getWeatherService() {
40  		return weatherService;
41  	}
42  
43  	public void setWeatherService(WeatherService weatherService) {
44  		this.weatherService = weatherService;
45  	}
46  
47  	public int getMinutesToRefresh() {
48  		return minutesToRefresh;
49  	}
50  
51  	public void setMinutesToRefresh(int minutesToRefresh) {
52  		this.minutesToRefresh = minutesToRefresh;
53  	}
54  
55  	private class BackgroundThread implements Runnable {
56  
57  		public void run() {
58  			while (true) {
59  				try {
60  					weatherService.refreshWeather();
61  					try {
62  						Thread.sleep(1000 * 60 * getMinutesToRefresh());
63  					} catch (InterruptedException e) {
64  						LOG.error(e.getMessage(), e);
65  					}
66  				} catch (Exception e) {
67  					LOG.error(e.getMessage(), e);
68  				}
69  			}
70  		}
71  
72  	}
73  
74  }