3636
3737/**
3838 * @author Mahmoud Ben Hassine
39+ * @author Myeongha Shin
3940 * @since 5.2.0
4041 */
4142public class MongoJobInstanceDao implements JobInstanceDao {
4243
43- private static final String COLLECTION_NAME = "BATCH_JOB_INSTANCE " ;
44+ private static final String COLLECTION_NAME = "JOB_INSTANCE " ;
4445
45- private static final String SEQUENCE_NAME = "BATCH_JOB_INSTANCE_SEQ " ;
46+ private static final String SEQUENCE_NAME = "JOB_INSTANCE_SEQ " ;
4647
4748 private final MongoOperations mongoOperations ;
4849
50+ private final String collectionName ;
51+
4952 private DataFieldMaxValueIncrementer jobInstanceIncrementer ;
5053
5154 private JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator ();
5255
5356 private final JobInstanceConverter jobInstanceConverter = new JobInstanceConverter ();
5457
55- public MongoJobInstanceDao (MongoOperations mongoOperations ) {
58+ public MongoJobInstanceDao (MongoOperations mongoOperations , String collectionPrefix ) {
5659 Assert .notNull (mongoOperations , "mongoOperations must not be null." );
60+ Assert .notNull (collectionPrefix , "collectionPrefix must not be null." );
5761 this .mongoOperations = mongoOperations ;
58- this .jobInstanceIncrementer = new MongoSequenceIncrementer (mongoOperations , SEQUENCE_NAME );
62+ this .collectionName = collectionPrefix + COLLECTION_NAME ;
63+ this .jobInstanceIncrementer = new MongoSequenceIncrementer (mongoOperations , collectionPrefix + SEQUENCE_NAME );
5964 }
6065
6166 public void setJobKeyGenerator (JobKeyGenerator jobKeyGenerator ) {
@@ -79,7 +84,7 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
7984 jobInstanceToSave .setJobKey (key );
8085 long instanceId = jobInstanceIncrementer .nextLongValue ();
8186 jobInstanceToSave .setJobInstanceId (instanceId );
82- this .mongoOperations .insert (jobInstanceToSave , COLLECTION_NAME );
87+ this .mongoOperations .insert (jobInstanceToSave , this . collectionName );
8388
8489 JobInstance jobInstance = new JobInstance (instanceId , jobName );
8590 jobInstance .incrementVersion (); // TODO is this needed?
@@ -90,16 +95,16 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
9095 public JobInstance getJobInstance (String jobName , JobParameters jobParameters ) {
9196 String key = this .jobKeyGenerator .generateKey (jobParameters );
9297 Query query = query (where ("jobName" ).is (jobName ).and ("jobKey" ).is (key ));
93- org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations
94- . findOne ( query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME );
98+ org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations . findOne (
99+ query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName );
95100 return jobInstance != null ? this .jobInstanceConverter .toJobInstance (jobInstance ) : null ;
96101 }
97102
98103 @ Override
99104 public JobInstance getJobInstance (long instanceId ) {
100105 Query query = query (where ("jobInstanceId" ).is (instanceId ));
101- org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations
102- . findOne ( query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME );
106+ org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations . findOne (
107+ query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName );
103108 return jobInstance != null ? this .jobInstanceConverter .toJobInstance (jobInstance ) : null ;
104109 }
105110
@@ -114,7 +119,7 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
114119 Sort .Order sortOrder = Sort .Order .desc ("jobInstanceId" );
115120 List <org .springframework .batch .core .repository .persistence .JobInstance > jobInstances = this .mongoOperations
116121 .find (query .with (Sort .by (sortOrder )),
117- org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
122+ org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
118123 .stream ()
119124 .toList ();
120125 return jobInstances .subList (start , jobInstances .size ())
@@ -134,7 +139,7 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
134139 public List <JobInstance > getJobInstances (String jobName ) {
135140 Query query = query (where ("jobName" ).is (jobName ));
136141 return this .mongoOperations
137- .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
142+ .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
138143 .stream ()
139144 .map (this .jobInstanceConverter ::toJobInstance )
140145 .toList ();
@@ -144,7 +149,7 @@ public List<JobInstance> getJobInstances(String jobName) {
144149 public List <Long > getJobInstanceIds (String jobName ) {
145150 Query query = query (where ("jobName" ).is (jobName ));
146151 return this .mongoOperations
147- .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
152+ .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
148153 .stream ()
149154 .map (org .springframework .batch .core .repository .persistence .JobInstance ::getJobInstanceId )
150155 .toList ();
@@ -153,7 +158,7 @@ public List<Long> getJobInstanceIds(String jobName) {
153158 public List <JobInstance > findJobInstancesByName (String jobName ) {
154159 Query query = query (where ("jobName" ).is (jobName ));
155160 return this .mongoOperations
156- .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
161+ .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
157162 .stream ()
158163 .map (this .jobInstanceConverter ::toJobInstance )
159164 .toList ();
@@ -165,14 +170,14 @@ public JobInstance getLastJobInstance(String jobName) {
165170 Sort .Order sortOrder = Sort .Order .desc ("jobInstanceId" );
166171 org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations .findOne (
167172 query .with (Sort .by (sortOrder )), org .springframework .batch .core .repository .persistence .JobInstance .class ,
168- COLLECTION_NAME );
173+ this . collectionName );
169174 return jobInstance != null ? this .jobInstanceConverter .toJobInstance (jobInstance ) : null ;
170175 }
171176
172177 @ Override
173178 public List <String > getJobNames () {
174179 return this .mongoOperations
175- .findAll (org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
180+ .findAll (org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
176181 .stream ()
177182 .map (org .springframework .batch .core .repository .persistence .JobInstance ::getJobName )
178183 .toList ();
@@ -195,7 +200,7 @@ public long getJobInstanceCount(String jobName) throws NoSuchJobException {
195200 throw new NoSuchJobException ("Job not found " + jobName );
196201 }
197202 Query query = query (where ("jobName" ).is (jobName ));
198- return this .mongoOperations .count (query , COLLECTION_NAME );
203+ return this .mongoOperations .count (query , this . collectionName );
199204 }
200205
201206}
0 commit comments