Example usage for org.springframework.batch.poller DirectPoller DirectPoller

List of usage examples for org.springframework.batch.poller DirectPoller DirectPoller

Introduction

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

Prototype

public DirectPoller(long interval) 

Source Link

Usage

From source file:com.enterra.batch.admin.sample.BootstrapTests.java

@Test
public void testServletConfiguration() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml" },
            parent);/*from w  ww  .  ja  va 2s  .co  m*/

    assertTrue(context.containsBean("jobRepository"));
    String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(),
            JobController.class);
    assertEquals(1, beanNames.length);

    Job job = context.getBean(JobRegistry.class).getJob("job1");
    final JobExecution jobExecution = parent.getBean(JobLauncher.class).run(job,
            new JobParametersBuilder().addString("fail", "false").toJobParameters());

    new DirectPoller<BatchStatus>(100).poll(new Callable<BatchStatus>() {
        public BatchStatus call() throws Exception {
            BatchStatus status = jobExecution.getStatus();
            if (status.isLessThan(BatchStatus.STOPPED) && status != BatchStatus.COMPLETED) {
                return null;
            }
            return status;
        }
    }).get(2000, TimeUnit.MILLISECONDS);

    context.close();
    parent.close();

    assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

}

From source file:org.springframework.batch.admin.BootstrapTests.java

@Test
public void testServletConfiguration() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml" },
            parent);/* ww  w.  ja v  a2  s  .  c o m*/

    assertTrue(context.containsBean("jobRepository"));
    String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(),
            JobController.class);
    assertEquals(1, beanNames.length);

    MenuManager menuManager = context.getBean(MenuManager.class);
    assertEquals(4, menuManager.getMenus().size());

    context.refresh();

    ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/test-job-context.xml" }, parent);
    Job job = child.getBean(Job.class);
    final JobExecution jobExecution = parent.getBean(JobLauncher.class).run(job, new JobParameters());

    new DirectPoller<BatchStatus>(100).poll(new Callable<BatchStatus>() {
        public BatchStatus call() throws Exception {
            BatchStatus status = jobExecution.getStatus();
            if (status.isLessThan(BatchStatus.STOPPED) && status != BatchStatus.COMPLETED) {
                return null;
            }
            return status;
        }
    }).get(2000, TimeUnit.MILLISECONDS);

    HomeController metaData = new HomeController();
    metaData.setApplicationContext(context);
    metaData.afterPropertiesSet();
    MockHttpServletRequest request = new MockHttpServletRequest();
    ModelMap model = new ModelMap();
    metaData.getResources(request, model);
    @SuppressWarnings("unchecked")
    List<ResourceInfo> resources = (List<ResourceInfo>) model.get("resources");
    StringBuilder content = new StringBuilder();
    for (ResourceInfo resourceInfo : resources) {
        content.append(resourceInfo.getMethod() + resourceInfo.getUrl() + "=\n");
    }
    FileUtils.writeStringToFile(new File("target/resources.properties"), content.toString());

    HomeController home = context.getBean(HomeController.class);
    // System.err.println(home.getUrlPatterns());
    assertTrue(home.getUrlPatterns().contains("/jobs/{jobName}"));

    String message = context.getMessage("GET/jobs/{jobName}", new Object[0], Locale.getDefault());
    assertTrue("No message for /jobs/{jobName}", StringUtils.hasText(message));

    child.close();
    context.close();
    parent.close();

    assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

}

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  w w w  . j a  v  a 2s . c om*/
                    : 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;
        }//  w w w  . j a  va2  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  w  w.j a v a 2s  .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();
    }
}