简体   繁体   English

我无法在Spring的后台运行该方法

[英]I can not run the method in the background in Spring

I have a service DocumentServiceImpl. 我有一个服务DocumentServiceImpl。

In it I want to run a method index() of class GlobalSearch in background. 在其中,我想在后台运行class GlobalSearch的方法index()

@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. 使用来自类ExtractTextFromFile的方法的类GlobalSearch的方法index I inject class ExtractTextFromFile using constructor and Lombok annotation @RequiredArgsConstructor 我使用构造函数和Lombok注释@RequiredArgsConstructor注入类ExtractTextFromFile

@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; ..但IDE给出了Variable 'extractTextFromFile' might not have been initializepublic final ExtractTextFromFile extractTextFromFile; Variable 'extractTextFromFile' might not have been initialize 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: 此类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和ExtractTextFromFile类的注解@Component,然后将它们初始化为简单的句点

 GlobalSearch globalSearch = new GlobalSearch() 

...and ...和

 ExtractTextFromFile extractTextFromFile = new ExtractTextFromFile()

Why don't you try @asyc over the method and @EnableAsyc 你为什么不尝试@asyc方法和@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 通过配置使用@EnableAsync

@SpringBootApplication
@EnableAsync
public class SpringbootdemoApplication {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何与spring异步运行方法? - How can I run a method Asynchronously with spring? 我如何在后台线程中运行 onNavigationItemSelected 方法 - how i can run the onNavigationItemSelected method in background thread Spring单例初始化完成后如何运行方法? - How can I run a method after the Spring singleton initialization is complete? 如何在 Linux (Centos 7) shell 上在后台正确运行 Spring Boot Batch 应用程序? - How can I correctly run in background a Spring Boot Batch application on a Linux (Centos 7) shell? 为什么我可以为Spring项目运行JUnit测试,但不能运行main方法呢? - Why can I run JUnit tests for my Spring project, but not a main method? 如何使用Swing运行后台任务? - How can I run background tasks with Swing? 我可以在ThreadPool中运行后台任务吗? - Can I run background tasks in a ThreadPool? 您可以使用在后台运行in的方法中的发布吗? - Can you use publish from a method called in run in background? 我无法运行Spring和MyBatis应用程序 - I can not run the Spring and MyBatis application 我可以配置 Spring MVC 应用程序,以便在我运行应用程序时,控制器或任何方法都应该被自动调用 - Can i configure the Spring MVC application so that when ever i run the application, the controller or any method should be called automatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM