Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- public class JobConfiguration {
- ...
- ...
- ...
- @Bean
- public JdbcCursorItemReader<Notification> reader() {
- JdbcCursorItemReader<Notification> reader = new JdbcCursorItemReader<Notification>();
- reader.setDataSource(dataSource);
- ...
- ...
- reader.setRowMapper(new BeanPropertyRowMapper<>(Notification.class));
- return reader;
- }
- @Bean
- public NotificationItemProcessor notificatonProcessor() {
- return new NotificationItemProcessor();
- }
- @Bean
- public JdbcBatchItemWriter<Notification> updateWriter() {
- JdbcBatchItemWriter<Notification> writer = new JdbcBatchItemWriter<Notification>();
- writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Notification>());
- ...
- writer.setDataSource(dataSource);
- return writer;
- }
- /**
- * Composite Exchange Writer
- * @return
- * @throws InstantiationException
- * @throws IllegalAccessException
- */
- @SuppressWarnings("unchecked")
- @Bean
- public CompositeItemWriter<Notification> compositeExchangeWriter() throws InstantiationException, IllegalAccessException {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put(ExchangeRouter.INSERT_EXCHANGE_FOR_NOTIFICATION.getActionName(), exchangeWorkflowWriter());
- map.put(ExchangeRouter.INSERT_EXCHANGE_FOR_PACK.getActionName(), exchangeWriter());
- map.put(ExchangeRouter.DO_NOTHING.getActionName(), doNothing());
- return new CompositeItemWriterBuilder(map, ExchangeWriterRouterClassifier.class).build();
- }
- @Bean
- public JdbcBatchItemWriter<Notification> exchangeWorkflowWriter() {
- JdbcBatchItemWriter<Notification> writer = new JdbcBatchItemWriter<Notification>();
- writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Notification>());
- writer.setSql(" INSERT INTO SOME TABLE..");
- writer.setDataSource(dataSource);
- return writer;
- }
- @Bean
- public JdbcBatchItemWriter<Notification> exchangeWriter() {
- JdbcBatchItemWriter<Notification> writer = new JdbcBatchItemWriter<Notification>();
- writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Notification>());
- writer.setSql("INSERT INTO SOME OTHER TABLE.");
- writer.setDataSource(dataSource);
- return writer;
- }
- @Bean
- public ItemWriter<Document> doNothing() {
- return new DummyWriter();
- }
- @Bean
- public Job generatePdf(JobCompletionNotificationListener listener) throws InstantiationException, IllegalAccessException {
- return jobBuilderFactory.get("generatePdf")
- .incrementer(new RunIdIncrementer())
- .flow(treatStock())
- .end()
- .build();
- }
- @Bean
- public Step treatStock() throws InstantiationException, IllegalAccessException {
- return stepBuilderFactory.get("treatStock")
- .<Notification, Notification>chunk(1)
- .reader(reader())
- .processor(notificatonProcessor())
- .writer(compositeExchangeWriter())
- .writer(updateWriter())
- .build();
- }
- }
- public class CompositeItemWriterBuilder extends CompositeItemBuilder<CompositeItemWriter> {
- public CompositeItemWriterBuilder(HashMap<String, Object> matcherMap, Class<?> routerDelegate) throws InstantiationException, IllegalAccessException {
- BackToBackPatternClassifier classif = new BackToBackPatternClassifier();
- classif.setRouterDelegate(routerDelegate.newInstance());
- classif.setMatcherMap(matcherMap);
- ClassifierCompositeItemWriter classifier = new ClassifierCompositeItemWriter();
- classifier.setClassifier(classif);
- this.delegates.add(classifier);
- }
- public CompositeItemWriterBuilder(List<Object> delegates) {
- this.delegates = delegates;
- }
- @Override
- protected Class<?> getCompositeItem() {
- return CompositeItemWriter.class;
- }
- }
- public abstract class CompositeItemBuilder<T> {
- protected List<Object> delegates = new ArrayList<Object>();
- @SuppressWarnings("unchecked")
- public T build() throws InstantiationException, IllegalAccessException {
- Object compositeItem = getCompositeItem().newInstance();
- Method setDelegates = ReflectionUtils.findMethod(compositeItem.getClass(), "setDelegates", List.class);
- ReflectionUtils.invokeMethod(setDelegates,compositeItem, delegates);
- return (T) compositeItem;
- }
- abstract protected Class<?> getCompositeItem();
- }
- public class ExchangeWriterRouterClassifier {
- @Classifier
- public String classify(Notification notification) {
- return notification.getExchangesWorkflow().getRouter().getActionName();
- }
- }
Add Comment
Please, Sign In to add comment