@Test public void logsDebugOnError() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(ErrorConfig.class); try { context.refresh(); fail("Did not error"); } catch (Exception ex) { this.initializer.onApplicationEvent(new ApplicationFailedEvent( new SpringApplication(), new String[0], context, ex)); } assertThat(this.debugLog.size()).isNotEqualTo(0); assertThat(this.infoLog.size()).isEqualTo(0); }
@Test public void logsInfoOnErrorIfDebugDisabled() { setupLogging(false, true); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(ErrorConfig.class); try { context.refresh(); fail("Did not error"); } catch (Exception ex) { this.initializer.onApplicationEvent(new ApplicationFailedEvent( new SpringApplication(), new String[0], context, ex)); } assertThat(this.debugLog.size()).isEqualTo(0); assertThat(this.infoLog.size()).isNotEqualTo(0); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { onApplicationStartedEvent((ApplicationStartedEvent) event); } if (event instanceof ApplicationPreparedEvent) { Restarter.getInstance() .prepare(((ApplicationPreparedEvent) event).getApplicationContext()); } if (event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) { Restarter.getInstance().finish(); if (event instanceof ApplicationFailedEvent) { Restarter.getInstance().prepare(null); } } }
private void testInitialize(boolean failed) { Restarter.clearInstance(); RestartApplicationListener listener = new RestartApplicationListener(); SpringApplication application = new SpringApplication(); ConfigurableApplicationContext context = mock( ConfigurableApplicationContext.class); listener.onApplicationEvent(new ApplicationStartedEvent(application, ARGS)); assertThat(Restarter.getInstance()).isNotEqualTo(nullValue()); assertThat(Restarter.getInstance().isFinished()).isFalse(); listener.onApplicationEvent( new ApplicationPreparedEvent(application, ARGS, context)); if (failed) { listener.onApplicationEvent(new ApplicationFailedEvent(application, ARGS, context, new RuntimeException())); } else { listener.onApplicationEvent( new ApplicationReadyEvent(application, ARGS, context)); } }
@Test public void logsDebugOnError() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(ErrorConfig.class); try { context.refresh(); fail("Did not error"); } catch (Exception ex) { this.initializer.onApplicationEvent(new ApplicationFailedEvent( new SpringApplication(), new String[0], context, ex)); } assertThat(this.debugLog.size(), not(equalTo(0))); assertThat(this.infoLog.size(), equalTo(0)); }
@Test public void logsInfoOnErrorIfDebugDisabled() { setupLogging(false, true); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(ErrorConfig.class); try { context.refresh(); fail("Did not error"); } catch (Exception ex) { this.initializer.onApplicationEvent(new ApplicationFailedEvent( new SpringApplication(), new String[0], context, ex)); } assertThat(this.debugLog.size(), equalTo(0)); assertThat(this.infoLog.size(), not(equalTo(0))); }
private void testInitialize(boolean failed) { Restarter.clearInstance(); RestartApplicationListener listener = new RestartApplicationListener(); SpringApplication application = new SpringApplication(); ConfigurableApplicationContext context = mock( ConfigurableApplicationContext.class); listener.onApplicationEvent(new ApplicationStartedEvent(application, ARGS)); assertThat(Restarter.getInstance(), not(nullValue())); assertThat(Restarter.getInstance().isFinished(), equalTo(false)); if (failed) { listener.onApplicationEvent(new ApplicationFailedEvent(application, ARGS, context, new RuntimeException())); } else { listener.onApplicationEvent( new ApplicationReadyEvent(application, ARGS, context)); } }
@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()); }
@SuppressWarnings("ALL") @Override public void onApplicationEvent(ApplicationFailedEvent event) { SimpleCommandLinePropertySource commandLinePropertySource = new SimpleCommandLinePropertySource(event.getArgs()); if (commandLinePropertySource.containsProperty("ding-url")) { final String name = ListenerUtils.getServerName(commandLinePropertySource.getProperty("ding-app-name")); //ding-url ding-msg Map<String, Object> map = of( "msgtype", "text", "text", of("content", "【[对不起]】" + name + "启动失败。") ); new Thread() { @Override public void run() { super.run(); String url = commandLinePropertySource.getProperty("ding-url"); try { String returnString = Request.Post(url).connectTimeout(8000) .bodyString(JSON.toJSONString(map), ContentType.APPLICATION_JSON).execute().returnContent().asString(); log.info("钉钉通知已发送 return:" + returnString); } catch (Exception e) { log.warn("发送钉钉通知失败", e); } } }.start(); } }
@Override public void onApplicationEvent(ApplicationEvent event) { if (this.logger.isDebugEnabled()) { if (event instanceof ApplicationEnvironmentPreparedEvent) { this.logger .debug("Application started with classpath: " + getClasspath()); } else if (event instanceof ApplicationFailedEvent) { this.logger.debug( "Application failed to start with classpath: " + getClasspath()); } } }
@Override public boolean supportsEventType(ResolvableType resolvableType) { Class<?> type = resolvableType.getRawClass(); if (type == null) { return false; } return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(type) || ApplicationFailedEvent.class.isAssignableFrom(type); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextClosedEvent) { onContextClosedEvent((ContextClosedEvent) event); } if (event instanceof ApplicationFailedEvent) { onApplicationFailedEvent((ApplicationFailedEvent) event); } }
@Test public void onDifferentPortWithPrimaryFailure() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); ApplicationFailedEvent event = mock(ApplicationFailedEvent.class); given(event.getApplicationContext()).willReturn(this.applicationContext); this.applicationContext.publishEvent(event); assertThat(((ConfigurableApplicationContext) managementContext).isActive()) .isFalse(); }
protected void onApplicationEvent(ApplicationEvent event) { ConfigurableApplicationContext initializerApplicationContext = AutoConfigurationReportLoggingInitializer.this.applicationContext; if (event instanceof ContextRefreshedEvent) { if (((ApplicationContextEvent) event) .getApplicationContext() == initializerApplicationContext) { logAutoConfigurationReport(); } } else if (event instanceof ApplicationFailedEvent) { if (((ApplicationFailedEvent) event) .getApplicationContext() == initializerApplicationContext) { logAutoConfigurationReport(true); } } }
@Override public boolean supportsEventType(ResolvableType resolvableType) { Class<?> type = resolvableType.getRawClass(); if (type == null) { return false; } return ContextRefreshedEvent.class.isAssignableFrom(type) || ApplicationFailedEvent.class.isAssignableFrom(type); }
@Test public void noErrorIfNotInitialized() throws Exception { this.initializer .onApplicationEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], null, new RuntimeException("Planned"))); assertThat(this.infoLog.get(0)) .contains("Unable to provide auto-configuration report"); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { if (this.logger.isDebugEnabled()) { this.logger .debug("Application started with classpath: " + getClasspath()); } } else if (event instanceof ApplicationFailedEvent) { if (this.logger.isInfoEnabled()) { this.logger.info( "Application failed to start with classpath: " + getClasspath()); } } }
@Override public boolean supportsEventType(ResolvableType resolvableType) { Class<?> type = resolvableType.getRawClass(); if (type == null) { return false; } return ApplicationStartedEvent.class.isAssignableFrom(type) || ApplicationFailedEvent.class.isAssignableFrom(type); }
@Test public void noErrorIfNotInitialized() throws Exception { this.initializer .onApplicationEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], null, new RuntimeException("Planned"))); assertThat(this.infoLog.get(0), containsString("Unable to provide auto-configuration report")); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { onApplicationStartedEvent((ApplicationStartedEvent) event); } if (event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) { Restarter.getInstance().finish(); } }
/** * Utilizes {@link ApplicationEvent}s to determine the start, end, and failure of a * task. Specifically: * <ul> * <li>{@link ContextRefreshedEvent} - Start of a task</li> * <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.applicationFailedEvent = (ApplicationFailedEvent) applicationEvent; doTaskEnd(); } else if(applicationEvent instanceof ExitCodeEvent){ this.exitCodeEvent = (ExitCodeEvent) applicationEvent; } else if(applicationEvent instanceof ApplicationReadyEvent) { doTaskEnd(); } }
/** * 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 = context.getBean(DefaultTaskListenerConfiguration.TestTaskExecutionListener.class); context.publishEvent(new ApplicationFailedEvent(application, new String[0], context, exception)); context.publishEvent(new ApplicationReadyEvent(application, new String[0], context)); TaskExecution taskExecution = new TaskExecution(0, 1, "wombat", new Date(), new Date(), null, new ArrayList<String>(), null, null); verifyListenerResults(true, 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 = context.getBean(DefaultAnnotationConfiguration.AnnotatedTaskListener.class); context.publishEvent(new ApplicationFailedEvent(application, new String[0], context, exception)); context.publishEvent(new ApplicationReadyEvent(application, new String[0], context)); TaskExecution taskExecution = new TaskExecution(0, 1, "wombat", new Date(), new Date(), null, new ArrayList<String>(), null, null); verifyListenerResults(true, true, true, taskExecution,annotatedListener); }
@Test public void testTaskFailedUpdate() { context.refresh(); RuntimeException exception = new RuntimeException("This was expected"); SpringApplication application = new SpringApplication(); this.taskExplorer = context.getBean(TaskExplorer.class); context.publishEvent(new ApplicationFailedEvent(application, new String[0], context, exception)); context.publishEvent(new ApplicationReadyEvent(application, new String[0], context)); verifyTaskExecution(0, true, 1, exception, null); }
@Test public void testTaskFailedWithExitCodeEvent() { final int exitCode = 10; context.refresh(); RuntimeException exception = new RuntimeException("This was expected"); SpringApplication application = new SpringApplication(); this.taskExplorer = context.getBean(TaskExplorer.class); context.publishEvent(new ExitCodeEvent(context, exitCode)); context.publishEvent(new ApplicationFailedEvent(application, new String[0], context, exception)); context.publishEvent(new ApplicationReadyEvent(application, new String[0], context)); verifyTaskExecution(0, true, exitCode, exception, null); }
@Bean public ApplicationListener<ApplicationFailedEvent> shutdownNotificatioin() { return new ShutdownNotification(); }
private void onApplicationFailedEvent(ApplicationFailedEvent event) { propagateCloseIfNecessary(event.getApplicationContext()); }