View Javadoc

1   /**
2    * 
3    */
4   package net.sf.provisioner.core;
5   
6   import net.sf.provisioner.adapters.SQLAdapter;
7   import net.sf.provisioner.config.Configuration;
8   
9   import org.apache.log4j.Logger;
10  
11  
12  
13  /**
14   * .
15   * <p>
16   * 
17   * .
18   * <p>
19   * 
20   *             
21   * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:27 $
22   * @author Gonzalo Espert
23   */
24  public class Producer {
25  
26  	/** Logger for this class and subclasses */
27  	Logger logger = Logger.getLogger(getClass());
28  	
29  	public boolean isRunning = false;
30  	long timeInterval;
31  	String configPath;
32  	int maxConsumers;
33  	
34  	SQLAdapter operationQueue;
35      
36  	/**
37       * .
38       * <p>
39       * 
40       * .
41       * <p>
42       * 
43       * 
44       * 
45       * @param 
46       *            
47       * @throws 
48       *            
49       *           
50       */
51  	public Producer(long timeInterval, String configPath, int maxConsumers) {
52  		
53  		this.timeInterval = timeInterval;
54  		this.configPath = configPath;
55  		this.maxConsumers = maxConsumers;
56  	}
57  	
58  	/**
59       * Main loop.
60       * <p>
61       * 
62       * .
63       * <p>
64       * 
65       * 
66       * 
67       * @param 
68       *            
69       * @throws 
70       *            
71       *           
72       */
73  	public void Start(Configuration config) throws Exception{
74  				
75  		/**
76  		 * Creates access to the operations queue
77  		 */
78  		this.operationQueue = new SQLAdapter(config.db);
79  		
80  		try {
81  			
82  			this.isRunning = true;
83  			
84  			while (isRunning) {
85  				
86  				logger.info("Retrieving pending operations...");
87  				Operation[] operations = this.operationQueue.getPendingOperations(this.maxConsumers);
88  				
89  				for (int i = 0; i < this.maxConsumers; i++) {
90  	
91  					if (!operations[i].getId().equalsIgnoreCase("0")) {
92  					
93  						logger.info("Create consumer to process operation number: " + operations[i].getId());
94  						Consumer consumer = new Consumer(operations[i], config);
95  					}
96  				}
97  				try {
98  					Thread.sleep(this.timeInterval);
99  				}
100 				catch (Exception e) {
101 					throw e;
102 				}
103 			}
104 			
105 		} catch (Exception e) {
106 		  			
107 			throw e;
108 		}
109 		
110 		finally {
111 			
112 			this.isRunning = false;
113 		}
114 	}
115 	
116 	/**
117      * End of main loop.
118      * <p>
119      * 
120      * .
121      * <p>
122      * 
123      * 
124      * 
125      * @param 
126      *            
127      * @throws 
128      *            
129      *           
130      */
131 	public void Stop() {
132 		this.isRunning = false;
133 	}
134 }