@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartedEvent) { onApplicationStartedEvent((ApplicationStartedEvent) event); } else if (event instanceof ApplicationEnvironmentPreparedEvent) { onApplicationEnvironmentPreparedEvent( (ApplicationEnvironmentPreparedEvent) event); } else if (event instanceof ApplicationPreparedEvent) { onApplicationPreparedEvent((ApplicationPreparedEvent) event); } else if (event instanceof ContextClosedEvent && ((ContextClosedEvent) event) .getApplicationContext().getParent() == null) { onContextClosedEvent(); } }
@Test public void eventsOrder() { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setWebEnvironment(false); final List<ApplicationEvent> events = new ArrayList<ApplicationEvent>(); class ApplicationRunningEventListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { events.add((event)); } } application.addListeners(new ApplicationRunningEventListener()); this.context = application.run(); assertThat(events).hasSize(5); assertThat(events.get(0)).isInstanceOf(ApplicationStartedEvent.class); assertThat(events.get(1)).isInstanceOf(ApplicationEnvironmentPreparedEvent.class); assertThat(events.get(2)).isInstanceOf(ApplicationPreparedEvent.class); assertThat(events.get(3)).isInstanceOf(ContextRefreshedEvent.class); assertThat(events.get(4)).isInstanceOf(ApplicationReadyEvent.class); }
@Test public void registerListener() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class, ListenerConfig.class); application.setApplicationContextClass(SpyApplicationContext.class); final LinkedHashSet<ApplicationEvent> events = new LinkedHashSet<ApplicationEvent>(); application.addListeners(new ApplicationListener<ApplicationEvent>() { @Override public void onApplicationEvent(ApplicationEvent event) { events.add(event); } }); this.context = application.run(); assertThat(events).hasAtLeastOneElementOfType(ApplicationPreparedEvent.class); assertThat(events).hasAtLeastOneElementOfType(ContextRefreshedEvent.class); verifyTestListenerEvents(); }
@Test public void registerListenerWithCustomMulticaster() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class, ListenerConfig.class, Multicaster.class); application.setApplicationContextClass(SpyApplicationContext.class); final LinkedHashSet<ApplicationEvent> events = new LinkedHashSet<ApplicationEvent>(); application.addListeners(new ApplicationListener<ApplicationEvent>() { @Override public void onApplicationEvent(ApplicationEvent event) { events.add(event); } }); this.context = application.run(); assertThat(events).hasAtLeastOneElementOfType(ApplicationPreparedEvent.class); assertThat(events).hasAtLeastOneElementOfType(ContextRefreshedEvent.class); verifyTestListenerEvents(); }
@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 eventsOrder() { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setWebEnvironment(false); final List<ApplicationEvent> events = new ArrayList<ApplicationEvent>(); class ApplicationRunningEventListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { events.add((event)); } } application.addListeners(new ApplicationRunningEventListener()); this.context = application.run(); assertThat(5, is(events.size())); assertThat(events.get(0), is(instanceOf(ApplicationStartedEvent.class))); assertThat(events.get(1), is(instanceOf(ApplicationEnvironmentPreparedEvent.class))); assertThat(events.get(2), is(instanceOf(ApplicationPreparedEvent.class))); assertThat(events.get(3), is(instanceOf(ContextRefreshedEvent.class))); assertThat(events.get(4), is(instanceOf(ApplicationReadyEvent.class))); }
@Test public void registerListenerWithCustomMulticaster() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class, Multicaster.class); application.setApplicationContextClass(SpyApplicationContext.class); final LinkedHashSet<ApplicationEvent> events = new LinkedHashSet<ApplicationEvent>(); application.addListeners(new ApplicationListener<ApplicationEvent>() { @Override public void onApplicationEvent(ApplicationEvent event) { events.add(event); } }); this.context = application.run(); assertThat(events, hasItem(isA(ApplicationPreparedEvent.class))); assertThat(events, hasItem(isA(ContextRefreshedEvent.class))); }
@Override public void onApplicationEvent(ApplicationEvent input) { if (input instanceof ApplicationPreparedEvent) { this.event = (ApplicationPreparedEvent) input; if (this.context == null) { this.context = this.event.getApplicationContext(); } } else if (input instanceof ContextRefreshedEvent) { if (this.context != null && input.getSource().equals(this.context) && this.event != null) { this.context.publishEvent(this.event); } } else { if (this.context != null && input.getSource().equals(this.context)) { this.context = null; this.event = null; } } }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationPreparedEvent) { if (running == Boolean.FALSE) running = Boolean.TRUE; if (holdThread == null) { holdThread = new Thread(new Runnable() { @Override public void run() { if (LOGGER.isTraceEnabled()) { LOGGER.trace(Thread.currentThread().getName()); } while (running && !Thread.currentThread().isInterrupted()) { try { Thread.sleep(2000); } catch (InterruptedException e) { } } } }, "Dubbo-Holder"); holdThread.setDaemon(false); holdThread.start(); } } if (event instanceof ContextClosedEvent) { running = Boolean.FALSE; if (null != holdThread) { holdThread.interrupt(); holdThread = null; } } }
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) { ConfigurableListableBeanFactory beanFactory = event.getApplicationContext() .getBeanFactory(); if (!beanFactory.containsBean(LOGGING_SYSTEM_BEAN_NAME)) { beanFactory.registerSingleton(LOGGING_SYSTEM_BEAN_NAME, this.loggingSystem); } }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationEnvironmentPreparedEvent) { onApplicationEnvironmentPreparedEvent( (ApplicationEnvironmentPreparedEvent) event); } if (event instanceof ApplicationPreparedEvent) { onApplicationPreparedEvent(event); } }
private Environment getEnvironment(SpringApplicationEvent event) { if (event instanceof ApplicationEnvironmentPreparedEvent) { return ((ApplicationEnvironmentPreparedEvent) event).getEnvironment(); } if (event instanceof ApplicationPreparedEvent) { return ((ApplicationPreparedEvent) event).getApplicationContext() .getEnvironment(); } return null; }
private SpringApplicationEvent createPreparedEvent(String propName, String propValue) { ConfigurableEnvironment environment = createEnvironment(propName, propValue); ConfigurableApplicationContext context = mock( ConfigurableApplicationContext.class); given(context.getEnvironment()).willReturn(environment); return new ApplicationPreparedEvent(new SpringApplication(), new String[] {}, context); }
@Override public void onApplicationEvent(ApplicationPreparedEvent event) { ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment(); if (StringUtils.hasText(environment.resolvePlaceholders("${dm.config.git.uri:}"))) { if (!environment.getPropertySources().contains(this.propertySource.getName())) { environment.getPropertySources().addLast(this.propertySource); } } }
@Test public void registerListener() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class); application.setApplicationContextClass(SpyApplicationContext.class); final LinkedHashSet<ApplicationEvent> events = new LinkedHashSet<ApplicationEvent>(); application.addListeners(new ApplicationListener<ApplicationEvent>() { @Override public void onApplicationEvent(ApplicationEvent event) { events.add(event); } }); this.context = application.run(); assertThat(events, hasItem(isA(ApplicationPreparedEvent.class))); assertThat(events, hasItem(isA(ContextRefreshedEvent.class))); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationEnvironmentPreparedEvent) { ConfigurableEnvironment environment = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment(); configure(environment); if(shouldInitializeHome) { initialize(); } } else if (event instanceof ApplicationPreparedEvent) { writeLogs(); } }
@Override public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) { return ApplicationPreparedEvent.class.isAssignableFrom(eventType) || ContextRefreshedEvent.class.isAssignableFrom(eventType) || ContextClosedEvent.class.isAssignableFrom(eventType); }
@Override public void onApplicationEvent(ApplicationPreparedEvent input) { this.event = input; if (this.context == null) { this.context = this.event.getApplicationContext(); this.args = this.event.getArgs(); this.application = this.event.getSpringApplication(); } }
@Override public void onApplicationEvent(ApplicationPreparedEvent event) { String jobLogPath = event.getApplicationContext().getEnvironment().getProperty("batch.joblog.path"); if (!StringUtils.isEmpty(jobLogPath)) { if (!jobLogPath.endsWith("/")) { jobLogPath = jobLogPath + "/"; } System.setProperty("JOB_LOG_PATH", jobLogPath); } }
private void onApplicationPreparedEvent(ApplicationEvent event) { this.logger.replayTo(ConfigFileApplicationListener.class); addPostProcessors(((ApplicationPreparedEvent) event).getApplicationContext()); }
@Override public void onApplicationEvent(ApplicationPreparedEvent event) { this.logger.replayTo(ConfigFileEnvironmentPostProcessor.class); addPostProcessors(event.getApplicationContext()); }
@Override public void onApplicationEvent(final ApplicationPreparedEvent event) { //LOGGER.replayTo(DockerSecretEnvironmentPostProcessor.class); }