2015. 3. 19. 18:22ㆍPrograming/Android / Java
If you develop a more complex system, the chances of being a heterogeneous are pretty big. So if you develop a system in Java, but you have to integrate all ready build parts with other technology, the most desirable option is some kind of service oriented architecture. But when this is not available you will simply rely on an external process.
To start an external process is quite simple. Just look at the exec
methods from Runtime
.
What could be more difficult is to know when that process as finished. The Java process and that subprocess will run in parallel. If you want to perform an action when the subprocess has finish its execution, this article may help you in this direction.
It would be very nice if the Process
class would implement the Observable design pattern in some way. But it doesn’t and on this we will base our workaround creation.Process
contains a useful method for us, waitFor
, that pauses the current thread execution until the given subprocess is finished. So, we will create a thread that will be suspended until the given subprocess is finished. Then we will fire the listeners to notify that the process has finished its execution.
Enough talking, let’s see some code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | /** * Detects when a process is finished and invokes the associated listeners. */ public class ProcessExitDetector extends Thread { /** The process for which we have to detect the end. */ private Process process; /** The associated listeners to be invoked at the end of the process. */ private List<ProcessListener> listeners = new ArrayList<ProcessListener>(); /** * Starts the detection for the given process * @param process the process for which we have to detect when it is finished */ public ProcessExitDetector(Process process) { try { // test if the process is finished process.exitValue(); throw new IllegalArgumentException( "The process is already ended" ); } catch (IllegalThreadStateException exc) { this .process = process; } } /** @return the process that it is watched by this detector. */ public Process getProcess() { return process; } public void run() { try { // wait for the process to finish process.waitFor(); // invokes the listeners for (ProcessListener listener : listeners) { listener.processFinished(process); } } catch (InterruptedException e) { } } /** Adds a process listener. * @param listener the listener to be added */ public void addProcessListener(ProcessListener listener) { listeners.add(listener); } /** Removes a process listener. * @param listener the listener to be removed */ public void removeProcessListener(ProcessListener listener) { listeners.remove(listener); } } |
The code is very easy to understand. It creates a class as the detector for the process exit. It has a process to watch for passed in as a constructor argument. The entire thread does nothing else than wait for the given process to finish and then invokes the listeners.
The code for the ProcessListener
is as easy:
1 2 3 | public interface ProcessListener extends EventListener { void processFinished(Process process); } |
Now you have a very easy way to detect when a subprocess is finished, and as proof, here is a code sample:
1 2 3 4 5 6 7 8 9 | // ... processExitDetector = new ProcessExitDetector(subprocess); processExitDetector .addProcessListener( new ProcessListener() { public void processFinished(Process process) { System.out.println( "The subprocess has finished." ); } }); processExitDetector.start(); // ... |
All the code samples were tested with JDK 6 under Windows XP.
Refer : https://beradrian.wordpress.com/2008/11/03/detecting-process-exit-in-java/
'Programing > Android / Java' 카테고리의 다른 글
Linux/Google Android - chmod command (리눅스 파일 및 디렉토리 권한 변경) (0) | 2015.03.23 |
---|---|
Google Android SIGPIPE (0) | 2015.03.23 |
구글 안드로이드 판매자 센터 오류 - 세금 정보를 업데이트 (0) | 2015.03.16 |
[Java] How to convert byte array to char array in java? (0) | 2015.03.11 |
자바 한글 인코딩 변환 / Java Korean Encoding & Decoding (0) | 2015.03.10 |