/** * Initialize the ApplicationEventMulticaster. * Uses SimpleApplicationEventMulticaster if none defined in the context. * @see org.springframework.context.event.SimpleApplicationEventMulticaster */ protected void initApplicationEventMulticaster() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) { this.applicationEventMulticaster = beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class); if (logger.isDebugEnabled()) { logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]"); } } else { this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory); beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster); if (logger.isDebugEnabled()) { logger.debug("Unable to locate ApplicationEventMulticaster with name '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "': using default [" + this.applicationEventMulticaster + "]"); } } }
private synchronized ApplicationEventMulticaster getApplicationEventMulticaster() { try { Field field = DataStore.class.getDeclaredField("mainContext"); field.setAccessible(true); AbstractApplicationContext ctx = (AbstractApplicationContext) field.get(DataStore.getInstance()); if (ctx == null) return null; Method m = AbstractApplicationContext.class.getDeclaredMethod("getApplicationEventMulticaster"); m.setAccessible(true); ApplicationEventMulticaster eventCaster = (ApplicationEventMulticaster) m.invoke(ctx); field.setAccessible(false); m.setAccessible(false); return eventCaster; } catch (Exception e) { e.printStackTrace(); } return null; }
private Editor createEditor(){ Object o = BeanUtils.instantiateClass(editorClass); Assert.isTrue((o instanceof Editor), "Editor class '"+ editorClass + "' was instantiated but is not an Editor"); Editor editor = (Editor)o; editor.setDescriptor(this); ApplicationEventMulticaster multicaster = getApplicationEventMulticaster(); if(editor instanceof ApplicationListener && multicaster != null){ multicaster.addApplicationListener((ApplicationListener)editor); } if(editorProperties != null){ BeanWrapper wrapper = new BeanWrapperImpl(editor); wrapper.setPropertyValues(editorProperties); } if(editor instanceof InitializingBean){ try { ((InitializingBean)editor).afterPropertiesSet(); } catch (Exception e) { throw new BeanInitializationException("Problem running on " + editor, e); } } return editor; }
@Bean(name = "applicationEventMulticaster") public ApplicationEventMulticaster simpleApplicationEventMulticaster() { SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setThreadNamePrefix("asyncEventExecutor-"); taskExecutor.setCorePoolSize(4); taskExecutor.initialize(); eventMulticaster.setTaskExecutor(taskExecutor); return eventMulticaster; }
@Override public void publishEvent(ApplicationEvent event) { Assert.notNull(event, "Event must not be null"); if (logger.isTraceEnabled()) { logger.trace("Publishing event in " + getDisplayName() + ": " + event); } ((ApplicationEventMulticaster) getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)).multicastEvent(event); if (!(getParent() == null || event instanceof ContextRefreshedEvent || event instanceof ContextClosedEvent)) { getParent().publishEvent(event); } }
/** * Return the internal ApplicationEventMulticaster used by the context. * @return the internal ApplicationEventMulticaster (never {@code null}) * @throws IllegalStateException if the context has not been initialized yet */ ApplicationEventMulticaster getApplicationEventMulticaster() throws IllegalStateException { if (this.applicationEventMulticaster == null) { throw new IllegalStateException("ApplicationEventMulticaster not initialized - " + "call 'refresh' before multicasting events via the context: " + this); } return this.applicationEventMulticaster; }
@Override public void postProcessBeforeDestruction(Object bean, String beanName) { if (bean instanceof ApplicationListener) { ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster(); multicaster.removeApplicationListener((ApplicationListener<?>) bean); multicaster.removeApplicationListenerBean(beanName); } }
@Bean(name = "applicationEventMulticaster") public ApplicationEventMulticaster simpleApplicationEventMulticaster() { multicasterExecutor.initialize(); SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); eventMulticaster.setTaskExecutor(multicasterExecutor); return eventMulticaster; }
@Before public void init() throws Throwable { stubDemo(); pluginDao.refresh(); initGit(); if (Files.exists(Paths.get(gitWorkspace.toString(), "plugin_cache.json"))) { Files.delete(Paths.get(gitWorkspace.toString(), "plugin_cache.json")); } applicationEventMulticaster = (ApplicationEventMulticaster) applicationContext .getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME); }
/** * Constructor. * * @param asyncEventMulticaster The asynchronous event multicaster to use */ public MetacatApplicationEventMulticaster( @Nonnull @NonNull final ApplicationEventMulticaster asyncEventMulticaster ) { super(); this.asyncEventMulticaster = asyncEventMulticaster; }
/** * The application event multicaster to use. * * @param asyncEventMulticaster The asynchronous event publisher * @return The application event multicaster to use. */ @Bean public MetacatApplicationEventMulticaster applicationEventMulticaster( final ApplicationEventMulticaster asyncEventMulticaster ) { return new MetacatApplicationEventMulticaster(asyncEventMulticaster); }
/** * A multicast (async) event publisher to replace the synchronous one used by Spring via the ApplicationContext. * * @param taskExecutor The task executor to use * @return The application event multicaster to use */ @Bean public ApplicationEventMulticaster asyncEventMulticaster(final TaskExecutor taskExecutor) { final SimpleApplicationEventMulticaster applicationEventMulticaster = new SimpleApplicationEventMulticaster(); applicationEventMulticaster.setTaskExecutor(taskExecutor); return applicationEventMulticaster; }
/** * Server internal event publisher that allows parallel event processing if * the event listener is marked as so. * * @return publisher bean */ @Bean(name = AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME) public ApplicationEventMulticaster applicationEventMulticaster() { final SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new TenantAwareApplicationEventPublisher( tenantAware, applicationEventFilter()); simpleApplicationEventMulticaster.setTaskExecutor(executor); return simpleApplicationEventMulticaster; }
/** * 修改特定上下文的Event Multicaster的默认实现 * * @param context 被修改的上下文 */ public static void initApplicationEventMulticaster(GenericApplicationContext context) { ApplicationEventMulticaster multicaster; ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) { multicaster = beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class); } else { multicaster = new SmartApplicationEventMulticaster(beanFactory); beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster); } setApplicationEventMulticaster(context, multicaster); }
private static void setApplicationEventMulticaster(GenericApplicationContext context, ApplicationEventMulticaster applicationEventMulticaster) { try { FieldUtils.writeField(context, "applicationEventMulticaster", applicationEventMulticaster, true); } catch (IllegalAccessException e) { throw new ApplicationContextException("Can't hacking the spring context for applicationEventMulticaster", e); } }
/** * Return the internal ApplicationEventMulticaster used by the context. * @return the internal ApplicationEventMulticaster (never {@code null}) * @throws IllegalStateException if the context has not been initialized yet */ private ApplicationEventMulticaster getApplicationEventMulticaster() throws IllegalStateException { if (this.applicationEventMulticaster == null) { throw new IllegalStateException("ApplicationEventMulticaster not initialized - " + "call 'refresh' before multicasting events via the context: " + this); } return this.applicationEventMulticaster; }
public ApplicationEventMulticaster getApplicationEventMulticaster() { if (getApplicationContext() != null) { final String beanName = AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME; if (getApplicationContext().containsBean(beanName)) { return (ApplicationEventMulticaster)getApplicationContext().getBean(beanName); } } return null; }
@Bean(name = "applicationEventMulticaster") public ApplicationEventMulticaster simpleApplicationEventMulticaster() { SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); eventMulticaster.setTaskExecutor(executor); return eventMulticaster; }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; this.eventMulticaster = (ApplicationEventMulticaster) applicationContext .getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME); }
public void setAsyncEventMulticaster(ApplicationEventMulticaster asyncEventMulticaster) { this.asyncEventMulticaster = asyncEventMulticaster; }
public void setSyncEventMulticaster(ApplicationEventMulticaster syncEventMulticaster) { this.syncEventMulticaster = syncEventMulticaster; }
@Bean(name = "applicationEventMulticaster") public ApplicationEventMulticaster simpleApplicationEventMulticaster() { SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor()); return eventMulticaster; }
@Bean(name = AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME) public ApplicationEventMulticaster applicationEventMulticaster() { return spy(new SimpleApplicationEventMulticaster()); }
private void removeEventListener(final TestContext testContext) { final ApplicationEventMulticaster multicaster = testContext.getApplicationContext() .getBean(ApplicationEventMulticaster.class); multicaster.removeApplicationListener(eventCaptor); }
/** * @return The default application context ApplicationEventMulticaster. */ // do NOT change the name as it is important to replace the default application context event multi caster @Bean(name = "applicationEventMulticaster") public ApplicationEventMulticaster getDataPrepEventsCaster( ) { return new DataPrepEventsCaster(executor, beanFactory); }
/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { Assert.hasText(channelName, "channelName is required"); Assert.hasText(channelVersion, "channelVersion is required"); localChannel = new Channel(); localChannel.setName(channelName); localChannel.setVersion(channelVersion); // Retrieve host address try { localChannel.setHostAddress(InetAddress.getLocalHost().getHostAddress()); } catch (UnknownHostException e) { throw new Exception("Unable to resolve localhost address", e); } DefaultListableBeanFactory autowireCapableBeanFactory = (DefaultListableBeanFactory) this.applicationContext .getAutowireCapableBeanFactory(); ApplicationEventMulticaster eventMulticaster = this.applicationContext.getBean( AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class); Assert.notNull(eventMulticaster, "Spring multicaster is required"); if (multicastProvider == null) { Assert.hasText(clusterName, "clusterName is required"); JGroupsMulticastProvider provider = new JGroupsMulticastProvider(); provider.setClusterName(getClusterName()); provider.setStartOnContext(false); autowireCapableBeanFactory.autowireBeanProperties(provider, AutowireCapableBeanFactory.AUTOWIRE_NO, false); autowireCapableBeanFactory.initializeBean(provider, "jGroupsMulticastProvider"); this.multicastProvider = provider; eventMulticaster.addApplicationListener(provider); } if (multicastProvider.isStartOnContext()) { throw new RuntimeException( "multicastProvider hasn't to start automatically, set 'startOnContext' property to false"); } multicast = new Multicast(); multicast.setProvider(multicastProvider); autowireCapableBeanFactory.autowireBeanProperties(multicast, AutowireCapableBeanFactory.AUTOWIRE_NO, false); autowireCapableBeanFactory.initializeBean(multicast, "multicast"); eventMulticaster.addApplicationListener(this.multicast); }