类org.springframework.boot.context.event.ApplicationFailedEvent源码实例Demo

下面列出了怎么用org.springframework.boot.context.event.ApplicationFailedEvent的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: mercury   文件: SpringEventListener.java
@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);
    }
}
 
/**
 * 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);
}
 
/**
 * 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);
}
 
源代码4 项目: mykit-delay   文件: FailedEventListener.java
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();
        }
    }
}
 
源代码5 项目: sdmq   文件: FailedEventListener.java
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();
        }
    }
}
 
@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());
}
 
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
	
	Throwable exception = event.getException();
	handleException(exception);
	
	logger.info("4 处理异常, FailedEventApplicationListener...");
}
 
源代码8 项目: spring-cloud-task   文件: TaskLifecycleListener.java
/**
 * 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();
	}
}
 
@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);
}
 
@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));

}
 
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof ApplicationFailedEvent) {
		this.context.close();
	}

}
 
源代码12 项目: mykit-delay   文件: FailedEventListener.java
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    Throwable throwable = event.getException();
    handler(throwable, event);
}
 
源代码13 项目: sdmq   文件: FailedEventListener.java
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    Throwable throwable = event.getException();
    handler(throwable, event);
}
 
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
    // 支持事件的类型
    return ApplicationReadyEvent.class.equals(eventType) ||
            ApplicationFailedEvent.class.equals(eventType);
}
 
@EventListener({ApplicationReadyEvent.class, ApplicationFailedEvent.class})
public void onSpringBootEvent(SpringApplicationEvent event) {
    System.out.println("@EventListener 监听到事件 : " + event.getClass().getSimpleName());
}
 
@EventListener(ApplicationFailedEvent.class)
public void onApplicationFailed() {
	unExportDubboMetadataConfigService();
}
 
源代码17 项目: seed   文件: ApplicationFailedEventListener.java
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
    Throwable cause = event.getException();
    System.out.println("SpringBoot启动异常-->" + cause.getMessage());
}
 
@Bean
public ApplicationListener<ApplicationFailedEvent> shutdownNotificatioin() {
    return new ShutdownNotification();
}
 
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
	return ApplicationFailedEvent.class.isAssignableFrom(eventType);
}
 
 类所在包
 同包方法