View Javadoc

1   package net.sf.crispy.concurrent;
2   
3   import java.lang.reflect.Method;
4   
5   import net.sf.crispy.InvocationException;
6   import net.sf.crispy.impl.ServiceManager;
7   
8   
9   /**
10   * Create a Thread and execute the service call.
11   * 
12   * @author Linke
13   * @since Version 1.0
14   *
15   */
16  public class ThreadWorker {
17  	
18  	private ThreadController threadController = null;
19  	
20  	public ThreadWorker(ThreadController pvThreadController) {
21  		threadController = pvThreadController;
22  	}
23  	
24  	protected Thread createNewThread(Runnable pvRunnable) {
25  		Thread lvThread = new Thread(pvRunnable);
26  //		lvThread.setDaemon(true);
27  		return lvThread;
28  	}
29  	
30  	public void executeAsynchrone (final Object pvObject, final Method pvMethod, final Object[] pvArgs, 
31  								   final ExecuteCallback pvExecutorCallback, final AsynchronousCallback pvAsynchroneCallback) {
32  		
33  		Runnable lvRunnable = new Runnable() {
34  
35  			public void run() {
36  				String lvMethodName = (pvMethod == null ? "NoMethodName" : pvMethod.getName());
37  				try {
38  					Object lvResult = pvExecutorCallback.execute(pvObject, pvMethod, pvArgs);
39  					pvAsynchroneCallback.handleResult(lvResult, lvMethodName);
40  				} catch (Exception e) {
41  					if (ServiceManager.DEBUG_MODE_ON == true) {
42  						e.printStackTrace();
43  					}
44  					InvocationException lvException = new InvocationException("Exception by execution the method" +
45  							pvMethod + " with parameter: " + pvArgs, e);
46  					pvAsynchroneCallback.handleError(lvException, lvMethodName);
47  				}
48  				finally {
49  					threadController.releasThreadWorker(ThreadWorker.this);
50  				}
51  			}
52  			
53  		};
54  		createNewThread(lvRunnable).start();
55  	}
56  
57  }