Example usage for org.springframework.batch.poller Poller poll

List of usage examples for org.springframework.batch.poller Poller poll

Introduction

In this page you can find the example usage for org.springframework.batch.poller Poller poll.

Prototype

Future<T> poll(Callable<T> callable) throws Exception;

Source Link

Document

Use the callable provided to poll for a non-null result.

Usage

From source file:org.springframework.batch.admin.web.server.JsonIntegrationTests.java

@Test
public void testJobLaunch() throws Exception {

    RestTemplate template = new RestTemplate();
    ResponseEntity<String> result = template.exchange(
            serverRunning.getUrl() + "/jobs/job2.json?jobParameters=fail=true", HttpMethod.POST, null,
            String.class);
    JsonWrapper wrapper = new JsonWrapper(result.getBody());
    // System.err.println(wrapper);
    assertNotNull(wrapper.get("jobExecution.resource"));
    assertNotNull(wrapper.get("jobExecution.status"));
    assertNotNull(wrapper.get("jobExecution.id"));

    // Poll for the completed job execution
    final String resource = wrapper.get("jobExecution.resource", String.class);
    Poller<JsonWrapper> poller = new DirectPoller<JsonWrapper>(100L);
    Future<JsonWrapper> poll = poller.poll(new Callable<JsonWrapper>() {
        public JsonWrapper call() throws Exception {
            RestTemplate template = new RestTemplate();
            ResponseEntity<String> result = template.exchange(resource, HttpMethod.GET, null, String.class);
            JsonWrapper wrapper = new JsonWrapper(result.getBody());
            // System.err.println(wrapper);
            Map<?, ?> map = wrapper.get("jobExecution.stepExecutions", Map.class);
            return map.isEmpty() || wrapper.get("jobExecution.stepExecutions['job2.step1']['resource']") == null
                    ? null//from www. ja va2  s.co m
                    : wrapper;
        }
    });
    JsonWrapper jobExecution = poll.get(500L, TimeUnit.MILLISECONDS);
    assertNotNull(jobExecution);
    // System.err.println(jobExecution);

    // Verify that there is a step execution in the result
    result = template.exchange(
            jobExecution.get("jobExecution.stepExecutions['job2.step1'].resource", String.class),
            HttpMethod.GET, null, String.class);
    wrapper = new JsonWrapper(result.getBody());
    // System.err.println(wrapper);
    assertNotNull(wrapper.get("stepExecution.id"));
    assertNotNull(wrapper.get("stepExecution.status"));
    assertNotNull(wrapper.get("jobExecution.resource"));
    assertNotNull(wrapper.get("jobExecution.status"));
    assertNotNull(wrapper.get("jobExecution.id"));

}

From source file:org.springframework.batch.admin.web.server.JsonIntegrationTests.java

@Test
public void testJobStop() throws Exception {

    RestTemplate template = new RestTemplate();
    ResponseEntity<String> result = template.exchange(serverRunning.getUrl()
            + "/jobs/infinite.json?jobParameters=timestamp=" + System.currentTimeMillis(), HttpMethod.POST,
            null, String.class);
    JsonWrapper wrapper = new JsonWrapper(result.getBody());
    // System.err.println(wrapper);
    assertNotNull(wrapper.get("jobExecution.resource"));
    assertNotNull(wrapper.get("jobExecution.status"));
    assertNotNull(wrapper.get("jobExecution.id"));

    template.exchange(wrapper.get("jobExecution.resource", String.class), HttpMethod.DELETE, null,
            String.class);

    // Poll for the completed job execution
    final String resource = wrapper.get("jobExecution.resource", String.class);
    Poller<JsonWrapper> poller = new DirectPoller<JsonWrapper>(100L);
    Future<JsonWrapper> poll = poller.poll(new Callable<JsonWrapper>() {
        public JsonWrapper call() throws Exception {
            RestTemplate template = new RestTemplate();
            ResponseEntity<String> result = template.exchange(resource, HttpMethod.GET, null, String.class);
            JsonWrapper wrapper = new JsonWrapper(result.getBody());
            // System.err.println(wrapper);
            BatchStatus status = wrapper.get("jobExecution.status", BatchStatus.class);
            return status.isGreaterThan(BatchStatus.STOPPING) ? wrapper : null;
        }/*from ww  w .  j a v  a2  s  . c o m*/
    });
    JsonWrapper jobExecution = poll.get(500L, TimeUnit.MILLISECONDS);
    assertNotNull(jobExecution);

    BatchStatus status = jobExecution.get("jobExecution.status", BatchStatus.class);
    assertEquals(BatchStatus.STOPPED, status);

}

From source file:org.springframework.cloud.task.batch.partition.DeployerPartitionHandler.java

private Collection<StepExecution> pollReplies(final StepExecution masterStepExecution,
        final Set<StepExecution> executed, final Set<StepExecution> candidates, final int size)
        throws Exception {

    final Collection<StepExecution> result = new ArrayList<>(executed.size());

    Callable<Collection<StepExecution>> callback = new Callable<Collection<StepExecution>>() {
        @Override//from w  ww.  j a v  a  2 s  .  c  o m
        public Collection<StepExecution> call() throws Exception {
            Set<StepExecution> newExecuted = new HashSet<>();

            for (StepExecution curStepExecution : executed) {
                if (!result.contains(curStepExecution)) {
                    StepExecution partitionStepExecution = jobExplorer.getStepExecution(
                            masterStepExecution.getJobExecutionId(), curStepExecution.getId());

                    if (isComplete(partitionStepExecution.getStatus())) {
                        result.add(partitionStepExecution);
                        currentWorkers--;

                        if (!candidates.isEmpty()) {

                            launchWorkers(candidates, newExecuted);
                            candidates.removeAll(newExecuted);
                        }
                    }
                }
            }

            executed.addAll(newExecuted);

            if (result.size() == size) {
                return result;
            } else {
                return null;
            }
        }
    };

    Poller<Collection<StepExecution>> poller = new DirectPoller<>(this.pollInterval);
    Future<Collection<StepExecution>> resultsFuture = poller.poll(callback);

    if (timeout >= 0) {
        return resultsFuture.get(timeout, TimeUnit.MILLISECONDS);
    } else {
        return resultsFuture.get();
    }
}