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

The following examples show how to use org.springframework.boot.context.event.ApplicationFailedEvent. These examples are extracted from open source projects. 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 Project: mercury   Source File: SpringEventListener.java    License: 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 2
/**
 * 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 3
/**
 * 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 4
Source Project: mykit-delay   Source File: FailedEventListener.java    License: 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 Project: sdmq   Source File: FailedEventListener.java    License: 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 6
@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 7
Source Project: micro-service   Source File: FailedEventApplicationListener.java    License: 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 8
Source Project: spring-cloud-task   Source File: TaskLifecycleListener.java    License: 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
@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 10
@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 11
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof ApplicationFailedEvent) {
		this.context.close();
	}

}
 
Example 12
Source Project: mykit-delay   Source File: FailedEventListener.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    Throwable throwable = event.getException();
    handler(throwable, event);
}
 
Example 13
Source Project: sdmq   Source File: FailedEventListener.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    Throwable throwable = event.getException();
    handler(throwable, event);
}
 
Example 14
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
    // 支持事件的类型
    return ApplicationReadyEvent.class.equals(eventType) ||
            ApplicationFailedEvent.class.equals(eventType);
}
 
Example 15
@EventListener({ApplicationReadyEvent.class, ApplicationFailedEvent.class})
public void onSpringBootEvent(SpringApplicationEvent event) {
    System.out.println("@EventListener 监听到事件 : " + event.getClass().getSimpleName());
}
 
Example 16
@EventListener(ApplicationFailedEvent.class)
public void onApplicationFailed() {
	unExportDubboMetadataConfigService();
}
 
Example 17
Source Project: seed   Source File: ApplicationFailedEventListener.java    License: 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 18
@Bean
public ApplicationListener<ApplicationFailedEvent> shutdownNotificatioin() {
    return new ShutdownNotification();
}
 
Example 19
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
	return ApplicationFailedEvent.class.isAssignableFrom(eventType);
}