Skip to content

Instantly share code, notes, and snippets.

@dsyer
Created June 13, 2012 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dsyer/2924318 to your computer and use it in GitHub Desktop.
Save dsyer/2924318 to your computer and use it in GitHub Desktop.
Spring Batch @configuration samples using step scope
*~
#*
.#*
.gradle
*.sw?
*.class
.classpath
.project
.settings
bin
build
target
*.class
@GrabConfig(systemClassLoader=true)
@Grab("org.springframework:spring-jdbc:4.0.0.BOOTSTRAP-SNAPSHOT")
@Grab("org.hsqldb:hsqldb-j5:2.0.0")
@Controller
class App {
@Value('${value:World}')
private String value
@Autowired
private DataSource dataSource
@RequestMapping("/")
@ResponseBody
String home() {
println ("********: ${value}, ${dataSource}")
return "Hello ${value}!"
}
}
@Configuration
class HadoopConfig implements CommandlineRunner {
@Autowired
private HadoopConfiguration configuration
@Bean
JobRunner runner() {
JobRunner runner = new JobRunner()
runner.jobNames = ["wordcountJob"]
runner
}
@Bean
JobFactoryBean wordcountJob() {
JobFactoryBean factory = new JobFactoryBean()
factory.mapper = "org.apache.hadoop.examples.WordCount.TokenizerMapper"
factory.reducer = "org.apache.hadoop.examples.WordCount.IntSumReducer"
factory.outputPath = "/user/gutenberg/output"
factory.inputPath = ["/user/gutenberg/input"]
factory.configuration = configuration
factory
}
@Override
void run(String... args) {
println "I'm running!"
runner().startJobs()
}
}
<!doctype html>
<html>
<body>
hello, world
</body>
</html>
@Grab("org.hsqldb:hsqldb-j5:2.0.0")
@EnableBatchProcessing
class JobConfig {
@Autowired
private JobBuilderFactory jobs
@Autowired
private StepBuilderFactory steps
@Bean
protected Tasklet tasklet() {
return new Tasklet() {
RepeatStatus execute(StepContribution contribution, ChunkContext context) {
return RepeatStatus.FINISHED
}
}
}
@Bean
Job job() throws Exception {
return jobs.get("job").start(step1()).build()
}
@Bean
protected Step step1() throws Exception {
return steps.get("step1").tasklet(tasklet()).build()
}
}
@Controller
class Min {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!"
}
}
@Configuration
@EnableWebMvc
class WebConfig extends WebMvcConfigurerAdapter {
@Override
void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/public/")
}
@Override
void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("redirect:index.html")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment