简体   繁体   中英

I can not run the method in the background in Spring

I have a service DocumentServiceImpl.

In it I want to run a method index() of class GlobalSearch in background.

@Service
@RequiredArgsConstructor
 public class DocumentServiceImpl implements DocumentService { 

... any code

@Transactional
 public void save(){

...

  Thread indexTread = new Thread(new GlobalSearch(file, id), "GlobalSearch");
  indexTread.start();

....

 }

}

Method index of class GlobalSearch using method from class ExtractTextFromFile. I inject class ExtractTextFromFile using constructor and Lombok annotation @RequiredArgsConstructor

@Component
@RequiredArgsConstructor
 public class GlobalSearch implements Runnable{
 public final ExtractTextFromFile extractTextFromFile; (41 lines)

 public File file;
 public Long id;
 public GlobalSearch(File File, Long id){
    this.file = file;
    this.id = id;
}

public void index(File file, Long id) {
    File textFile = extractTextFromFile.toText(file, id);

 ... code of this method

}

@Override
public void run() {
    index(file, id);
}

...other methods
public void search(){...}
public String delete(){...}

}

.. but IDE gives Variable 'extractTextFromFile' might not have been initialize for public final ExtractTextFromFile extractTextFromFile;

If i inject using:

@Autowired
ExtractTextFromFile extractTextFromFile;

Then it gives error:

Exception in thread "GlobalSearch" java.lang.NullPointerException
at bps.module.zxc.component.GlobalSearch.index(GlobalSearch.java:41)
at bps.module.zxc.component.GlobalSearch.run(GlobalSearch.java:136)
at java.lang.Thread.run(Thread.java:748)

This class ExtractTextFromFile:

@Component
@RequiredArgsConstructor
 public class ExtractTextFromFile {
 public File toText(File file, Long id) {
  extractFromPfd();
  extractFromWord();
 }
  extractFromPfd(){...};
  extractFromWord(){...};
 }

I solved the problem, i removed annotation @Component of classes GlobalSearch and ExtractTextFromFile then i initialised them as simple clases with

 GlobalSearch globalSearch = new GlobalSearch() 

...and

 ExtractTextFromFile extractTextFromFile = new ExtractTextFromFile()

Why don't you try @asyc over the method and @EnableAsyc

@Async
     public void looper()
     {
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(int i=0;i<20000;i++)
        {
            System.out.println("hitting "+i);
        }
     }

Use @EnableAsync over configurations

@SpringBootApplication
@EnableAsync
public class SpringbootdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM