Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.Properties;
  4.  
  5. import org.apache.commons.exec.CommandLine;
  6. import org.apache.commons.exec.DefaultExecuteResultHandler;
  7. import org.apache.commons.exec.DefaultExecutor;
  8. import org.apache.commons.exec.ExecuteException;
  9. import org.apache.commons.exec.PumpStreamHandler;
  10. import org.apache.commons.exec.ShutdownHookProcessDestroyer;
  11.  
  12.  
  13. /**
  14. * This is a standalone class that essentially helps spawn a jar given via the commandline along with the
  15. * arguments that are required by the jar.
  16. *
  17. */
  18. public class JarSpawner {
  19.  
  20. /**
  21. * This class is internally used to save the absolute file path of a given jar and its command line arguments.
  22. *
  23. */
  24. static class JarFileAttributes {
  25. public File getFilePath() {
  26. return filePath;
  27. }
  28.  
  29. public String getJarArgs() {
  30. return jarArgs;
  31. }
  32.  
  33. @Override
  34. public String toString() {
  35. return "JarFileAttributes [filePath=" + filePath.getAbsolutePath() + ", jarArgs=" + jarArgs + "]";
  36. }
  37.  
  38. private File filePath;
  39. private String jarArgs;
  40.  
  41. public JarFileAttributes(File filePath, String jarArgs) {
  42. this.filePath = filePath;
  43. this.jarArgs = jarArgs;
  44. }
  45. }
  46.  
  47. public static void main(String[] args) throws ExecuteException, IOException, InterruptedException {
  48. JarFileAttributes attributes = getJarFile(args);
  49. Properties properties = new Properties();
  50. properties.load(JarSpawner.class.getResourceAsStream("/spawner.properties"));
  51. long interval = Long.parseLong((String) properties.get("defaultInterval"));
  52. while (true) {
  53. continuouslyRestart(attributes, interval);
  54. Thread.sleep(interval);
  55. System.out.println("Application exited. Respawning the application again");
  56. }
  57.  
  58. }
  59.  
  60. /**
  61. * This method parses the commandline argument and extracts the jar file and its arguments.
  62. * @param args - The command line arguments that needs to be parsed.
  63. * @return - A {@link JarFileAttributes} object that represents the path of the jar file and its command line args.
  64. */
  65. public static JarFileAttributes getJarFile(String[] args) {
  66. File file = null;
  67. boolean wasJarFound = false;
  68. int jarArgsIndex = 0;
  69. StringBuffer sb = new StringBuffer();
  70. for (int i = 0; i < args.length; i++) {
  71. if (args[i].contains(".jar")) {
  72. file = new File(args[i]);
  73. wasJarFound = true;
  74. jarArgsIndex = i + 1;
  75. }
  76. }
  77. if (!wasJarFound) {
  78. throw new RuntimeException("Please specify the jar file");
  79. }
  80. for (int i = jarArgsIndex; i < args.length; i++) {
  81. sb.append(args[i]).append(" ");
  82. }
  83. return new JarFileAttributes(file, sb.toString());
  84. }
  85.  
  86. /**
  87. * This method spawns a jar, and waits for it to exit [either cleanly or forcibly]
  88. *
  89. * @param attributes - {@link JarFileAttributes} that represents the jar file path and its arguments.
  90. * @param interval - How often should the application check if the jar is still running or if it exit.
  91. * @throws ExecuteException
  92. * @throws IOException
  93. * @throws InterruptedException
  94. */
  95. public static void continuouslyRestart(JarFileAttributes attributes, long interval)
  96. throws ExecuteException, IOException, InterruptedException {
  97. System.out.println("Spawning the application ");
  98. CommandLine cmdLine = new CommandLine("java");
  99. cmdLine.addArgument("-jar");
  100. cmdLine.addArguments(attributes.getFilePath().getAbsolutePath() + " " + attributes.getJarArgs());
  101.  
  102.  
  103. DefaultExecutor executor = new DefaultExecutor();
  104. executor.setStreamHandler(new PumpStreamHandler());
  105. executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
  106. DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
  107. executor.execute(cmdLine, handler);
  108. while (!handler.hasResult()) {
  109. Thread.sleep(interval);
  110. }
  111.  
  112.  
  113. if (handler.hasResult()) {
  114. ExecuteException e = handler.getException();
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement