View Javadoc

1   /** 2007, Digitalis Informatica. All rights reserved.
2    * 
3    * Distribuicao e Gestao de Informatica, Lda.
4    * Estrada de Paco de Arcos num.9 - Piso -1
5    * 2780-666 Paco de Arcos
6    * Telefone: (351) 21 4408990
7    * Fax: (351) 21 4408999
8    * http://www.digitalis.pt 
9    */
10  
11  package pt.digitalis.sampleApp.iss;
12  
13  import pt.digitalis.iss.IProcess;
14  import pt.digitalis.iss.ISS;
15  import pt.digitalis.iss.ProcessResults;
16  import pt.digitalis.iss.ProcessState;
17  import pt.digitalis.iss.ServiceRequest;
18  
19  /**
20   * This class simulates parallel requests being made to the AppServer. The
21   * attributes include the ISS who will execute the request, a user-supplied id
22   * and the service request that will be executed.
23   * 
24   * @author Rodrigo Gonçalves <a
25   *         href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a><br/>
26   */
27  public class myProcessCaller extends Thread {
28  
29  	/** The ISS that will serve the request. */
30  	private ISS theISS;
31  
32  	/** An id. */
33  	String id = "";
34  
35  	/** The request */
36  	ServiceRequest req;
37  
38  	/** The process to delegate. */
39  	IProcess theProcess;
40  
41  	/** The results after execution */
42  	ProcessResults results;
43  
44  	/**
45  	 * Ctor.
46  	 * 
47  	 * @param theProcess
48  	 *            the process to execute
49  	 * @param theISS
50  	 *            the ISS that should execute the process
51  	 * @param id
52  	 *            this Caller's id
53  	 */
54  	public myProcessCaller(IProcess theProcess, ISS theISS, String id) {
55  		super("myProcessCaller-" + id);
56  		this.theProcess = theProcess;
57  		this.theISS = theISS;
58  		this.id = id;
59  	}
60  
61  	/**
62  	 * Inspector for the service request attribute.
63  	 * 
64  	 * @return the service request
65  	 */
66  	public ServiceRequest getRequest() {
67  		return this.req;
68  	}
69  
70  	/**
71  	 * Overriding Thread#run()...
72  	 * 
73  	 * @see java.lang.Thread#run()
74  	 */
75  	public void run() {
76  		try {
77  			if (theISS == null) {
78  
79  				// No ISS execute immediately
80  				results = theProcess.execute();
81  
82  			} else {
83  
84  				// Delegate execution to the ISS manager
85  				this.req = theISS.delegate(this.theProcess, this.id);
86  
87  				while (req.getState() != ProcessState.FINISHED
88  						&& req.getState() != ProcessState.ERROR) {
89  					try {
90  						synchronized (this) {
91  							// ...go to sleep
92  							wait(500);
93  						}
94  					} catch (InterruptedException e) {
95  						e.printStackTrace();
96  					}
97  
98  					req = theISS.fetchResults(id);
99  				}
100 
101 				results = new ProcessResults();
102 				results.setResults(req.getResults());
103 			}
104 
105 		} catch (OutOfMemoryError e) {
106 			System.out
107 					.println("Process #"
108 							+ id
109 							+ ": Out of memory! Releasing failed process objects from RAM...");
110 
111 			req = null;
112 			results = null;
113 		}
114 	}
115 
116 	/**
117 	 * @return the results
118 	 */
119 	public ProcessResults getResults() {
120 		return results;
121 	}
122 
123 	/**
124 	 * @return the theProcess
125 	 */
126 	public IProcess getTheProcess() {
127 		return theProcess;
128 	}
129 }