Корректное завершение spring boot Application
Есть приложение на spring boot
@EnableScheduling
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class Application {
private static final Logger log = Logger.getLogger(Application.class);
@Value("${executor}")
private String executorName;
@Autowired
public void setExecutorName(ApplicationContext context) {
context.getBean(executorName);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
которое запускает Scheduler
@Service("exporter")
@PropertySource("classpath:application.properties")
public class ExecutorExporterService {
@Value("${topicName}")
String topicName;
@Autowired
EntityRepository entityRepository;
@Autowired
ConsumerKafka consumerKafka;
@Transactional
@Scheduled(fixedRateString = "${timeInterval}")
public void runExecutor() throws InterruptedException, ExecutionException {
Set<ConsumerRecord> consumerRecords = consumerKafka.consumeKafka(Collections.singletonList(topicName));
List<Person> persons = consumerRecords.stream().map(record -> (Person) record.value()).collect(Collectors.toList());
entityRepository.saveAll(persons);
consumerKafka.commitSyncConsumer();
consumerKafka.closeConsumer();
System.out.println(persons);
}
}
запускаю mvn spring-boot:start
пытаюсь остановить: mvn spring-boot:stop
выдает:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.4.RELEASE:stop (default-cli) on project leantegra_importer: Spring application lifecycle JMX bean not found (fork is null). Could not stop application gracefully: org.springframework.boot:type=Admin,name=SpringApplication -> [Help 1]
И приложение не останавливается.
Как сделать корректную остановку? И возможно ли это сделать по Ctrl C?
Ответы (1 шт):
Автор решения: Jackson750
→ Ссылка
В данной статье есть несколько вариантов
Способ №1:
@RestController
public class ShutdownController implements ApplicationContextAware {
private ApplicationContext context;
@PostMapping("/shutdownContext")
public void shutdownContext() {
((ConfigurableApplicationContext) context).close();
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.context = ctx;
}
}
и через curl curl -X POST localhost:port/shutdownContext
Способ №2:
SpringApplicationBuilder app = new
SpringApplicationBuilder(Application.class)
.web(WebApplicationType.NONE);
app.build().addListeners(new
ApplicationPidFileWriter("./bin/shutdown.pid"));
app.run();
В консоли kill $(cat ./bin/shutdown.pid)