You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@batchee.apache.org by st...@apache.org on 2014/08/09 20:34:53 UTC

[1/2] git commit: BATCHEE-51 add stop and restart to SimpleRest api

Repository: incubator-batchee
Updated Branches:
  refs/heads/master 88a8927c6 -> 50c23cf53


BATCHEE-51 add stop and restart to SimpleRest api

Usage via:
* .../jbatch/rest/stop/${executionId}
* .../jbatch/rest/restart/${executionId}


Project: http://git-wip-us.apache.org/repos/asf/incubator-batchee/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-batchee/commit/209a8d43
Tree: http://git-wip-us.apache.org/repos/asf/incubator-batchee/tree/209a8d43
Diff: http://git-wip-us.apache.org/repos/asf/incubator-batchee/diff/209a8d43

Branch: refs/heads/master
Commit: 209a8d43de09e9de65554570a3b304c708cab2ea
Parents: 88a8927
Author: Mark Struberg <st...@apache.org>
Authored: Sat Aug 9 14:17:13 2014 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Sat Aug 9 14:17:13 2014 +0200

----------------------------------------------------------------------
 .../batchee/servlet/SimpleRestController.java   | 132 ++++++++++++++-----
 .../org/apache/batchee/servlet/ServletTest.java | 118 +++++++++++------
 2 files changed, 180 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/209a8d43/gui/servlet/embedded/src/main/java/org/apache/batchee/servlet/SimpleRestController.java
----------------------------------------------------------------------
diff --git a/gui/servlet/embedded/src/main/java/org/apache/batchee/servlet/SimpleRestController.java b/gui/servlet/embedded/src/main/java/org/apache/batchee/servlet/SimpleRestController.java
index 6f6d6dc..b86239f 100644
--- a/gui/servlet/embedded/src/main/java/org/apache/batchee/servlet/SimpleRestController.java
+++ b/gui/servlet/embedded/src/main/java/org/apache/batchee/servlet/SimpleRestController.java
@@ -22,6 +22,8 @@ import java.io.StringWriter;
 import java.util.Map;
 import java.util.Properties;
 
+import javax.batch.operations.JobExecutionAlreadyCompleteException;
+import javax.batch.operations.JobExecutionNotRunningException;
 import javax.batch.operations.JobOperator;
 import javax.batch.operations.JobStartException;
 import javax.batch.operations.NoSuchJobExecutionException;
@@ -38,6 +40,8 @@ public class SimpleRestController {
 
     public static final String OP_START = "start/";
     public static final String OP_STATUS = "status/";
+    public static final String OP_STOP = "stop/";
+    public static final String OP_RESTART = "restart/";
 
     public static final long NO_JOB_ID = -1L;
 
@@ -58,29 +62,18 @@ public class SimpleRestController {
             startBatch(path.substring(OP_START.length()), req, resp);
         } else if (path != null && path.startsWith(OP_STATUS)) {
             batchStatus(path.substring(OP_STATUS.length()), req, resp);
+        } else if (path != null && path.startsWith(OP_STOP)) {
+            batchStop(path.substring(OP_STOP.length()), req, resp);
+        } else if (path != null && path.startsWith(OP_RESTART)) {
+            batchRestart(path.substring(OP_RESTART.length()), req, resp);
         } else {
             unknownCommand(path, resp);
         }
     }
 
     private void startBatch(String batchName, HttpServletRequest req, HttpServletResponse resp) {
-        Properties jobProperties = new Properties();
-        Map<String, String[]> parameterMap = req.getParameterMap();
-        for (Map.Entry<String, String[]> paramEntry : parameterMap.entrySet()) {
-            String key = paramEntry.getKey();
-            if (key == null || key.length() == 0) {
-                reportFailure(NO_JOB_ID, resp, "Parameter key must be set");
-                return;
-            }
-
-            String[] vals = paramEntry.getValue();
-            if (vals == null || vals.length != 1) {
-                reportFailure(NO_JOB_ID, resp, "Exactly one value must be set for each parameter (parameter name=" + key + ")");
-                return;
-            }
-            String val = vals[0];
-            jobProperties.put(key, val);
-        }
+        Properties jobProperties = extractJobProperties(req, resp);
+        if (jobProperties == null) { return; }
 
         try {
             long jobId = jobOperator.start(batchName, jobProperties);
@@ -95,36 +88,75 @@ public class SimpleRestController {
 
     private void batchStatus(String batchId, HttpServletRequest req, HttpServletResponse resp)
     {
-        if (batchId == null || batchId.isEmpty()) {
-            reportFailure(NO_JOB_ID, resp, "no executionId given");
+        Long executionId = extractExecutionId(batchId, resp);
+        if (executionId == null) {
             return;
         }
 
-        long executionId = NO_JOB_ID;
+        try {
+            JobExecution jobExecution = jobOperator.getJobExecution(executionId);
+            BatchStatus batchStatus = jobExecution.getBatchStatus();
+            reportSuccess(executionId, resp, batchStatus.name());
+        } catch (NoSuchJobExecutionException noSuchJob) {
+            reportFailure(executionId, resp, "NoSuchJob");
+        } catch (Exception generalException) {
+            StringBuilder msg = new StringBuilder("Failure in BatchExecution");
+            appendExceptionMsg(msg, generalException);
+            reportFailure(executionId, resp, msg.toString());
+        }
+    }
+
+    private void batchStop(String batchId, HttpServletRequest req, HttpServletResponse resp) {
+        Long executionId = extractExecutionId(batchId, resp);
+        if (executionId == null) {
+            return;
+        }
 
         try {
-            executionId = Long.valueOf(batchId);
-        } catch(NumberFormatException nfe) {
-            reportFailure(NO_JOB_ID, resp, "executionId must be numeric, but is " + batchId);
+            jobOperator.stop(executionId);
+            reportSuccess(executionId, resp, BatchStatus.STOPPING.toString());
+        } catch (NoSuchJobExecutionException noSuchJob) {
+            reportFailure(executionId, resp, "NoSuchJob");
+        } catch (JobExecutionNotRunningException notRunningException) {
+            reportFailure(executionId, resp, "JobExecutionNotRunning");
+        } catch (Exception generalException) {
+            StringBuilder msg = new StringBuilder("Failure in BatchExecution");
+            appendExceptionMsg(msg, generalException);
+            reportFailure(executionId, resp, msg.toString());
         }
+    }
+
+    private void batchRestart(String batchId, HttpServletRequest req, HttpServletResponse resp) {
+        Long executionId = extractExecutionId(batchId, resp);
+        if (executionId == null) {
+            return;
+        }
+
+        Properties jobProperties = extractJobProperties(req, resp);
 
         try {
-            JobExecution jobExecution = jobOperator.getJobExecution(executionId);
-            BatchStatus batchStatus = jobExecution.getBatchStatus();
-            reportSuccess(executionId, resp, batchStatus.name());
+            jobOperator.restart(executionId, jobProperties);
         } catch (NoSuchJobExecutionException noSuchJob) {
             reportFailure(executionId, resp, "NoSuchJob");
+        } catch (JobExecutionAlreadyCompleteException alreadyCompleted) {
+            reportFailure(executionId, resp, "NoSuchJob");
         } catch (Exception generalException) {
-            StringBuilder msg = new StringBuilder("NoSuchJob");
+            StringBuilder msg = new StringBuilder("Failure in BatchExecution");
             appendExceptionMsg(msg, generalException);
             reportFailure(executionId, resp, msg.toString());
         }
     }
 
+
     private void unknownCommand(String path, HttpServletResponse resp) {
         StringBuilder msg = new StringBuilder("Unknown command:");
         msg.append(path).append('\n');
 
+        msg.append("The returned response if of MIME type text/plain and contains the following information\n");
+        msg.append("  {jobExecutionId} (or -1 if no executionId was detected)\\n\n");
+        msg.append("  OK (or FAILURE)\\n\n");
+        msg.append("  followed by command specific information\n");
+
         msg.append("\nKnown commands are:\n\n");
         msg.append("* ").append(OP_START).append(" - start a new batch job\n");
         msg.append("  Sample: http://localhost:8080/myapp/jbatch/rest/start/myjobname?param1=x&param2=y\n");
@@ -134,14 +166,52 @@ public class SimpleRestController {
         msg.append("  Sample: http://localhost:8080/myapp/jbatch/rest/status/23\n");
         msg.append("  will return the state of executionId 23\n\n");
 
-        msg.append("The returned response if of MIME type text/plain and contains the following information\n");
-        msg.append("  {jobExecutionId} (or -1 if no executionId was detected)\\n\n");
-        msg.append("  OK (or FAILURE)\\n\n");
-        msg.append("  followed by command specific information\n");
+        msg.append("* ").append(OP_STOP).append(" - stop the job with the given executionId \n");
+        msg.append("  Sample: http://localhost:8080/myapp/jbatch/rest/stop/23\n");
+        msg.append("  will stop the job with executionId 23\n\n");
+
+        msg.append("* ").append(OP_RESTART).append(" - restart the job with the given executionId \n");
+        msg.append("  Sample: http://localhost:8080/myapp/jbatch/rest/restart/23\n");
+        msg.append("  will restart the job with executionId 23\n\n");
 
         reportFailure(NO_JOB_ID, resp, msg.toString());
     }
 
+    private Properties extractJobProperties(HttpServletRequest req, HttpServletResponse resp) {
+        Properties jobProperties = new Properties();
+        Map<String, String[]> parameterMap = req.getParameterMap();
+        for (Map.Entry<String, String[]> paramEntry : parameterMap.entrySet()) {
+            String key = paramEntry.getKey();
+            if (key == null || key.length() == 0) {
+                reportFailure(NO_JOB_ID, resp, "Parameter key must be set");
+                return null;
+            }
+
+            String[] vals = paramEntry.getValue();
+            if (vals == null || vals.length != 1) {
+                reportFailure(NO_JOB_ID, resp, "Exactly one value must be set for each parameter (parameter name=" + key + ")");
+                return null;
+            }
+            String val = vals[0];
+            jobProperties.put(key, val);
+        }
+        return jobProperties;
+    }
+
+    private Long extractExecutionId(String batchId, HttpServletResponse resp) {
+        if (batchId == null || batchId.isEmpty()) {
+            reportFailure(NO_JOB_ID, resp, "no executionId given");
+            return null;
+        }
+
+        try {
+            return Long.valueOf(batchId);
+        } catch(NumberFormatException nfe) {
+            reportFailure(NO_JOB_ID, resp, "executionId must be numeric, but is " + batchId);
+            return null;
+        }
+    }
+
     private void reportSuccess(long jobId, HttpServletResponse resp, String msg) {
         resp.setStatus(HttpServletResponse.SC_OK);
         writeContent(resp, Long.toString(jobId) + "\n");

http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/209a8d43/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java
----------------------------------------------------------------------
diff --git a/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java b/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java
index 122b270..d6ed626 100644
--- a/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java
+++ b/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java
@@ -17,6 +17,7 @@
 package org.apache.batchee.servlet;
 
 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
+import com.gargoylesoftware.htmlunit.TextPage;
 import com.gargoylesoftware.htmlunit.WebClient;
 import com.gargoylesoftware.htmlunit.html.DomNode;
 import com.gargoylesoftware.htmlunit.html.HtmlPage;
@@ -40,12 +41,55 @@ import java.util.Arrays;
 import java.util.List;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 @RunWith(Arquillian.class)
 public class ServletTest {
     @ArquillianResource
     private URL base;
 
+    @Deployment(testable = false)
+    public static Archive<?> war() {
+        final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "batchee-gui.war")
+                .addAsWebInfResource(new StringAsset(
+                        Descriptors.create(WebAppDescriptor.class)
+                                .metadataComplete(true)
+                                .createListener()
+                                .listenerClass(CreateSomeJobs.class.getName())
+                                .up()
+                                .createFilter()
+                                .filterName("JBatch Private Filter")
+                                .filterClass(JBatchServletInitializer.PrivateFilter.class.getName())
+                                .up()
+                                .createServlet()
+                                .servletName("JBatch")
+                                .servletClass(JBatchController.class.getName())
+                                .loadOnStartup(1)
+                                .up()
+                                .createFilterMapping()
+                                .filterName("JBatch Private Filter")
+                                .urlPattern("/*")
+                                .up()
+                                .createServletMapping()
+                                .servletName("JBatch")
+                                .urlPattern("/jbatch/*")
+                                .up()
+                                .exportAsString()), "web.xml")
+                        // GUI
+                .addPackages(true, JBatchController.class.getPackage())
+                        // test data to create some job things to do this test
+                .addPackage(CreateSomeJobs.class.getPackage())
+                .addAsWebInfResource("META-INF/batch-jobs/init.xml", "classes/META-INF/batch-jobs/init.xml");
+
+        for (final String resource : Arrays.asList("layout.jsp", "jobs.jsp", "job-instances.jsp", "step-executions.jsp",
+                "css/bootstrap.min.3.0.0.css", "js/bootstrap.min.3.0.0.js")) {
+            webArchive.addAsWebResource("META-INF/resources/internal/batchee/" + resource, "internal/batchee/" + resource);
+        }
+
+        return webArchive;
+    }
+
     @Test
     public void home() throws IOException {
         assertEquals("init", extractContent("", "/ul/li/a[1]/text()"));
@@ -67,6 +111,41 @@ public class ServletTest {
         client.getPage(base.toExternalForm() + "jbatch/internal/batchee/jobs.jsp");
     }
 
+    @Test
+    public void testSimpleRest() throws IOException, InterruptedException {
+        String textContent = executeSimpleRest("start/init?value=OK&sleep=2");
+        Long executionId = extractExecutionId(textContent);
+
+        Thread.sleep(100L);
+
+        textContent = executeSimpleRest("status/" + executionId);
+        String[] parms = textContent.split("\n");
+        assertTrue(parms.length == 3);
+        assertEquals(BatchStatus.STARTED.toString(), parms[2]);
+    }
+
+    private String executeSimpleRest(String command) throws IOException {
+        final String startUrl = base.toExternalForm() + "jbatch/rest/" + command;
+        final WebClient webClient = newWebClient();
+        final TextPage page = webClient.getPage(startUrl);
+        String textContent = page.getContent();
+        assertNotNull(textContent);
+        assertTrue(textContent.contains("\nOK\n"));
+        extractExecutionId(textContent);
+
+        return textContent;
+    }
+
+    private Long extractExecutionId(String textContent) {
+        String[] parms = textContent.split("\n");
+        assertTrue(parms.length >= 2);
+        Long executionId = Long.valueOf(parms[0]);
+        assertTrue(-1L != executionId);
+
+        return executionId;
+    }
+
+
     private String extractContent(final String endUrl, final String xpath) throws IOException {
         final String url = base.toExternalForm() + "jbatch/" + endUrl;
         final WebClient webClient = newWebClient();
@@ -90,44 +169,5 @@ public class ServletTest {
         return webClient;
     }
 
-    @Deployment(testable = false)
-    public static Archive<?> war() {
-        final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "batchee-gui.war")
-            .addAsWebInfResource(new StringAsset(
-                Descriptors.create(WebAppDescriptor.class)
-                    .metadataComplete(true)
-                    .createListener()
-                        .listenerClass(CreateSomeJobs.class.getName())
-                    .up()
-                    .createFilter()
-                        .filterName("JBatch Private Filter")
-                        .filterClass(JBatchServletInitializer.PrivateFilter.class.getName())
-                    .up()
-                    .createServlet()
-                        .servletName("JBatch")
-                        .servletClass(JBatchController.class.getName())
-                        .loadOnStartup(1)
-                    .up()
-                    .createFilterMapping()
-                        .filterName("JBatch Private Filter")
-                        .urlPattern("/*")
-                    .up()
-                    .createServletMapping()
-                        .servletName("JBatch")
-                        .urlPattern("/jbatch/*")
-                    .up()
-                    .exportAsString()), "web.xml")
-            // GUI
-            .addPackages(true, JBatchController.class.getPackage())
-            // test data to create some job things to do this test
-            .addPackage(CreateSomeJobs.class.getPackage())
-            .addAsWebInfResource("META-INF/batch-jobs/init.xml", "classes/META-INF/batch-jobs/init.xml");
-
-        for (final String resource : Arrays.asList("layout.jsp", "jobs.jsp", "job-instances.jsp", "step-executions.jsp",
-                                                    "css/bootstrap.min.3.0.0.css", "js/bootstrap.min.3.0.0.js")) {
-            webArchive.addAsWebResource("META-INF/resources/internal/batchee/" + resource, "internal/batchee/" + resource);
-        }
 
-        return webArchive;
-    }
 }


[2/2] git commit: BATCHEE-51 fix unit test

Posted by st...@apache.org.
BATCHEE-51 fix unit test


Project: http://git-wip-us.apache.org/repos/asf/incubator-batchee/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-batchee/commit/50c23cf5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-batchee/tree/50c23cf5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-batchee/diff/50c23cf5

Branch: refs/heads/master
Commit: 50c23cf53b68276bfedad6b98ed03fea9e19565d
Parents: 209a8d4
Author: Mark Struberg <st...@apache.org>
Authored: Sat Aug 9 17:09:00 2014 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Sat Aug 9 17:09:00 2014 +0200

----------------------------------------------------------------------
 .../org/apache/batchee/servlet/ServletTest.java | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/50c23cf5/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java
----------------------------------------------------------------------
diff --git a/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java b/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java
index d6ed626..c992e94 100644
--- a/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java
+++ b/gui/servlet/embedded/src/test/java/org/apache/batchee/servlet/ServletTest.java
@@ -43,6 +43,7 @@ import java.util.List;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 @RunWith(Arquillian.class)
 public class ServletTest {
@@ -122,6 +123,23 @@ public class ServletTest {
         String[] parms = textContent.split("\n");
         assertTrue(parms.length == 3);
         assertEquals(BatchStatus.STARTED.toString(), parms[2]);
+
+        boolean stopped = false;
+        for (int i = 0; i < 11; i++) {
+            textContent = executeSimpleRest("status/" + executionId);
+            parms = textContent.split("\n");
+            assertTrue(parms.length == 3);
+            if (BatchStatus.COMPLETED.toString().equals(parms[2])) {
+                stopped = true;
+                break;
+            }
+
+            Thread.sleep(200L);
+        }
+
+        if (!stopped) {
+            fail("failed to properly stop the batch. Last status = " + parms[2]);
+        }
     }
 
     private String executeSimpleRest(String command) throws IOException {
@@ -152,7 +170,7 @@ public class ServletTest {
 
         final HtmlPage page = webClient.getPage(url);
         final List<?> byXPath = page.getByXPath("//div[@id=\"content\"]" + xpath);
-        assertEquals(1, byXPath.size());
+        assertTrue(byXPath.size() >= 1);
 
         final Object next = byXPath.iterator().next();
         if (!DomNode.class.isInstance(next)) {