3838/**
3939 * @author Mahmoud Ben Hassine
4040 * @author Yanming Zhou
41+ * @author Myeongha Shin
4142 * @since 5.2.0
4243 */
4344public class MongoJobInstanceDao implements JobInstanceDao {
4445
45- private static final String COLLECTION_NAME = "BATCH_JOB_INSTANCE " ;
46+ private static final String COLLECTION_NAME = "JOB_INSTANCE " ;
4647
47- private static final String SEQUENCE_NAME = "BATCH_JOB_INSTANCE_SEQ " ;
48+ private static final String SEQUENCE_NAME = "JOB_INSTANCE_SEQ " ;
4849
4950 private final MongoOperations mongoOperations ;
5051
52+ private final String collectionName ;
53+
5154 private DataFieldMaxValueIncrementer jobInstanceIncrementer ;
5255
5356 private JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator ();
5457
5558 private final JobInstanceConverter jobInstanceConverter = new JobInstanceConverter ();
5659
57- public MongoJobInstanceDao (MongoOperations mongoOperations ) {
60+ public MongoJobInstanceDao (MongoOperations mongoOperations , String collectionPrefix ) {
5861 Assert .notNull (mongoOperations , "mongoOperations must not be null." );
62+ Assert .notNull (collectionPrefix , "collectionPrefix must not be null." );
5963 this .mongoOperations = mongoOperations ;
60- this .jobInstanceIncrementer = new MongoSequenceIncrementer (mongoOperations , SEQUENCE_NAME );
64+ this .collectionName = collectionPrefix + COLLECTION_NAME ;
65+ this .jobInstanceIncrementer = new MongoSequenceIncrementer (mongoOperations , collectionPrefix + SEQUENCE_NAME );
6166 }
6267
6368 public void setJobKeyGenerator (JobKeyGenerator jobKeyGenerator ) {
@@ -81,7 +86,7 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
8186 jobInstanceToSave .setJobKey (key );
8287 long instanceId = jobInstanceIncrementer .nextLongValue ();
8388 jobInstanceToSave .setJobInstanceId (instanceId );
84- this .mongoOperations .insert (jobInstanceToSave , COLLECTION_NAME );
89+ this .mongoOperations .insert (jobInstanceToSave , this . collectionName );
8590
8691 JobInstance jobInstance = new JobInstance (instanceId , jobName );
8792 jobInstance .incrementVersion (); // TODO is this needed?
@@ -92,16 +97,16 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
9297 public JobInstance getJobInstance (String jobName , JobParameters jobParameters ) {
9398 String key = this .jobKeyGenerator .generateKey (jobParameters );
9499 Query query = query (where ("jobName" ).is (jobName ).and ("jobKey" ).is (key ));
95- org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations
96- . findOne ( query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME );
100+ org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations . findOne (
101+ query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName );
97102 return jobInstance != null ? this .jobInstanceConverter .toJobInstance (jobInstance ) : null ;
98103 }
99104
100105 @ Override
101106 public JobInstance getJobInstance (long instanceId ) {
102107 Query query = query (where ("jobInstanceId" ).is (instanceId ));
103- org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations
104- . findOne ( query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME );
108+ org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations . findOne (
109+ query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName );
105110 return jobInstance != null ? this .jobInstanceConverter .toJobInstance (jobInstance ) : null ;
106111 }
107112
@@ -116,7 +121,7 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
116121 Sort .Order sortOrder = Sort .Order .desc ("jobInstanceId" );
117122 List <org .springframework .batch .core .repository .persistence .JobInstance > jobInstances = this .mongoOperations
118123 .find (query .with (Sort .by (sortOrder )),
119- org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
124+ org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
120125 .stream ()
121126 .toList ();
122127 if (jobInstances .size () <= start ) {
@@ -139,7 +144,7 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
139144 public List <JobInstance > getJobInstances (String jobName ) {
140145 Query query = query (where ("jobName" ).is (jobName ));
141146 return this .mongoOperations
142- .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
147+ .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
143148 .stream ()
144149 .map (this .jobInstanceConverter ::toJobInstance )
145150 .toList ();
@@ -149,7 +154,7 @@ public List<JobInstance> getJobInstances(String jobName) {
149154 public List <Long > getJobInstanceIds (String jobName ) {
150155 Query query = query (where ("jobName" ).is (jobName ));
151156 return this .mongoOperations
152- .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
157+ .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
153158 .stream ()
154159 .map (org .springframework .batch .core .repository .persistence .JobInstance ::getJobInstanceId )
155160 .toList ();
@@ -158,7 +163,7 @@ public List<Long> getJobInstanceIds(String jobName) {
158163 public List <JobInstance > findJobInstancesByName (String jobName ) {
159164 Query query = query (where ("jobName" ).is (jobName ));
160165 return this .mongoOperations
161- .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
166+ .find (query , org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
162167 .stream ()
163168 .map (this .jobInstanceConverter ::toJobInstance )
164169 .toList ();
@@ -170,14 +175,14 @@ public JobInstance getLastJobInstance(String jobName) {
170175 Sort .Order sortOrder = Sort .Order .desc ("jobInstanceId" );
171176 org .springframework .batch .core .repository .persistence .JobInstance jobInstance = this .mongoOperations .findOne (
172177 query .with (Sort .by (sortOrder )), org .springframework .batch .core .repository .persistence .JobInstance .class ,
173- COLLECTION_NAME );
178+ this . collectionName );
174179 return jobInstance != null ? this .jobInstanceConverter .toJobInstance (jobInstance ) : null ;
175180 }
176181
177182 @ Override
178183 public List <String > getJobNames () {
179184 return this .mongoOperations
180- .findAll (org .springframework .batch .core .repository .persistence .JobInstance .class , COLLECTION_NAME )
185+ .findAll (org .springframework .batch .core .repository .persistence .JobInstance .class , this . collectionName )
181186 .stream ()
182187 .map (org .springframework .batch .core .repository .persistence .JobInstance ::getJobName )
183188 .toList ();
@@ -200,12 +205,12 @@ public long getJobInstanceCount(String jobName) throws NoSuchJobException {
200205 throw new NoSuchJobException ("Job not found " + jobName );
201206 }
202207 Query query = query (where ("jobName" ).is (jobName ));
203- return this .mongoOperations .count (query , COLLECTION_NAME );
208+ return this .mongoOperations .count (query , this . collectionName );
204209 }
205210
206211 @ Override
207212 public void deleteJobInstance (JobInstance jobInstance ) {
208- this .mongoOperations .remove (query (where ("jobInstanceId" ).is (jobInstance .getId ())), COLLECTION_NAME );
213+ this .mongoOperations .remove (query (where ("jobInstanceId" ).is (jobInstance .getId ())), this . collectionName );
209214 }
210215
211216}
0 commit comments