commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r928201 - in /commons/proper/daemon/trunk/src/samples: ProcrunService.java ProcrunServiceInstall.cmd
Date Sat, 27 Mar 2010 14:14:46 GMT
Author: sebb
Date: Sat Mar 27 14:14:45 2010
New Revision: 928201

URL: http://svn.apache.org/viewvc?rev=928201&view=rev
Log:
Allow ProcrunService to be run in both JVM and JAVA modes

Modified:
    commons/proper/daemon/trunk/src/samples/ProcrunService.java
    commons/proper/daemon/trunk/src/samples/ProcrunServiceInstall.cmd

Modified: commons/proper/daemon/trunk/src/samples/ProcrunService.java
URL: http://svn.apache.org/viewvc/commons/proper/daemon/trunk/src/samples/ProcrunService.java?rev=928201&r1=928200&r2=928201&view=diff
==============================================================================
--- commons/proper/daemon/trunk/src/samples/ProcrunService.java (original)
+++ commons/proper/daemon/trunk/src/samples/ProcrunService.java Sat Mar 27 14:14:45 2010
@@ -1,3 +1,5 @@
+import java.io.File;
+import java.io.IOException;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -22,6 +24,9 @@ import java.util.Date;
 
 /**
  * Sample service implementation for use with Windows Procrun.
+ * <p>
+ * Use the main() method for running as a Java (external) service.
+ * Use the start() and stop() methods for running as a jvm (in-process) service 
  */
 public class ProcrunService implements Runnable {
 
@@ -32,8 +37,22 @@ public class ProcrunService implements R
     
     private final long pause; // How long to pause in service loop
 
-    private ProcrunService(long wait){
+    private final File stopFile;
+    
+    /**
+     * 
+     * @param wait seconds to wait in loop
+     * @param filename optional filename - if non-null, run loop will stop when it disappears
+     * @throws IOException 
+     */
+    private ProcrunService(long wait, File file) {
         pause=wait;
+        stopFile = file;
+    }
+
+    private static File tmpFile(String filename) {
+        return new File(System.getProperty("java.io.tmpdir"),
+                filename != null ? filename : "ProcrunService.tmp");
     }
 
     private static void usage(){
@@ -41,50 +60,87 @@ public class ProcrunService implements R
     }
 
     /**
+     * Helper method for process args with defaults.
+     * 
+     * @param args array of string arguments, not null, may be empty
+     * @param argnum which argument to extract
+     * @return the argument or null
+     */
+    private static String getArg(String[] args, int argnum){
+        if (args.length > argnum) {
+            return args[argnum];
+        } else {
+            return null;
+        }
+    }
+
+    /**
      * Common entry point for start and stop service functions.
      * 
      * @param args [start [pause time] | stop]
+     * @throws IOException 
      */
-    public static void main(String[] args) {
+    public static void main(String[] args) throws IOException {
         final int argc = args.length;
         log("ProcrunService called with "+argc+" arguments from thread: "+Thread.currentThread());
         for(int i=0; i < argc; i++) {
             System.out.println("["+i+"] "+args[i]);
         }
-        String mode=null;
-        if (argc > 0) {
-            mode = args[0];
-            if ("start".equals(mode)){
-                long wait = DEFAULT_PAUSE;
-                if (argc > 1) {
-                    wait = Integer.valueOf(args[1]).intValue();
-                }
-                log("Starting the thread, wait(seconds): "+wait);
-                thrd = new Thread(new ProcrunService(wait*MS_PER_SEC));
-                thrd.start();
-            } else 
-            if ("stop".equals(mode)) {
-                if (thrd != null) {
-                    log("Interrupting the thread");
-                    thrd.interrupt();
-                } else {
-                    log("No thread to interrupt");
-                }
-            } else {
-                usage();
-            }
+        String mode=getArg(args, 0);
+        if ("start".equals(mode)){
+            File f = tmpFile(getArg(args, 2));
+            log("Creating file: "+f.getPath());
+            f.createNewFile();
+            startThread(getArg(args, 1), f);
+        } else 
+        if ("stop".equals(mode)) {
+            final File tmpFile = tmpFile(getArg(args, 1));
+            log("Deleting file: "+tmpFile.getPath());
+            tmpFile.delete();
         } else {
             usage();
         }
     }
-    
+
+    /**
+     * Start the jvm version of the service.
+     * @param args optional, arg[0] = timeout (seconds)
+     */
+    public static void start(String [] args) {
+        startThread(getArg(args, 0), null);
+    }
+
+    private static void startThread(String waitParam, File file) {
+        long wait = DEFAULT_PAUSE;
+        if (waitParam != null) {
+            wait = Integer.valueOf(waitParam).intValue();
+        }
+        log("Starting the thread, wait(seconds): "+wait);
+        thrd = new Thread(new ProcrunService(wait*MS_PER_SEC,file));
+        thrd.start();        
+    }
+
+    /**
+     * Stop the JVM version of the service.
+     * 
+     * @param args ignored
+     */
+    public static void stop(String [] args){
+        if (thrd != null) {
+            log("Interrupting the thread");
+            thrd.interrupt();
+        } else {
+            log("No thread to interrupt");
+        }        
+    }
+
     /**
      * This method performs the work of the service.
      * In this case, it just logs a message every so often.
      */
     public void run() {
         log("Started thread in "+System.getProperty("user.dir"));
-        while(true){
+        while(stopFile == null || stopFile.exists()){
             try {
                 log("pausing...");
                 Thread.sleep(pause);

Modified: commons/proper/daemon/trunk/src/samples/ProcrunServiceInstall.cmd
URL: http://svn.apache.org/viewvc/commons/proper/daemon/trunk/src/samples/ProcrunServiceInstall.cmd?rev=928201&r1=928200&r2=928201&view=diff
==============================================================================
--- commons/proper/daemon/trunk/src/samples/ProcrunServiceInstall.cmd (original)
+++ commons/proper/daemon/trunk/src/samples/ProcrunServiceInstall.cmd Sat Mar 27 14:14:45
2010
@@ -21,8 +21,9 @@ rem Copy this file and ProcrunService.ja
 
 setlocal
 
-rem The service name (make sure it does not clash with an existing service)
-set SERVICE=ProcrunService
+rem The service names (make sure they does not clash with an existing service)
+set SERVICE_JVM=ProcrunServiceJvm
+set SERVICE_JAVA=ProcrunServiceJava
 
 rem my location
 set MYPATH=%~dp0
@@ -33,14 +34,31 @@ set PATH_PRUNSRV=%MYPATH%
 rem location of jarfile
 set PATH_JAR=%MYPATH%
 
-set PRUNSRV=%PATH_PRUNSRV%prunsrv
-echo Installing %SERVICE% if necessary
-%PRUNSRV% //IS//%SERVICE% --Install
+rem Allow prunsrv to be overridden
+if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv
 
-echo Setting the parameters for %SERVICE%
-%PRUNSRV% //US//%SERVICE% --Jvm=auto --StdOutput auto --StdError auto ^
+rem Install the 2 services
+
+echo Installing %SERVICE_JVM%
+%PRUNSRV% //DS//%SERVICE_JVM%
+%PRUNSRV% //IS//%SERVICE_JVM% --Install
+
+echo Setting the parameters for %SERVICE_JVM%
+%PRUNSRV% //US//%SERVICE_JVM% --Jvm=auto --StdOutput auto --StdError auto ^
+--Classpath=%PATH_JAR%ProcrunService.jar ^
+--StartMode=jvm --StartClass=ProcrunService --StartMethod=start ^
+ --StopMode=jvm  --StopClass=ProcrunService  --StopMethod=stop
+
+echo Installation of %SERVICE_JVM% is complete
+
+echo Installing %SERVICE_JAVA%
+%PRUNSRV% //DS//%SERVICE_JAVA%
+%PRUNSRV% //IS//%SERVICE_JAVA% --Install
+
+echo Setting the parameters for %SERVICE_JAVA%
+%PRUNSRV% //US//%SERVICE_JAVA% --Jvm=auto --StdOutput auto --StdError auto ^
 --Classpath=%PATH_JAR%ProcrunService.jar ^
---StartMode=jvm --StartClass=ProcrunService --StartParams=start ^
- --StopMode=jvm  --StopClass=ProcrunService  --StopParams=stop
+--StartMode=java --StartClass=ProcrunService --StartParams=start ^
+ --StopMode=java  --StopClass=ProcrunService  --StopParams=stop
 
-echo Installation of %SERVICE% is complete
\ No newline at end of file
+echo Installation of %SERVICE_JAVA% is complete
\ No newline at end of file



Mime
View raw message