org.springframework.boot.context.event.ApplicationFailedEvent Java Examples

The following examples show how to use org.springframework.boot.context.event.ApplicationFailedEvent. 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: TaskExecutionListenerTests.java    From spring-cloud-task with Apache License 2.0 7 votes vote down vote up
/**
 * Verify that if a TaskExecutionListener Bean is present that the onTaskFailed method
 * is called.
 */
@Test
public void testTaskFail() {
	RuntimeException exception = new RuntimeException(EXCEPTION_MESSAGE);
	setupContextForTaskExecutionListener();
	SpringApplication application = new SpringApplication();
	DefaultTaskListenerConfiguration.TestTaskExecutionListener taskExecutionListener = this.context
			.getBean(
					DefaultTaskListenerConfiguration.TestTaskExecutionListener.class);
	this.context.publishEvent(new ApplicationFailedEvent(application, new String[0],
			this.context, exception));
	this.context.publishEvent(
			new ApplicationReadyEvent(application, new String[0], this.context));

	TaskExecution taskExecution = new TaskExecution(0, 1, "wombat", new Date(),
			new Date(), null, new ArrayList<>(), null, null);
	verifyListenerResults(true, true, taskExecution, taskExecutionListener);
}
 
Example #2
Source File: TaskExecutionListenerTests.java    From spring-cloud-task with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that if a bean has a @FailedTask annotation present that the associated
 * method is called.
 */
@Test
public void testAnnotationFail() {
	RuntimeException exception = new RuntimeException(EXCEPTION_MESSAGE);
	setupContextForAnnotatedListener();
	SpringApplication application = new SpringApplication();
	DefaultAnnotationConfiguration.AnnotatedTaskListener annotatedListener = this.context
			.getBean(DefaultAnnotationConfiguration.AnnotatedTaskListener.class);
	this.context.publishEvent(new ApplicationFailedEvent(application, new String[0],
			this.context, exception));
	this.context.publishEvent(
			new ApplicationReadyEvent(application, new String[0], this.context));

	TaskExecution taskExecution = new TaskExecution(0, 1, "wombat", new Date(),
			new Date(), null, new ArrayList<>(), null, null);
	verifyListenerResults(true, true, taskExecution, annotatedListener);
}
 
Example #3
Source File: SpringEventListener.java    From mercury with Apache License 2.0 6 votes vote down vote up
@EventListener
public void handleEvent(Object event) {
    // do optional life-cycle management
    if (event instanceof ServletWebServerInitializedEvent) {
        // when deploy as WAR, this event will not happen
        log.debug("Loading Spring Boot");
    }
    if (event instanceof ApplicationReadyEvent) {
        /*
         * this event will happen in both WAR and JAR deployment mode
         * At this point, Spring Boot is ready
         */
        if (!started) {
            new MainApps().start();
        }
    }
    // in case Spring Boot fails, it does not make sense to keep the rest of the application running.
    if (event instanceof ApplicationFailedEvent) {
        log.error("{}", ((ApplicationFailedEvent) event).getException().getMessage());
        System.exit(-1);
    }
}
 
Example #4
Source File: FailedEventListener.java    From mykit-delay with Apache License 2.0 5 votes vote down vote up
private void handler(Throwable throwable, ApplicationFailedEvent event) {
    ApplicationContext ctx = event.getApplicationContext();
    if (ctx != null) {
        RedisQueue redisQueue = ctx.getBean(RedisQueueImpl.class);
        if (redisQueue != null && redisQueue.isRunning()) {
            redisQueue.stop();
        }
    }
}
 
Example #5
Source File: BootstrapApplicationListener.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof ApplicationFailedEvent) {
		this.context.close();
	}

}
 
Example #6
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 #7
Source File: TaskLifecycleListenerTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskFailedUpdate() {
	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 ApplicationFailedEvent(application, new String[0],
			this.context, exception));
	this.context.publishEvent(
			new ApplicationReadyEvent(application, new String[0], this.context));

	verifyTaskExecution(0, true, 1, exception, null);
}
 
Example #8
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 #9
Source File: FailedEventApplicationListener.java    From micro-service with MIT License 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
	
	Throwable exception = event.getException();
	handleException(exception);
	
	logger.info("4 处理异常, FailedEventApplicationListener...");
}
 
Example #10
Source File: ShutdownNotification.java    From spring-backend-boilerplate with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    if (StringUtils.isEmpty(webhookConfigurationProperties.getUrl())) {
        return;
    }

    webhookClient.sendMessage(webhookConfigurationProperties.getUrl(),
                              String.format("%s startup failed. The cause is %s",
                                            getAppName(),
                                            event.getException().getMessage()),
                              event.getException().getMessage());
}
 
Example #11
Source File: FailedEventListener.java    From sdmq with Apache License 2.0 5 votes vote down vote up
private void handler(Throwable throwable, ApplicationFailedEvent event) {
    ApplicationContext ctx = event.getApplicationContext();
    if (ctx != null) {
        RedisQueueImpl redisQueue = ctx.getBean(RedisQueueImpl.class);
        if (redisQueue != null && redisQueue.isRunning()) {
            redisQueue.stop();
        }
    }
}
 
Example #12
Source File: WebHookSupportConfiguration.java    From spring-backend-boilerplate with Apache License 2.0 4 votes vote down vote up
@Bean
public ApplicationListener<ApplicationFailedEvent> shutdownNotificatioin() {
    return new ShutdownNotification();
}
 
Example #13
Source File: ApplicationFailedEventListener.java    From seed with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    Throwable cause = event.getException();
    System.out.println("SpringBoot启动异常-->" + cause.getMessage());
}
 
Example #14
Source File: DubboMetadataAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@EventListener(ApplicationFailedEvent.class)
public void onApplicationFailed() {
	unExportDubboMetadataConfigService();
}
 
Example #15
Source File: MultipleSpringBootEventsListener.java    From thinking-in-spring-boot-samples with Apache License 2.0 4 votes vote down vote up
@EventListener({ApplicationReadyEvent.class, ApplicationFailedEvent.class})
public void onSpringBootEvent(SpringApplicationEvent event) {
    System.out.println("@EventListener 监听到事件 : " + event.getClass().getSimpleName());
}
 
Example #16
Source File: MultipleSpringBootEventsListener.java    From thinking-in-spring-boot-samples with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
    // 支持事件的类型
    return ApplicationReadyEvent.class.equals(eventType) ||
            ApplicationFailedEvent.class.equals(eventType);
}
 
Example #17
Source File: FailedEventListener.java    From sdmq with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    Throwable throwable = event.getException();
    handler(throwable, event);
}
 
Example #18
Source File: BootstrapApplicationListener.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
	return ApplicationFailedEvent.class.isAssignableFrom(eventType);
}
 
Example #19
Source File: FailedEventListener.java    From mykit-delay with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    Throwable throwable = event.getException();
    handler(throwable, event);
}