CommandlineJobRunner 을 이용해서 동작하는 클래스가 여러개의 쓰레드에서 동작했을 때 문제 없는지 확인하는 TestCase이다.
ExecutorService 를 이용해서 개발된 코드이다. 동시에 몇개의 쓰레드를 동시에 시작시키고 이런 iteration을 몇개나 할 수 있는지, 예상해서 개발이 가능하다.
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sample.batch.example;
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sample.batch.example;
import static org.junit.Assert.assertNotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dave Syer
*
*/
@ContextConfiguration(locations = { "/launch-context.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class ExampleJobConfigurationTests {
* @author Dave Syer
*
*/
@ContextConfiguration(locations = { "/launch-context.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class ExampleJobConfigurationTests {
@Autowired
private JobLauncher jobLauncher;
private JobLauncher jobLauncher;
@Autowired
private Job job;
private Job job;
private int seq = new Random().nextInt();
@Test
public void testSimpleProperties() throws Exception {
assertNotNull(jobLauncher);
}
public void testSimpleProperties() throws Exception {
assertNotNull(jobLauncher);
}
@Test
public void testLaunchJob() throws Exception {
ExecutorService service = Executors.newFixedThreadPool(5);
for (int i = 1; i < 100; i++) {
service.execute(new SimpleJob());
}
service.shutdown();
try {
while (!service.awaitTermination(2000, TimeUnit.MILLISECONDS)) {
}
} catch (InterruptedException e) {
e.printStackTrace();
}
public void testLaunchJob() throws Exception {
ExecutorService service = Executors.newFixedThreadPool(5);
for (int i = 1; i < 100; i++) {
service.execute(new SimpleJob());
}
service.shutdown();
try {
while (!service.awaitTermination(2000, TimeUnit.MILLISECONDS)) {
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String getSeq() {
return String.valueOf(seq++);
}
return String.valueOf(seq++);
}
class SimpleJob implements Runnable {
@Override
public void run() {
try {
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("job_mssql", new JobParameter("job_mssql"));
System.out.println("## seq: " + getSeq());
parameters.put("seq", new JobParameter(getSeq()));
JobParameters jobParameters = new JobParameters(parameters);
jobLauncher.run(job, jobParameters);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("job_mssql", new JobParameter("job_mssql"));
System.out.println("## seq: " + getSeq());
parameters.put("seq", new JobParameter(getSeq()));
JobParameters jobParameters = new JobParameters(parameters);
jobLauncher.run(job, jobParameters);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
'general java' 카테고리의 다른 글
Spring Batch - MSSQL 채번 (0) | 2011.04.07 |
---|---|
Spring Batch retry 예제들 (0) | 2011.04.06 |
[Spring Batch] Retry code (0) | 2011.03.17 |
[Spring Batch] retry-policy가 존재하면, 반드시 retry-limit이 존재해야 한다. (0) | 2011.03.17 |
[Spring Batch] Oracle DB-Job실행중 ORA-08177 발생 (0) | 2011.02.25 |