ExitStatus详解
ExitStatus详解
ExitStatus源代码详解
从ExitStatus源代码可以看出,ExitStatus有UNKNOWN, EXECUTING, COMPLETED, NOOP, FAILED, STOPPED这几个属性。
package org.springframework.batch.core;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import org.springframework.util.StringUtils;
public class ExitStatus implements Serializable, Comparable<ExitStatus> {
public static final ExitStatus UNKNOWN = new ExitStatus("UNKNOWN");
public static final ExitStatus EXECUTING = new ExitStatus("EXECUTING");
public static final ExitStatus COMPLETED = new ExitStatus("COMPLETED");
public static final ExitStatus NOOP = new ExitStatus("NOOP");
public static final ExitStatus FAILED = new ExitStatus("FAILED");
public static final ExitStatus STOPPED = new ExitStatus("STOPPED");
private final String exitCode;
private final String exitDescription;
public ExitStatus(String exitCode) {
this(exitCode, "");
}
public ExitStatus(String exitCode, String exitDescription) {
this.exitCode = exitCode;
this.exitDescription = exitDescription == null ? "" : exitDescription;
}
public String getExitCode() {
return this.exitCode;
}
public String getExitDescription() {
return this.exitDescription;
}
public ExitStatus and(ExitStatus status) {
if (status == null) {
return this;
} else {
ExitStatus result = this.addExitDescription(status.exitDescription);
if (this.compareTo(status) < 0) {
result = result.replaceExitCode(status.exitCode);
}
return result;
}
}
public int compareTo(ExitStatus status) {
if (this.severity(status) > this.severity(this)) {
return -1;
} else {
return this.severity(status) < this.severity(this) ? 1 : this.getExitCode().compareTo(status.getExitCode());
}
}
private int severity(ExitStatus status) {
if (status.exitCode.startsWith(EXECUTING.exitCode)) {
return 1;
} else if (status.exitCode.startsWith(COMPLETED.exitCode)) {
return 2;
} else if (status.exitCode.startsWith(NOOP.exitCode)) {
return 3;
} else if (status.exitCode.startsWith(STOPPED.exitCode)) {
return 4;
} else if (status.exitCode.startsWith(FAILED.exitCode)) {
return 5;
} else {
return status.exitCode.startsWith(UNKNOWN.exitCode) ? 6 : 7;
}
}
public String toString() {
return String.format("exitCode=%s;exitDescription=%s", this.exitCode, this.exitDescription);
}
public boolean equals(Object obj) {
return obj == null ? false : this.toString().equals(obj.toString());
}
public int hashCode() {
return this.toString().hashCode();
}
public ExitStatus replaceExitCode(String code) {
return new ExitStatus(code, this.exitDescription);
}
public boolean isRunning() {
return "RUNNING".equals(this.exitCode) || "UNKNOWN".equals(this.exitCode);
}
public ExitStatus addExitDescription(String description) {
StringBuffer buffer = new StringBuffer();
boolean changed = StringUtils.hasText(description) && !this.exitDescription.equals(description);
if (StringUtils.hasText(this.exitDescription)) {
buffer.append(this.exitDescription);
if (changed) {
buffer.append("; ");
}
}
if (changed) {
buffer.append(description);
}
return new ExitStatus(this.exitCode, buffer.toString());
}
public ExitStatus addExitDescription(Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
String message = writer.toString();
return this.addExitDescription(message);
}
}