Spring Batch Partition Handler错误

时间:2018-04-08 20:36:31

标签: java spring spring-boot spring-integration spring-batch

我对Spring framework& amp;春季批次

我正在尝试设置示例弹出批处理远程分区示例。

我使用此堆栈Spring Boot + Spring Batch + Spring Integration + AWS SQS

接下来,我已经成功完成了。

1.创建所有配置,包括频道,作业,队列和其他内容。

2.Ran主进程,我能够对表进行分区,并将分区元数据推送到AWS SQS。

但是在运行从属进程时我得到了错误,在从属进程中,我能够从队列中提取消息,但在StepExecutionRequestHandler的handle()方法中获取错误

  

org.springframework.messaging.MessageHandlingException:嵌套   例外是   org.springframework.expression.spel.SpelEvaluationException:EL1004E:   方法调用:无法找到方法句柄(java.lang.String)   org.springframework.batch.integration.partition.StepExecutionRequestHandler   type,failedMessage = GenericMessage [payload = StepExecutionRequest:   [jobExecutionId = 2,stepExecutionId = 3,stepName = slaveStep],   headers = {sequenceNumber = 2,aws_messageId ="",   SentTimestamp = 1523215624042,sequenceSize = 4,SenderId ="",   aws_receiptHandle ="",ApproximateReceiveCount = 2,   correlationId = 2:slaveStep,id ="",lookupDestination = master,   aws_queue = master,ApproximateFirstReceiveTimestamp = 1523215634470,   时间戳= 1523215864910}]

@Configuration
public class JobConfiguration implements ApplicationContextAware
{

  @Autowired
  public JobBuilderFactory jobBuilderFactory;

  @Autowired
  public StepBuilderFactory stepBuilderFactory;

  @Autowired
  public DataSource dataSource;

  @Autowired
  public JobExplorer jobExplorer;

  @Autowired
  public JobRepository jobRepository;

  private ApplicationContext applicationContext;

  private static final int GRID_SIZE = 4;

  @Bean
  public PartitionHandler partitionHandler(MessagingTemplate messagingTemplate) throws Exception
  {
    MessageChannelPartitionHandler partitionHandler = new MessageChannelPartitionHandler();
    partitionHandler.setStepName("slaveStep");
    partitionHandler.setGridSize(GRID_SIZE);
    partitionHandler.setMessagingOperations(messagingTemplate);
    partitionHandler.setPollInterval(5000l);
    partitionHandler.setJobExplorer(this.jobExplorer);

    partitionHandler.afterPropertiesSet();

    return partitionHandler;
  }

  @Bean
  public ColumnRangePartitioner partitioner()
  {
    ColumnRangePartitioner columnRangePartitioner = new ColumnRangePartitioner();

    columnRangePartitioner.setColumn("id");
    columnRangePartitioner.setDataSource(this.dataSource);
    columnRangePartitioner.setTable("customer");
    return columnRangePartitioner;
  }

  @Bean
  @Profile("slave")
  @ServiceActivator(inputChannel = "inboundRequests", outputChannel = "outboundStaging")
  public StepExecutionRequestHandler stepExecutionRequestHandler()
  {
    StepExecutionRequestHandler stepExecutionRequestHandler = new StepExecutionRequestHandler();

    BeanFactoryStepLocator stepLocator = new BeanFactoryStepLocator();
    stepLocator.setBeanFactory(this.applicationContext);
    stepExecutionRequestHandler.setStepLocator(stepLocator);
    stepExecutionRequestHandler.setJobExplorer(this.jobExplorer);

    return stepExecutionRequestHandler;
  }

  @Bean(name = PollerMetadata.DEFAULT_POLLER)
  public PollerMetadata defaultPoller()
  {
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(10));
    return pollerMetadata;
  }

  @Bean
  @StepScope
  public JdbcPagingItemReader<Customer> pagingItemReader(@Value("#{stepExecutionContext['minValue']}") Long minValue,
      @Value("#{stepExecutionContext['maxValue']}") Long maxValue)
  {
    System.out.println("reading " + minValue + " to " + maxValue);
    JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();

    reader.setDataSource(this.dataSource);
    reader.setFetchSize(100);
    reader.setRowMapper(new CustomerRowMapper());

    MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
    queryProvider.setSelectClause("id, firstName, lastName, birthdate");
    queryProvider.setFromClause("from customer");
    queryProvider.setWhereClause("where id >= " + minValue + " and id <= " + maxValue);

    Map<String, Order> sortKeys = new HashMap<>(1);

    sortKeys.put("id", Order.ASCENDING);

    queryProvider.setSortKeys(sortKeys);

    reader.setQueryProvider(queryProvider);

    return reader;
  }

  @Bean
  @StepScope
  public JdbcBatchItemWriter<Customer> customerItemWriter()
  {
    JdbcBatchItemWriter<Customer> itemWriter = new JdbcBatchItemWriter<>();

    itemWriter.setDataSource(this.dataSource);
    itemWriter.setSql("INSERT INTO new_customer VALUES (:id, :firstName, :lastName, :birthdate)");
    itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider());
    itemWriter.afterPropertiesSet();

    return itemWriter;
  }

  @Bean
  public Step step1() throws Exception
  {
    return stepBuilderFactory.get("step1").partitioner(slaveStep().getName(), partitioner()).step(slaveStep())
        .partitionHandler(partitionHandler(null)).build();
  }

  @Bean
  public Step slaveStep()
  {
    return stepBuilderFactory.get("slaveStep").<Customer, Customer>chunk(1000).reader(pagingItemReader(null, null)).writer(customerItemWriter())
        .build();
  }

  @Bean
  @Profile("master")
  public Job job() throws Exception
  {
    return jobBuilderFactory.get("job").start(step1()).build();
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
  {
    this.applicationContext = applicationContext;
  }
}

@Configuration
public class IntegrationConfiguration
{

  @Autowired
  private AmazonSQSAsync amazonSqs;

  @Bean
  public MessagingTemplate messageTemplate()
  {
    MessagingTemplate messagingTemplate = new MessagingTemplate(outboundRequests());

    messagingTemplate.setReceiveTimeout(60000000l);

    return messagingTemplate;
  }

  @Bean
  public DirectChannel outboundRequests()
  {
    return new DirectChannel();
  }


  @Bean
  @Profile("slave")
  public MessageProducer sqsMessageDrivenChannelAdapter()
  {
    SqsMessageDrivenChannelAdapter adapter = new SqsMessageDrivenChannelAdapter(this.amazonSqs, "master");
    adapter.setOutputChannel(inboundRequests());
    adapter.afterPropertiesSet();
    return adapter;
  }

  @Bean
  @ServiceActivator(inputChannel = "outboundRequests")
  public MessageHandler sqsMessageHandler()
  {
    SqsMessageHandler messageHandler = new SqsMessageHandler(amazonSqs);
    messageHandler.setQueue("master");
    return messageHandler;
  }

  @Bean
  public PollableChannel outboundStaging()
  {
    return new NullChannel();
  }

  @Bean
  public QueueChannel inboundRequests()
  {
    return new QueueChannel();
  }
}

由于

1 个答案:

答案 0 :(得分:0)

您应该记住,StepExecutionRequestHandler的合同如下:

public StepExecution handle(StepExecutionRequest request)

根据您的异常和SQS的性质,inboundRequests中消息的有效负载是一个字符串。我相信它是在JSON中。因此,请考虑在JsonToObjectTrabsformer之前使用StepExecutionRequestHandler

<强>更新

  

有效负载不是JSON格式。它是一个字符串,从toString()StepExecutionRequest创建。格式为StepExecutionRequest: [jobExecutionId=2, stepExecutionId=3,stepName=slaveStep]

OK!我明白你的意思了。 SQS Message只能有String个正文。向SQS发送消息的SqsMessageHandler默认使用GenericMessageConverter将传入对象转换为字符串。

我认为您需要考虑使用SqsMessageHandler配置MappingJackson2MessageConverter以将StepExecutionRequest真正序列化为正确的JSON并让它通过SQS传输。

在调用StepExecutionRequestHandler之前,在另一个(从属)方面,你真的应该在@Transformer之后SqsMessageDrivenChannelAdapter放置一个JsonToObjectTransformer