Java Code Examples for javax.batch.runtime.BatchRuntime#getJobOperator()

The following examples show how to use javax.batch.runtime.BatchRuntime#getJobOperator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CamelProcessorTest.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@Test
public void process() throws Exception {
    final List<Exchange> exchanges = new ArrayList<Exchange>(2);
    final Consumer consumer = CamelBridge.CONTEXT.getEndpoint("direct:processor").createConsumer(new Processor() {
        @Override
        public void process(final Exchange exchange) throws Exception {
            exchanges.add(exchange);
        }
    });
    consumer.start();

    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    Batches.waitForEnd(jobOperator, jobOperator.start("camel-processor", new Properties()));
    assertEquals(StoreItems.ITEMS.size(), 2);
    assertEquals(exchanges.size(), 2);

    for (int i = 1; i <= 2; i++) {
        assertEquals("" + i, StoreItems.ITEMS.get(i - 1));
        assertEquals("" + i, exchanges.get(i - 1).getIn().getBody());
    }

    consumer.stop();
}
 
Example 2
Source File: JSefaFlrReaderConverterTest.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@Test
public void testRead() throws Exception {
    String path = "target/work/JSefaFlrReaderWithConverter.csv";

    IOs.write(path,
              "string1   123      1  201007161200\n" +
              "string2   345      2  199004041350\n" +
              "string3   9876543211  197905072358");

    Properties props = new Properties();
    props.setProperty("input", path);

    JobOperator operator = BatchRuntime.getJobOperator();
    Batches.waitForEnd(operator, operator.start("jsefa-flr-reader-converter", props));

    Set<RecordWithConverter> expectedItems = new HashSet<RecordWithConverter>();
    expectedItems.add(new RecordWithConverter("string1", 123L, RecordWithConverter.RecordEnum.ONE, new GregorianCalendar(2010, Calendar.JULY, 16, 12, 0).getTime()));
    expectedItems.add(new RecordWithConverter("string2", 345L, RecordWithConverter.RecordEnum.TWO, new GregorianCalendar(1990, Calendar.APRIL, 4, 13, 50).getTime()));
    expectedItems.add(new RecordWithConverter("string3", 987654321L, RecordWithConverter.RecordEnum.ONE, new GregorianCalendar(1979, Calendar.MAY, 7, 23, 58).getTime()));

    Assert.assertEquals(Storage.STORAGE.size(), expectedItems.size());
    Assert.assertTrue(Storage.STORAGE.containsAll(expectedItems));
}
 
Example 3
Source File: SimpleErrorChunkUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenChunkError_thenErrorSkipped_CompletesWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("simpleErrorSkipChunk", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    for (StepExecution stepExecution : stepExecutions) {
        if (stepExecution.getStepName()
            .equals("errorStep")) {
            jobOperator.getStepExecutions(executionId)
            .stream()
            .map(BatchTestHelper::getProcessSkipCount)
            .forEach(skipCount -> assertEquals(1L, skipCount.longValue()));
        }
    }
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 4
Source File: JSefaCsvWriterTest.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
private void startBatchAndAssertResult(String path,
                                       String jobName,
                                       Properties jobProperties,
                                       List storage) {

    jobProperties.setProperty("output", path);

    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Batches.waitForEnd(jobOperator, jobOperator.start(jobName, jobProperties));

    List<String> batchOutput = IOs.getLines(path);
    assertEquals(batchOutput.size(), storage.size());

    for (int i = 0; i < batchOutput.size(); i++) {
        assertEquals(batchOutput.get(i), CsvUtil.toCsv(storage.get(i)));
    }
}
 
Example 5
Source File: CommonsCsvReaderWithDefaultMapperTest.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@Test
public void header() throws Exception {
    final String path = "target/work/CommonsCsvReaderWithDefaultMapperTestheader.txt";

    final Properties jobParams = new Properties();
    jobParams.setProperty("input", path);

    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    IOs.write(path, "c1,c2,int\nv11,v12\nv21,v22,1\nv31,v32,2");
    Batches.waitForEnd(jobOperator, jobOperator.start("csv-reader-defaultmappername", jobParams));

    final int size = StoreItems.ITEMS.size();
    assertEquals(size, 3);
    for (int i = 1; i <= size; i++) {
        final Named record = Named.class.cast(StoreItems.ITEMS.get(i - 1));
        assertEquals("v" + i + "1", record.c1);
        assertEquals("v" + i + "2", record.c2);
    }
}
 
Example 6
Source File: JacksonJSonWriterTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Test
public void writeWithFields() {
    final JobOperator operator = BatchRuntime.getJobOperator();
    Batches.waitForEnd(operator, operator.start("jackson-field-writer", new Properties()));
    final String output = IOs.slurp("target/work/jackson-field-output.json");
    assertEquals(output.replace("\n", "").replace("\r", "").replace(" ", "").replace("\t", ""),
        "{\"item1\":{\"v1\":\"v11\",\"v2\":\"v21\"},\"item2\":{\"v1\":\"v12\",\"v2\":\"v22\"}}");
}
 
Example 7
Source File: DefaultDataRepresentationServiceTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Test
public void testBatchExecutionWithCheckpoints() {
    final JobOperator op = BatchRuntime.getJobOperator();
    final long id = op.start("checkpoint-storage-test", null);
    Batches.waitForEnd(op, id);

    final List<StepExecution> steps = op.getStepExecutions(id);
    assertEquals(1, steps.size());
    final StepExecution exec = steps.iterator().next();
    Assert.assertEquals(BatchStatus.FAILED, exec.getBatchStatus());

    // now try to restart the batch again
    long restartId = op.restart(id, null);
    Batches.waitForEnd(op, restartId);
}
 
Example 8
Source File: AsyncProcessorTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Test
public void async() throws Exception {
    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    final long id = jobOperator.start("async-processor", null);
    Batches.waitForEnd(jobOperator, id);
    assertEquals(BatchStatus.COMPLETED, jobOperator.getJobExecution(id).getBatchStatus());
}
 
Example 9
Source File: ModelMapperProcessorTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Test
public void map() {
    final JobOperator operator = BatchRuntime.getJobOperator();
    Batches.waitForEnd(operator, operator.start("modelmapper-processor", null));
    assertNotNull(Writer.items);
    assertEquals(2, Writer.items.size());
    assertTrue(Writer.items.contains(new B("1", "#1")));
    assertTrue(Writer.items.contains(new B("2", "#2")));
}
 
Example 10
Source File: JBatchController.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final ServletConfig config) throws ServletException {
    this.operator = BatchRuntime.getJobOperator();

    this.context = config.getServletContext().getContextPath();
    if ("/".equals(context)) {
        context = "";
    }

    mapping = context + mapping;
    this.simpleRestController = new SimpleRestController(operator);

    // this is not perfect but when it works it list jobs not executed yet which is helpful
    // try to find not yet started jobs
    if (defaultScan) {
        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
        final Enumeration<URL> resources;
        try {
            resources = loader.getResources("META-INF/batch-jobs");
        } catch (final IOException e) {
            return;
        }
        while (resources.hasMoreElements()) {
            final URL url = resources.nextElement();
            final File file = toFile(url);

            if (file != null) {
                if (file.isDirectory()) {
                    findInDirectory(file);
                } else {
                    findInJar(file);
                }
            }
        }
    }
}
 
Example 11
Source File: JdbcReaderTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Test
public void read() throws Exception {
    {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        final Connection c = DriverManager.getConnection("jdbc:derby:memory:jdbcreader;create=true", "app", "app");

        PreparedStatement statement = c.prepareStatement("CREATE TABLE FOO("
            + "id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) CONSTRAINT FOO_PK PRIMARY KEY,"
            + "name VARCHAR(512))");
        statement.executeUpdate();
        statement.close();

        statement = c.prepareStatement("INSERT INTO FOO (name) VALUES(?)");
        statement.setString(1, "toto");
        statement.executeUpdate();
        statement.close();

        statement = c.prepareStatement("INSERT INTO FOO (name) VALUES(?)");
        statement.setString(1, "titi");
        statement.executeUpdate();
        statement.close();

        c.close();
    }

    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    Batches.waitForEnd(jobOperator, jobOperator.start("jdbc-reader", new Properties()));
    assertEquals(StoreItems.ITEMS.size(), 2);
    assertTrue(StoreItems.ITEMS.contains("titi"));
    assertTrue(StoreItems.ITEMS.contains("toto"));
}
 
Example 12
Source File: BatchExecutionServlet.java    From wow-auctions with GNU General Public License v3.0 5 votes vote down vote up
private void processJob(AuctionFile auctionFile) {
    Properties jobParameters = new Properties();
    JobOperator jobOperator = BatchRuntime.getJobOperator();

    jobParameters.setProperty("realmId", auctionFile.getRealm().getId().toString());
    jobParameters.setProperty("auctionFileId", auctionFile.getId().toString());

    jobOperator.start("process-job", jobParameters);
}
 
Example 13
Source File: JobSequenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenFlow_thenBatch_CompleteWithSuccess() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("flowJobSequence", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);
    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
    assertEquals(3 , jobOperator.getStepExecutions(executionId).size());
    assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
 
Example 14
Source File: NoStateTypedItemReaderTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoRead() throws Exception {
    DummyItemWriter.items.clear();

    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Batches.waitForEnd(jobOperator, jobOperator.start("no-state-reader", new Properties()));

    Assert.assertFalse(DummyItemWriter.items.isEmpty(), "items must be not empty");
    Assert.assertEquals(DummyItemWriter.items.size(), 4);
    Assert.assertEquals(DummyItemWriter.items.get(0), "A");
    Assert.assertEquals(DummyItemWriter.items.get(1), "B");
    Assert.assertEquals(DummyItemWriter.items.get(2), "3");
    Assert.assertEquals(DummyItemWriter.items.get(3), "4");
}
 
Example 15
Source File: PartitionIdTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Test
public void run() {
    final JobOperator op = BatchRuntime.getJobOperator();
    final long id = op.start("partition-execId", null);
    Batches.waitForEnd(op, id);

    final long stepId = op.getStepExecutions(id).iterator().next().getStepExecutionId();

    assertEquals(2, Reader.jobIds.size());
    assertEquals(2, Reader.stepIds.size());
    assertEquals(id, Reader.jobIds.get(1).longValue());
    assertEquals(Reader.jobIds.get(0), Reader.jobIds.get(1));
    assertEquals(stepId, Reader.stepIds.get(0).longValue());
    assertEquals(Reader.stepIds.get(0), Reader.stepIds.get(1));
}
 
Example 16
Source File: BeanValidationProcessorTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
private BatchStatus process(final String isOk) {
    final Properties jobParameters = new Properties();
    jobParameters.setProperty("ok", isOk);
    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    final long id = jobOperator.start("bean-validation-processor", jobParameters);
    Batches.waitForEnd(jobOperator, id);
    return jobOperator.getJobExecution(id).getBatchStatus();
}
 
Example 17
Source File: CreateSomeJobs.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(final ServletContextEvent sce) {
    final JobOperator operator = BatchRuntime.getJobOperator();

    try { // initialize only once to ensure we can use in tests ids
        operator.getJobInstances("init", 0, 10);
    } catch (final NoSuchJobException nsje) {
        final Properties jobParameters = new Properties();
        jobParameters.setProperty("test", "jbatch");
        final long id = operator.start("init", jobParameters);
        Batches.waitForEnd(operator, id);
    }
}
 
Example 18
Source File: JMXTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void createAJob() throws Exception {
    server = ManagementFactory.getPlatformMBeanServer();
    on = new ObjectName(BatchEEMBean.DEFAULT_OBJECT_NAME);

    final JobOperator jobOperator = BatchRuntime.getJobOperator();
    clearPersistence(jobOperator);

    id = jobOperator.start("jmx", new Properties() {{ setProperty("foo", "bar"); }});
    Batches.waitForEnd(jobOperator, id);
}
 
Example 19
Source File: JBatchComponent.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
public JBatchComponent() {
    operator = BatchRuntime.getJobOperator();
}
 
Example 20
Source File: BatchEEMBeanImpl.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
private JobOperator operator() { // lazy since we register jmx with the servicesmanager init
    return BatchRuntime.getJobOperator();
}