Skip to content

Commit

Permalink
Issue #72 fix PurgeBatchlet to work with the JdbcRepository wrapper i…
Browse files Browse the repository at this point in the history
…n WildFly, and add wildfly-jberet-samples/purgeJdbcRepository2.
  • Loading branch information
chengfang committed May 6, 2016
1 parent 26a9b88 commit f02aa54
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,7 @@ public interface BatchMessages {
@Message(id = 653, value = "Failed to deserialize: %s")
BatchRuntimeException failedToDeserialize(@Cause Throwable cause, Serializable value);

@Message(id = 654, value = "Failed to get JdbcRepository.")
BatchRuntimeException failedToGetJdbcRepository(@Cause Throwable cause);

}
63 changes: 53 additions & 10 deletions jberet-core/src/main/java/org/jberet/repository/PurgeBatchlet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Red Hat, Inc. and/or its affiliates.
* Copyright (c) 2015-2016 Red Hat, Inc. and/or its affiliates.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -12,6 +12,7 @@

package org.jberet.repository;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Set;
import javax.batch.api.BatchProperty;
Expand All @@ -20,9 +21,10 @@
import javax.batch.runtime.context.StepContext;
import javax.inject.Inject;

import org.jberet._private.BatchMessages;
import org.jberet.runtime.context.JobContextImpl;

public final class PurgeBatchlet implements Batchlet {
public class PurgeBatchlet implements Batchlet {
@Inject
private JobContext jobContext;

Expand Down Expand Up @@ -131,23 +133,64 @@ public String process() throws Exception {
jobRepository.removeJobExecutions(selector);
}

if (jobRepository instanceof JdbcRepository) {
if (sql != null) {
((JdbcRepository) jobRepository).executeStatements(sql, null);
} else if (sqlFile != null) {
((JdbcRepository) jobRepository).executeStatements(null, sqlFile);
if (sql != null) {
sql = sql.trim();
if (sql.isEmpty()) {
sql = null;
}
} else if (jobRepository instanceof MongoRepository) {
if (mongoRemoveQueries != null) {
((MongoRepository) jobRepository).executeRemoveQueries(mongoRemoveQueries);
}
if (sqlFile != null) {
sqlFile = sqlFile.trim();
if (sqlFile.isEmpty()) {
sqlFile = null;
}
}
if (sql != null || sqlFile != null) {
final JdbcRepository jdbcRepository = getJdbcRepository(jobRepository);
if (jdbcRepository != null) {
jdbcRepository.executeStatements(sql, sqlFile);
}
}

if (mongoRemoveQueries != null && jobRepository instanceof MongoRepository) {
((MongoRepository) jobRepository).executeRemoveQueries(mongoRemoveQueries);
}

return null;
}

@Override
public void stop() throws Exception {
}

/**
* Gets the {@code org.jberet.repository.JdbcRepository} from the
* {@code org.jberet.repository.JobRepository} passed in, in order to
* perform operations specific to {@code org.jberet.repository.JdbcRepository}.
*
* @param repo {@code JobRepository}
* @return {@code org.jberet.repository.JdbcRepository}
*/
protected JdbcRepository getJdbcRepository(final JobRepository repo) {
if (repo instanceof JdbcRepository) {
return (JdbcRepository) repo;
}
if (repo instanceof InMemoryRepository || repo instanceof MongoRepository
|| repo instanceof InfinispanRepository) {
return null;
}

try {
final Method getDelegateMethod = repo.getClass().getDeclaredMethod("getDelegate");
if (!getDelegateMethod.isAccessible()) {
getDelegateMethod.setAccessible(true);
}
final Object result = getDelegateMethod.invoke(repo);
return (result instanceof JdbcRepository) ? (JdbcRepository) result : null;
} catch (final NoSuchMethodException e) {
return null;
} catch (final Exception e) {
throw BatchMessages.MESSAGES.failedToGetJdbcRepository(e);
}
}
}
5 changes: 5 additions & 0 deletions wildfly-jberet-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<module>restWriter</module>
<module>scheduleExecutor</module>
<module>scheduleTimer</module>
<module>purgeJdbcRepository2</module>
</modules>

<properties>
Expand Down Expand Up @@ -111,6 +112,10 @@
<groupId>org.jberet</groupId>
<artifactId>jberet-rest-api</artifactId>
</dependency>
<dependency>
<groupId>org.jberet</groupId>
<artifactId>jberet-schedule-executor</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down
86 changes: 86 additions & 0 deletions wildfly-jberet-samples/purgeJdbcRepository2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright (c) 2016 Red Hat, Inc. and/or its affiliates.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
Contributors:
Cheng Fang - Initial API and implementation
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>wildfly-jberet-samples</artifactId>
<groupId>org.jberet.samples</groupId>
<version>1.3.0.Beta2-SNAPSHOT</version>
</parent>

<artifactId>purgeJdbcRepository</artifactId>

<packaging>war</packaging>

<name>purgeJdbcRepository Maven Webapp</name>

<dependencies>
<dependency>
<groupId>org.jberet.samples</groupId>
<artifactId>jberet-samples-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<finalName>purgeJdbcRepository2</finalName>
</build>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>wildfly</id>
<activation>
<property>
<name>wildfly</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright (c) 2016 Red Hat, Inc. and/or its affiliates.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
Contributors:
Cheng Fang - Initial API and implementation
-->

<job id="prepurge2" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<step id="prepurge2.step1">
<batchlet>
<script type="javascript">
function process() {
print('Running prepurge2.step1');
return null;
}
</script>
</batchlet>
</step>
</job>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright (c) 2016 Red Hat, Inc. and/or its affiliates.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
Contributors:
Cheng Fang - Initial API and implementation
-->

<job id="purgeJdbcRepository2" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<step id="purgeJdbcRepository2.step1">
<batchlet ref="org.jberet.repository.PurgeBatchlet">
<properties>
<property name="sql" value="#{jobParameters['sql']}"/>
<property name="sqlFile" value="#{jobParameters['sqlFile']}"/>

<property name="jobExecutionSelector" value="#{jobParameters['jobExecutionSelector']}"/>
<property name="keepRunningJobExecutions" value="#{jobParameters['keepRunningJobExecutions']}"/>
<property name="purgeJobsByNames" value="#{jobParameters['purgeJobsByNames']}"/>
<property name="jobExecutionIds" value="#{jobParameters['jobExecutionIds']}"/>
<property name="numberOfRecentJobExecutionsToKeep"
value="#{jobParameters['numberOfRecentJobExecutionsToKeep']}"/>
<property name="jobExecutionIdFrom" value="#{jobParameters['jobExecutionIdFrom']}"/>
<property name="jobExecutionIdTo" value="#{jobParameters['jobExecutionIdTo']}"/>
<property name="withinPastMinutes" value="#{jobParameters['withinPastMinutes']}"/>
<property name="jobExecutionEndTimeFrom" value="#{jobParameters['jobExecutionEndTimeFrom']}"/>
<property name="jobExecutionEndTimeTo" value="#{jobParameters['jobExecutionEndTimeTo']}"/>
<property name="batchStatuses" value="#{jobParameters['batchStatuses']}"/>
<property name="exitStatuses" value="#{jobParameters['exitStatuses']}"/>
<property name="jobExecutionsByJobNames" value="#{jobParameters['jobExecutionsByJobNames']}"/>
</properties>
</batchlet>
</step>
</job>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
delete from STEP_EXECUTION where JOBEXECUTIONID in
(select JOBEXECUTIONID from JOB_EXECUTION, JOB_INSTANCE
where JOB_EXECUTION.JOBINSTANCEID = JOB_INSTANCE.JOBINSTANCEID and JOB_INSTANCE.JOBNAME = 'prepurge2');


delete from JOB_EXECUTION where JOBEXECUTIONID in
(select JOBEXECUTIONID from JOB_EXECUTION, JOB_INSTANCE
where JOB_EXECUTION.JOBINSTANCEID = JOB_INSTANCE.JOBINSTANCEID and JOB_INSTANCE.JOBNAME = 'prepurge2')
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="com.google.guava"/>
</dependencies>
</deployment>
</jboss-deployment-structure>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2014-2016 Red Hat, Inc. and/or its affiliates.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Cheng Fang - Initial API and implementation
*/

package org.jberet.samples.wildfly.purgeJdbcRepository2;

import java.util.Properties;
import javax.batch.runtime.BatchStatus;

import org.jberet.rest.client.BatchClient;
import org.jberet.rest.entity.JobExecutionEntity;
import org.jberet.samples.wildfly.common.BatchTestBase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Tests to verify {@code PurgeBatchlet} in JBoss EAP and WildFly.
*
* Similar tests for Java SE environment are:
* <ul>
* <li>test-apps/purgeJdbcRepository
* <li>test-apps/purgeInMemoryRepository
* <li>test-apps/purgeMongoRepository
* </ul>
*/
public final class PurgeJdbcRepository2IT extends BatchTestBase {
/**
* The job name defined in {@code META-INF/batch-jobs/purgeJdbcRepository2.xml}
*/
private static final String jobName = "purgeJdbcRepository2";

/**
* The job name defined in {@code META-INF/batch-jobs/prepurge2.xml}
*/
private static final String prepurge2JobName = "prepurge2";

/**
* The full REST API URL, including scheme, hostname, port number, context path, servlet path for REST API.
* For example, "http://localhost:8080/testApp/api"
*/
private static final String restUrl = BASE_URL + "purgeJdbcRepository2/api";

private BatchClient batchClient = new BatchClient(restUrl);

@Override
protected BatchClient getBatchClient() {
return batchClient;
}

@Test
public void withSql() throws Exception {
final long prepurgeExecutionId = prepurge();
JobExecutionEntity prepurgeJobExecution = batchClient.getJobExecution(prepurgeExecutionId);
assertEquals(BatchStatus.COMPLETED, prepurgeJobExecution.getBatchStatus());

final String sql =
"delete from STEP_EXECUTION where JOBEXECUTIONID in " +
"(select JOBEXECUTIONID from JOB_EXECUTION, JOB_INSTANCE " +
"where JOB_EXECUTION.JOBINSTANCEID = JOB_INSTANCE.JOBINSTANCEID and JOB_INSTANCE.JOBNAME like 'prepurge%'); " +

"delete from JOB_EXECUTION where JOBINSTANCEID in " +
"(select JOBINSTANCEID from JOB_INSTANCE where JOBNAME like 'prepurge%');";


final Properties jobParams = new Properties();
jobParams.setProperty("sql", sql);
startJobCheckStatus(jobName, jobParams, 3000, BatchStatus.COMPLETED);

// prepurgeJobExecution = batchClient.getJobExecution(prepurgeExecutionId);
// assertEquals(null, prepurgeJobExecution);
}

private long prepurge() throws Exception {
final JobExecutionEntity jobExecutionEntity = batchClient.startJob(prepurge2JobName, null);
Thread.sleep(1000);
return jobExecutionEntity.getExecutionId();
}

}

0 comments on commit f02aa54

Please sign in to comment.