dimanche 12 avril 2015

@Autowired in ThreadPoolTaskExecutor doesn't work

I trying to run async thread on button press in Java GUI application.


My code is:


Config class:



@Configuration
@ComponentScan({"khartn", "khartn.torrentsuploader.processor"})
public class AppConfig {

@Bean(initMethod = "init")
public NewJFrame mainForm() {
System.out.println("init mainForm");
return new NewJFrame();
}

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setCorePoolSize(5);
pool.setMaxPoolSize(10);
pool.setWaitForTasksToCompleteOnShutdown(true);
pool.initialize();
return pool;
}

}


JFrame class:



public class NewJFrame extends javax.swing.JFrame {

@Autowired
FileProcessor fileProcessor;
@Autowired
ThreadPoolTaskExecutor taskExecutor;

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
taskExecutor.execute(new Runnable() {

@Autowired
MyDirectoryReader myDirectoryReader;

@Autowired
AuthThread authThread;

@Override
public void run() {
jLabel3.setText("Авторизация...");
Boolean authSuccessfull = false;
while (!authSuccessfull) {
authSuccessfull = authThread.auth();

}
jLabel3.setText("Загрузка файлов");

myDirectoryReader.readDir();
}

});

}


I got error on line



authSuccessfull = authThread.auth();



java.lang.NullPointerException
at khartn.torrentsuploader.form.NewJFrame$4.run(NewJFrame.java:162)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)


Why this exception occurs?


And how to fix this exception?


Why my components is not autowired?


Thank you.


Aucun commentaire:

Enregistrer un commentaire