How to create a Thread in Spring Boot?

Asked

Viewed 258 times

0

I am starting a file upload file application, where saves the path in the database and the image on the server, when performing a test can send the image to the folder, now I need to be able to create the temporary location when submitting the form with other properties the image can be submitted at the same time, but for that to happen is necessary this temporary location with Thread.

Get the simple upload template through of that repository , this application is only sending the file to folder, but at first only need it at the moment.

My application currently looks like this;

@RestController
@RequestMapping("/files")
public class FotosResource {

//  @PostMapping
//  public void upload(@RequestParam MultipartFile files) {
//      disco.salvarFoto(files);
//  }   

    @PostMapping
    public DeferredResult<String> upload(@RequestParam MultipartFile files) {
        DeferredResult<String> resultado = new DeferredResult<>();

        Thread thread = new Thread(new FotoStorageRunnable(files, resultado));
        thread.start();



        return resultado;
    }


}

And I tried to create this Thread;

public class FotoStorageRunnable implements Runnable {

    @Autowired
    private Disco disco;

    private MultipartFile files;
    private DeferredResult<String> resultado;

    public FotoStorageRunnable(MultipartFile files, DeferredResult<String> resultado) {
        this.files = files;
        this.resultado = resultado;
    }

    @Override
    public void run() {
        disco.salvarFoto(files);
        resultado.setResult("OK! Foto recebida!");

    }

}

While running the application in STS generated this error in the console, I am using Spring Boot;

Exception in thread "Thread-9" java.lang.NullPointerException
    at com.algaworks.contato.storage.FotoStorageRunnable.run(FotoStorageRunnable.java:22)
    at java.lang.Thread.run(Unknown Source)

Is indicating that the error is in this line of code;

disk.saveFoto(files);

How do I get the bug fixed?

The expected behavior would be that it works the same way as it was behaving before the change, being able to send the selected file to folder.

=============UPDATING=========

@Component
public class Disco {

    @Value("${contato.disco.raiz}")
    private String raiz;

    @Value("${contato.disco.diretorio-fotos}")
    private String diretorioFotos;

    public void salvarFoto(MultipartFile files) {
        this.salvar(this.diretorioFotos, files);
    }

    public void salvar(String diretorio, MultipartFile arquivo) {
        Path diretorioPath = Paths.get(this.raiz, diretorio);
        Path arquivoPath = diretorioPath.resolve(arquivo.getOriginalFilename());

        try {
            Files.createDirectories(diretorioPath);
            arquivo.transferTo(arquivoPath.toFile());           
        } catch (IOException e) {
            throw new RuntimeException("Problemas na tentativa de salvar arquivo.", e);
        }       
    }
}
  • usually the class needs to be with the prototype annotation ( the default is single ) so in theory there is no way to run a single thing in parallel

  • I am sorry to say that you are totally mistaken, take a look at this repository https://github.com/algaworks/curso-sistemas-web-com-spring-javascript-bootstrap/blob/master/14.4-bestlienabilityda-application-asynchronous/Brewer/src/main/java/com/algaworks/Brewer/Storage/Fotostoragerunnable.java &#Xada-application-asynchronous-return/Brewer/src/main/java/com/algaworks/Brewer/controller/Fotoscontroller.java

  • And this project works perfectly, there’s only one difference, this project I showed you is in Spring Frameworks and my project is done in Spring Boot.

  • " you are totally mistaken" : I am not not, but the possible reason for your nullpointer is that you have not written down your Disk bean and because of this your dependency does not inject

  • Please Lucas Miranda, how could I put the note in the Disco class?

  • you could annotate the class with for example "@Component" ex: "@Component public class Disco{ }"

  • Lucas Miranda, she was already with this note. I will update the post for you see, I would have some other suggestion?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.