Skip to content

Commit 0a4154e

Browse files
committed
Springbatch transactionManager feature
1 parent 19b4980 commit 0a4154e

53 files changed

Lines changed: 1880 additions & 32 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

batch/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
<version>${seed.version}</version>
7171
<scope>test</scope>
7272
</dependency>
73+
<dependency>
74+
<groupId>org.seedstack.business</groupId>
75+
<artifactId>business-core</artifactId>
76+
<version>${business.version}</version>
77+
<scope>test</scope>
78+
</dependency>
7379
<dependency>
7480
<groupId>org.springframework.batch</groupId>
7581
<artifactId>spring-batch-infrastructure</artifactId>
@@ -82,6 +88,24 @@
8288
<version>${spring.version}</version>
8389
<scope>test</scope>
8490
</dependency>
91+
<dependency>
92+
<groupId>org.springframework</groupId>
93+
<artifactId>spring-orm</artifactId>
94+
<version>${spring.version}</version>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.springframework.data</groupId>
99+
<artifactId>spring-data-jpa</artifactId>
100+
<version>1.6.4.RELEASE</version>
101+
<scope>test</scope>
102+
</dependency>
103+
<dependency>
104+
<groupId>org.hibernate</groupId>
105+
<artifactId>hibernate-entitymanager</artifactId>
106+
<version>4.3.4.Final</version>
107+
<scope>test</scope>
108+
</dependency>
85109
<dependency>
86110
<groupId>org.hsqldb</groupId>
87111
<artifactId>hsqldb</artifactId>
@@ -94,5 +118,11 @@
94118
<version>${logback.version}</version>
95119
<scope>test</scope>
96120
</dependency>
121+
<dependency>
122+
<groupId>org.seedstack.addons.jpa</groupId>
123+
<artifactId>jpa</artifactId>
124+
<version>${jpa-addon.version}</version>
125+
<scope>test</scope>
126+
</dependency>
97127
</dependencies>
98128
</project>

batch/src/it/java/org/seedstack/seed/springbatch/SpringBatchCommandHandlerIT.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
77
*/
88
package org.seedstack.seed.springbatch;
99

10-
import com.google.inject.ConfigurationException;
11-
import com.google.inject.Injector;
12-
import com.google.inject.Key;
13-
import com.google.inject.name.Names;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Fail.fail;
12+
13+
import javax.inject.Inject;
14+
import javax.inject.Named;
15+
1416
import org.junit.Before;
1517
import org.junit.Test;
1618
import org.seedstack.seed.cli.WithCommandLine;
1719
import org.seedstack.seed.it.AbstractSeedIT;
20+
import org.seedstack.seed.springbatch.sample.domain.ContactDao;
1821
import org.seedstack.spring.WithApplicationContexts;
1922
import org.springframework.batch.core.repository.JobRepository;
2023

21-
import javax.inject.Inject;
22-
import javax.inject.Named;
23-
24-
import static org.assertj.core.api.Assertions.assertThat;
25-
import static org.assertj.core.api.Fail.fail;
24+
import com.google.inject.ConfigurationException;
25+
import com.google.inject.Injector;
26+
import com.google.inject.Key;
27+
import com.google.inject.name.Names;
2628

2729
/**
2830
* @author epo.jemba@ext.mpsa.com
@@ -32,6 +34,9 @@ public class SpringBatchCommandHandlerIT extends AbstractSeedIT {
3234
@Inject
3335
@Named("jobRepository")
3436
JobRepository jobRepository;
37+
38+
@Inject
39+
ContactDao contactDao;
3540

3641
@Inject
3742
Injector injector;
@@ -74,4 +79,22 @@ public void execute_batch_with_multiple_parameters() {
7479
assertThat(passedBefore).isTrue();
7580
assertThat(jobRepository).isNotNull();
7681
}
82+
83+
@Test
84+
@WithCommandLine(command = "run-job", args = {"--job", "fileUploadJob"}, expectedExitCode = 0)
85+
@WithApplicationContexts({"META-INF/spring/spring-context-batch.xml", "META-INF/spring/spring-context-orm.xml", "META-INF/spring/spring-context.xml"})
86+
public void execute_batch_with_spring_transactionmanager() {
87+
assertThat(passedBefore).isTrue();
88+
assertThat(contactDao).isNotNull();
89+
assertThat(contactDao.getEntityManager()).isNotNull();
90+
}
91+
@Test
92+
@WithCommandLine(command = "run-job", args = {"--job", "fileUploadJobWith2Manager"}, expectedExitCode = 0)
93+
@WithApplicationContexts({"META-INF/spring/spring-context-batch.xml", "META-INF/spring/spring-context-orm.xml", "META-INF/spring/spring-context.xml"})
94+
public void execute_threaded_batch_with_2_spring_transactionmanager() {
95+
assertThat(passedBefore).isTrue();
96+
assertThat(contactDao).isNotNull();
97+
assertThat(contactDao.getEntityManager()).isNotNull();
98+
}
99+
77100
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.seed.springbatch.sample.domain;
9+
10+
import javax.persistence.Column;
11+
import javax.persistence.Entity;
12+
import javax.persistence.GeneratedValue;
13+
import javax.persistence.GenerationType;
14+
import javax.persistence.Id;
15+
import javax.persistence.Table;
16+
17+
import org.seedstack.business.domain.BaseAggregateRoot;
18+
19+
@Entity
20+
@Table(name = "CONTACTS")
21+
public class Contact extends BaseAggregateRoot<Long>{
22+
23+
@Id
24+
@GeneratedValue(strategy = GenerationType.IDENTITY)
25+
@Column(name = "ID")
26+
private Long id;
27+
28+
@Column(name = "FIRST_NAME")
29+
private String firstName;
30+
31+
@Column(name = "LAST_NAME")
32+
private String lastName;
33+
34+
@Column(name = "EMAIL")
35+
private String email;
36+
37+
public String getFirstName() {
38+
return firstName;
39+
}
40+
41+
public Long getId() {
42+
return id;
43+
}
44+
45+
public void setId(Long id) {
46+
this.id = id;
47+
}
48+
49+
public void setFirstName(String firstName) {
50+
this.firstName = firstName;
51+
}
52+
53+
public String getLastName() {
54+
return lastName;
55+
}
56+
57+
public void setLastName(String lastName) {
58+
this.lastName = lastName;
59+
}
60+
61+
public String getEmail() {
62+
return email;
63+
}
64+
65+
public void setEmail(String email) {
66+
this.email = email;
67+
}
68+
69+
@Override
70+
public Long getEntityId() {
71+
return id;
72+
}
73+
74+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.seed.springbatch.sample.domain;
9+
10+
import javax.persistence.EntityManager;
11+
12+
import org.seedstack.business.domain.Repository;
13+
14+
public interface ContactDao extends Repository<Contact, Long>{
15+
public EntityManager getEntityManager();
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.seed.springbatch.sample.domain;
9+
10+
import javax.persistence.EntityManager;
11+
12+
import org.seedstack.jpa.BaseJpaRepository;
13+
14+
public class ContactDaoImpl extends BaseJpaRepository<Contact, Long> implements ContactDao {
15+
16+
@Override
17+
public EntityManager getEntityManager() {
18+
return entityManager;
19+
}
20+
21+
22+
23+
24+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.seed.springbatch.sample.domain;
9+
10+
import javax.persistence.Column;
11+
import javax.persistence.Entity;
12+
import javax.persistence.GeneratedValue;
13+
import javax.persistence.GenerationType;
14+
import javax.persistence.Id;
15+
import javax.persistence.Table;
16+
17+
import org.seedstack.business.domain.BaseAggregateRoot;
18+
19+
@Entity
20+
@Table(name = "USER")
21+
public class User extends BaseAggregateRoot<Long>{
22+
23+
@Id
24+
@GeneratedValue(strategy = GenerationType.IDENTITY)
25+
@Column(name = "ID")
26+
private Long id;
27+
28+
@Column(name = "FIRST_NAME")
29+
private String firstName;
30+
31+
@Column(name = "LAST_NAME")
32+
private String lastName;
33+
34+
@Column(name = "EMAIL")
35+
private String email;
36+
37+
public String getFirstName() {
38+
return firstName;
39+
}
40+
41+
public Long getId() {
42+
return id;
43+
}
44+
45+
public void setId(Long id) {
46+
this.id = id;
47+
}
48+
49+
public void setFirstName(String firstName) {
50+
this.firstName = firstName;
51+
}
52+
53+
public String getLastName() {
54+
return lastName;
55+
}
56+
57+
public void setLastName(String lastName) {
58+
this.lastName = lastName;
59+
}
60+
61+
public String getEmail() {
62+
return email;
63+
}
64+
65+
public void setEmail(String email) {
66+
this.email = email;
67+
}
68+
69+
@Override
70+
public Long getEntityId() {
71+
return id;
72+
}
73+
74+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.seed.springbatch.sample.domain;
9+
10+
import javax.persistence.EntityManager;
11+
12+
import org.seedstack.business.domain.Repository;
13+
14+
public interface UserDao extends Repository<User, Long>{
15+
public EntityManager getEntityManager();
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.seed.springbatch.sample.domain;
9+
10+
import javax.persistence.EntityManager;
11+
12+
import org.seedstack.jpa.BaseJpaRepository;
13+
14+
public class UserDaoImpl extends BaseJpaRepository<User, Long> implements UserDao {
15+
16+
@Override
17+
public EntityManager getEntityManager() {
18+
return entityManager;
19+
}
20+
21+
22+
23+
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.seed.springbatch.sample.listeners;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.batch.core.BatchStatus;
13+
import org.springframework.batch.core.JobExecution;
14+
import org.springframework.batch.core.JobExecutionListener;
15+
import org.springframework.stereotype.Component;
16+
17+
@Component("jobExecutionListener")
18+
public class AppJobExecutionListener implements JobExecutionListener {
19+
20+
private static final Logger logger = LoggerFactory.getLogger(AppJobExecutionListener.class);
21+
22+
public void afterJob(JobExecution jobExecution) {
23+
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
24+
logger.debug("Job completed with JobId {} ", jobExecution.getJobId());
25+
} else if (jobExecution.getStatus() == BatchStatus.FAILED) {
26+
logger.debug("Job failed with JobId {} ", jobExecution.getJobId());
27+
}
28+
}
29+
30+
public void beforeJob(JobExecution jobExecution) {
31+
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
32+
logger.debug("Job completed with JobId {} ", jobExecution.getJobId());
33+
} else if (jobExecution.getStatus() == BatchStatus.FAILED) {
34+
logger.debug("Job failed with JobId {} ", jobExecution.getJobId());
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)