Skip to content

Commit

Permalink
QTZ-301 Merge branch 2.2.x to trunk : removed by accident the org.qua…
Browse files Browse the repository at this point in the history
…rtz.jobs package, re adding it, using quartz 2.2.x core version
  • Loading branch information
adahanne committed May 28, 2012
1 parent 92a3c3c commit 0069ec5
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 307 deletions.
147 changes: 82 additions & 65 deletions quartz-jobs/src/main/java/org/quartz/jobs/NativeJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@
import java.io.InputStream;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* <p> Built in job for executing native executables in a separate process.</p>
* <p>
* Built in job for executing native executables in a separate process.
* </p>
*
* <pre>
* JobDetail job = new JobDetail("dumbJob", null, org.quartz.jobs.NativeJob.class);
* job.getJobDataMap().put(org.quartz.jobs.NativeJob.PROP_COMMAND, "echo \"hi\" >> foobar.txt");
* Trigger trigger = TriggerUtils.makeSecondlyTrigger(5);
* trigger.setName("dumbTrigger");
* sched.scheduleJob(job, trigger);
* JobDetail job = new JobDetail(&quot;dumbJob&quot;, null, org.quartz.jobs.NativeJob.class);
* job.getJobDataMap().put(org.quartz.jobs.NativeJob.PROP_COMMAND,
* &quot;echo \&quot;hi\&quot; &gt;&gt; foobar.txt&quot;);
* Trigger trigger = TriggerUtils.makeSecondlyTrigger(5);
* trigger.setName(&quot;dumbTrigger&quot;);
* sched.scheduleJob(job, trigger);
* </pre>
*
* If PROP_WAIT_FOR_PROCESS is true, then the Integer exit value of the process
Expand All @@ -58,46 +61,48 @@ public class NativeJob implements Job {
private final Logger log = LoggerFactory.getLogger(getClass());

/*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constants.
*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

/**
* Required parameter that specifies the name of the command (executable)
* to be ran.
* Required parameter that specifies the name of the command (executable) to
* be ran.
*/
public static final String PROP_COMMAND = "command";

/**
* Optional parameter that specifies the parameters to be passed to the
* executed command.
*/
public static final String PROP_PARAMETERS = "parameters";



/**
* Optional parameter (value should be 'true' or 'false') that specifies
* whether the job should wait for the execution of the native process to
* Optional parameter (value should be 'true' or 'false') that specifies
* whether the job should wait for the execution of the native process to
* complete before it completes.
*
* <p>Defaults to <code>true</code>.</p>
* <p>
* Defaults to <code>true</code>.
* </p>
*/
public static final String PROP_WAIT_FOR_PROCESS = "waitForProcess";

/**
* Optional parameter (value should be 'true' or 'false') that specifies
* whether the spawned process's stdout and stderr streams should be
* consumed. If the process creates output, it is possible that it might
* Optional parameter (value should be 'true' or 'false') that specifies
* whether the spawned process's stdout and stderr streams should be
* consumed. If the process creates output, it is possible that it might
* 'hang' if the streams are not consumed.
*
* <p>Defaults to <code>false</code>.</p>
* <p>
* Defaults to <code>false</code>.
* </p>
*/
public static final String PROP_CONSUME_STREAMS = "consumeStreams";



/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
Expand All @@ -107,10 +112,10 @@ public class NativeJob implements Job {
*/

public void execute(JobExecutionContext context)
throws JobExecutionException {
throws JobExecutionException {

JobDataMap data = context.getMergedJobDataMap();

String command = data.getString(PROP_COMMAND);

String parameters = data.getString(PROP_PARAMETERS);
Expand All @@ -120,34 +125,35 @@ public void execute(JobExecutionContext context)
}

boolean wait = true;
if(data.containsKey(PROP_WAIT_FOR_PROCESS)) {
if (data.containsKey(PROP_WAIT_FOR_PROCESS)) {
wait = data.getBooleanValue(PROP_WAIT_FOR_PROCESS);
}
boolean consumeStreams = false;
if(data.containsKey(PROP_CONSUME_STREAMS)) {
if (data.containsKey(PROP_CONSUME_STREAMS)) {
consumeStreams = data.getBooleanValue(PROP_CONSUME_STREAMS);
}

Integer exitCode = this.runNativeCommand(command, parameters, wait, consumeStreams);

Integer exitCode = this.runNativeCommand(command, parameters, wait,
consumeStreams);
context.setResult(exitCode);

}

protected Logger getLog() {
return log;
}

private Integer runNativeCommand(String command, String parameters, boolean wait, boolean consumeStreams) throws JobExecutionException {

private Integer runNativeCommand(String command, String parameters,
boolean wait, boolean consumeStreams) throws JobExecutionException {

String[] cmd = null;
String[] args = new String[2];
Integer result = null;
Integer result = null;
args[0] = command;
args[1] = parameters;


try {
//with this variable will be done the swithcing
// with this variable will be done the swithcing
String osName = System.getProperty("os.name");

// specific for Windows
Expand All @@ -163,45 +169,51 @@ private Integer runNativeCommand(String command, String parameters, boolean wait
cmd[i + 2] = args[i];
}
} else if (osName.equals("Linux")) {
if (cmd == null) {
cmd = new String[3];
}
cmd[0] = "/bin/sh";
cmd[1] = "-c";
cmd[2] = args[0] + " " + args[1];
} else { // try this...
if (cmd == null) {
cmd = new String[3];
}
cmd[0] = "/bin/sh";
cmd[1] = "-c";
cmd[2] = args[0] + " " + args[1];
} else { // try this...
cmd = args;
}

Runtime rt = Runtime.getRuntime();
// Executes the command
getLog().info("About to run " + cmd[0] + " " + cmd[1] + " " + (cmd.length>2 ? cmd[2] : "") + " ...");
getLog().info(
"About to run " + cmd[0] + " " + cmd[1] + " "
+ (cmd.length > 2 ? cmd[2] : "") + " ...");
Process proc = rt.exec(cmd);
// Consumes the stdout from the process
StreamConsumer stdoutConsumer = new StreamConsumer(proc.getInputStream(), "stdout");
StreamConsumer stdoutConsumer = new StreamConsumer(
proc.getInputStream(), "stdout");

// Consumes the stderr from the process
if(consumeStreams) {
StreamConsumer stderrConsumer = new StreamConsumer(proc.getErrorStream(), "stderr");
if (consumeStreams) {
StreamConsumer stderrConsumer = new StreamConsumer(
proc.getErrorStream(), "stderr");
stdoutConsumer.start();
stderrConsumer.start();
}
if(wait) {

if (wait) {
result = Integer.valueOf(proc.waitFor());
}
// any error message?

} catch (Throwable x) {
throw new JobExecutionException("Error launching native command: ", x, false);
throw new JobExecutionException("Error launching native command: ",
x, false);
}

return result;
}

/**
* Consumes data from the given input stream until EOF and prints the data to stdout
*
* Consumes data from the given input stream until EOF and prints the data
* to stdout
*
* @author cooste
* @author jhouse
*/
Expand All @@ -218,8 +230,8 @@ public StreamConsumer(InputStream inputStream, String type) {
}

/**
* Runs this object as a separate thread, printing the contents of the InputStream
* supplied during instantiation, to either stdout or stderr
* Runs this object as a separate thread, printing the contents of the
* InputStream supplied during instantiation, to either stdout or stderr
*/
@Override
public void run() {
Expand All @@ -229,20 +241,25 @@ public void run() {
String line = null;

while ((line = br.readLine()) != null) {
if(type.equalsIgnoreCase("stderr")) {
if (type.equalsIgnoreCase("stderr")) {
getLog().warn(type + ">" + line);
} else {
getLog().info(type + ">" + line);
}
}
} catch (IOException ioe) {
getLog().error("Error consuming " + type + " stream of spawned process.", ioe);
getLog().error(
"Error consuming " + type
+ " stream of spawned process.", ioe);
} finally {
if(br != null) {
try { br.close(); } catch(Exception ignore) {}
if (br != null) {
try {
br.close();
} catch (Exception ignore) {
}
}
}
}
}

}
Loading

0 comments on commit 0069ec5

Please sign in to comment.