org.springframework.boot.ExitCodeEvent Java Examples

The following examples show how to use org.springframework.boot.ExitCodeEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExitCodeEventOnExitBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    System.exit(SpringApplication.exit(
            new SpringApplicationBuilder(ExitCodeEventOnExitBootstrap.class)
                    .listeners((ApplicationListener<ExitCodeEvent>) event ->
                            System.out.println("监听到退出码:" + event.getExitCode())
                    )
                    .web(false) // 非 Web 应用
                    .run(args)  // 运行 SpringBoot 应用
    ));
}
 
Example #2
Source File: TaskLifecycleListener.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
/**
 * Utilizes {@link ApplicationEvent}s to determine the end and failure of a task.
 * Specifically:
 * <ul>
 * <li>{@link ApplicationReadyEvent} - Successful end of a task</li>
 * <li>{@link ApplicationFailedEvent} - Failure of a task</li>
 * </ul>
 * @param applicationEvent The application being listened for.
 */
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
	if (applicationEvent instanceof ApplicationFailedEvent) {
		this.applicationFailedException = ((ApplicationFailedEvent) applicationEvent)
				.getException();
		doTaskEnd();
	}
	else if (applicationEvent instanceof ExitCodeEvent) {
		this.exitCodeEvent = (ExitCodeEvent) applicationEvent;
	}
	else if (applicationEvent instanceof ApplicationReadyEvent) {
		doTaskEnd();
	}
}
 
Example #3
Source File: TaskLifecycleListenerTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskFailedWithExitCodeEvent() {
	final int exitCode = 10;
	this.context.register(TestListener.class);
	this.context.register(TestListener2.class);

	this.context.refresh();
	RuntimeException exception = new RuntimeException("This was expected");
	SpringApplication application = new SpringApplication();
	this.taskExplorer = this.context.getBean(TaskExplorer.class);
	this.context.publishEvent(new ExitCodeEvent(this.context, exitCode));
	this.context.publishEvent(new ApplicationFailedEvent(application, new String[0],
			this.context, exception));
	this.context.publishEvent(
			new ApplicationReadyEvent(application, new String[0], this.context));

	verifyTaskExecution(0, true, exitCode, exception, null);
	assertThat(TestListener.getStartupOrderList().size()).isEqualTo(2);
	assertThat(TestListener.getStartupOrderList().get(0))
			.isEqualTo(Integer.valueOf(2));
	assertThat(TestListener.getStartupOrderList().get(1))
			.isEqualTo(Integer.valueOf(1));

	assertThat(TestListener.getEndOrderList().size()).isEqualTo(2);
	assertThat(TestListener.getEndOrderList().get(0)).isEqualTo(Integer.valueOf(1));
	assertThat(TestListener.getEndOrderList().get(1)).isEqualTo(Integer.valueOf(2));

	assertThat(TestListener.getFailOrderList().size()).isEqualTo(2);
	assertThat(TestListener.getFailOrderList().get(0)).isEqualTo(Integer.valueOf(1));
	assertThat(TestListener.getFailOrderList().get(1)).isEqualTo(Integer.valueOf(2));

}
 
Example #4
Source File: ExitCodeEventDemoApplication.java    From tutorials with MIT License 4 votes vote down vote up
@EventListener
public void exitEvent(ExitCodeEvent event) {
    System.out.println("Exit code: " + event.getExitCode());
}