我从java / springboot开始,我想在几个同时执行导出块的小类中分发导出功能(最初是由SpingApplication管理的单个类)。
/*
Here is my initial application class setted conventionally according to SpringBoot practice ...
Which works but it's too long
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
public static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Exporter.class, args);
Exporter exporter = ctx.getBean(Exporter.class);
exporter.main(args);
}
}
/* The runnable classe in Exporter.java*/
package moteur;
import config.ClauseWhereConfig;
import config.FtpConfig;
import config.PigeExportHighcoConfig;
import config.Sql2oConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@EnableAutoConfiguration
@Import({ ExportSession.class})
public class Exporter {
@Autowired
private ExportSession currentExport;
public static Logger logger = LoggerFactory.getLogger(Exporter.class);
public void main(String[] args) {
run(args);
}
public void run(String[] args) {
/*do some stuff ... */
}
/*
Then I tried this :
Each exporter instance is supposed to make a piece of "select from" original (as you know with the keyword limit <from>, <to>
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class Application {
public static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Exporter.class, args);
Exporter exporter1 = ctx.getBean(Exporter.class);
Exporter exporter2 = ctx.getBean(Exporter.class);
exporter1.setName("exporter1");
exporter2.setName("exporter2");
exporter1.start();
exporter2.start();
}
}
/* The Exporter class now extends Thread object */
@Configuration
@EnableAutoConfiguration
@EnableAsync
@Import({ ExportSession.class, Sql2oConfig.class, ClauseWhereConfig.class, FtpConfig.class, PigeExportHighcoConfig.class})
public class Exporter extends Thread {
@Autowired
private ExportSession currentExport;
public static Logger logger = LoggerFactory.getLogger(Exporter.class);
public void main(String[] args) {
run(args);
}
public void run(String[] args) {
logger.info(getName() + "---- >: Is running" ) ;
}
这种愚蠢的尝试当然是行不通的:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at moteur.Application.main(Application.java:27)
事实上,我正在寻找小型自主批量治疗的解决方案 你有这个吗?
答案 0 :(得分:-1)
为什么不使用Java 8的completableFuture异步启动任务。我相信它更简单,最佳实践