2

I don't know if it's possible, but this is my question: I hava a batch developed using spring-boot and spring-batch, and I have to call another microservice using Feign... ...help!

this is my class Reader

package it.batch.step;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import it.client.feign.EmailAccountClient;
import it.dto.feign.mail.account.AccountOutDto;
import it.dto.feign.mail.account.SearchAccountFilterDto;
import it.dto.feign.mail.account.SearchAccountResponseDto;

public class Reader implements ItemReader <String> {

    private static final Logger LOGGER = LoggerFactory.getLogger(Reader.class);

    private int count = 0;

    @Autowired
    private EmailAccountClient emailAccountClient;

    @Override
    public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

        LOGGER.info("read - begin ");
        SearchAccountResponseDto clientResponse = emailAccountClient.searchAccount(getFilter());
        if (count < clientResponse.getAccounts().size()) {
            return convertToJsonString(clientResponse.getAccounts().get(count++));
        } else {
            count = 0;
        }
        return null;
    }

    private static SearchAccountFilterDto getFilter() {
        SearchAccountFilterDto filter = new SearchAccountFilterDto();

        return filter;
    }

    private String convertToJsonString(AccountOutDto account) {
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = "";
        try {
            jsonString = mapper.writeValueAsString(account);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        LOGGER.info("Contenuto JSON: " + jsonString);
        return jsonString;
    }
}

... when I launch the batch I have this error:

java.lang.NullPointerException: null
    at it.batch.step.Reader.read(Reader.java:32) ~[classes/:?]

where line 32 is:

SearchAccountResponseDto clientResponse = emailAccountClient.searchAccount(getFilter());

EmailAccountClient is null

  • Please add more details like, what is happening when you running the job ? – prasad Apr 10 '20 at 19:55
  • From the writer and processor you can make a Feign call, but it depends on use case. for me having a Custom Writer and there you can make Feign call. Kindly share more details – PAA Apr 12 '20 at 11:14

1 Answers1

1

Your client is null: your reader is not a @Component so Spring can't autowire the client. You must use a workaround like passing the autowired client through the constructor when you instantiate the reader, like this:

 private EmailAccountClient client;

 public reader(EmailAccountClient client){

  this.client=client;
  }

in the other class:

  @Autowired
  private EmailAccountClient client;

  @Bean
  public ItemReader<String> reader(){

      return new Reader(client)

  }
Bored
  • 499
  • 4
  • 11