com.alibaba.nacos.api.config.annotation.NacosConfigListener Java Examples
The following examples show how to use
com.alibaba.nacos.api.config.annotation.NacosConfigListener.
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 File: NacosConfigListenerMethodProcessor.java From nacos-spring-project with Apache License 2.0 | 6 votes |
private NacosConfigConverter determineNacosConfigConverter(Class<?> targetType, NacosConfigListener listener, String type) { Class<?> converterClass = listener.converter(); NacosConfigConverter configConverter = null; // Use default implementation if (NacosConfigConverter.class.equals(converterClass)) { configConverter = new DefaultNacosConfigConverter(targetType, conversionService, type); } else { // Use customized implementation configConverter = (NacosConfigConverter) instantiateClass(converterClass); } return configConverter; }
Example #2
Source File: NacosConfigEndpoint.java From nacos-spring-boot-project with Apache License 2.0 | 5 votes |
@Override public void onApplicationEvent(NacosConfigMetadataEvent event) { String key = buildMetadataKey(event); if (StringUtils.isNotEmpty(key) && !nacosConfigMetadataMap.containsKey(key)) { JSONObject jsonObject = new JSONObject(); jsonObject.put("groupId", event.getGroupId()); jsonObject.put("dataId", event.getDataId()); if (ClassUtils.isAssignable(event.getSource().getClass(), AnnotationMetadata.class)) { jsonObject.put("origin", "NacosPropertySource"); jsonObject.put("target", ((AnnotationMetadata) event.getSource()).getClassName()); } else if (ClassUtils.isAssignable(event.getSource().getClass(), NacosConfigListener.class)) { jsonObject.put("origin", "NacosConfigListener"); Method configListenerMethod = (Method) event.getAnnotatedElement(); jsonObject.put("target", configListenerMethod.getDeclaringClass().getName() + ":" + configListenerMethod.toString()); } else if (ClassUtils.isAssignable(event.getSource().getClass(), NacosConfigurationProperties.class)) { jsonObject.put("origin", "NacosConfigurationProperties"); jsonObject.put("target", event.getBeanType().getName()); } else if (ClassUtils.isAssignable(event.getSource().getClass(), Element.class)) { jsonObject.put("origin", "NacosPropertySource"); jsonObject.put("target", event.getXmlResource().toString()); } else { throw new RuntimeException("unknown NacosConfigMetadataEvent"); } nacosConfigMetadataMap.put(key, jsonObject); } }
Example #3
Source File: PrintLogger.java From nacos-spring-boot-project with Apache License 2.0 | 5 votes |
@NacosConfigListener(dataId = "${nacos.example.listener.data-id}", timeout = 5000) public void onChange(String newLog) throws Exception { Properties properties = new DefaultPropertiesConfigParse().parse(newLog); for (Object t : properties.keySet()) { String key = String.valueOf(t); if (key.startsWith(LOGGER_TAG)) { String strLevel = properties.getProperty(key, "info"); LogLevel level = LogLevel.valueOf(strLevel.toUpperCase()); loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level); logger.info("{}:{}", key, strLevel); } } }
Example #4
Source File: NacosConfigListenerMethodProcessor.java From nacos-spring-project with Apache License 2.0 | 5 votes |
private void publishMetadataEvent(String beanName, Object bean, Class<?> beanClass, String dataId, String groupId, NacosConfigListener listener, Method method) { NacosProperties nacosProperties = listener.properties(); Properties resolvedNacosProperties = configServiceBeanBuilder .resolveProperties(nacosProperties); NacosConfigMetadataEvent metadataEvent = new NacosConfigMetadataEvent(listener); // Nacos Metadata metadataEvent.setDataId(dataId); metadataEvent.setGroupId(groupId); Map<String, Object> nacosPropertiesAttributes = getAnnotationAttributes( nacosProperties); metadataEvent.setNacosPropertiesAttributes(nacosPropertiesAttributes); metadataEvent.setNacosProperties(resolvedNacosProperties); // Bean Metadata metadataEvent.setBeanName(beanName); metadataEvent.setBean(bean); metadataEvent.setBeanType(beanClass); metadataEvent.setAnnotatedElement(method); // Publish event applicationEventPublisher.publishEvent(metadataEvent); }
Example #5
Source File: NacosConfigListenerMethodProcessor.java From nacos-spring-project with Apache License 2.0 | 5 votes |
@Override protected boolean isCandidateMethod(Object bean, Class<?> beanClass, NacosConfigListener listener, Method method, ApplicationContext applicationContext) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != 1) { // Only one argument on method if (logger.isWarnEnabled()) { logger.warn("Listener method [" + method + "] parameters' count must be one !"); } return false; } Class<?> targetType = parameterTypes[0]; NacosConfigConverter configConverter = determineNacosConfigConverter(targetType, listener, listener.type().getType()); if (!configConverter.canConvert(targetType)) { if (logger.isWarnEnabled()) { logger.warn("Listener method [" + method + "] is not a candidate , thus its parameter type [" + targetType + "] can't be converted , please check NacosConfigConverter implementation : " + configConverter.getClass().getName()); } } return true; }
Example #6
Source File: NacosConfigListenerMethodProcessorTest.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = DATA_ID) public void onMessage(String config) { assertEquals("9527", config); // asserts true }
Example #7
Source File: TestConfiguration.java From nacos-spring-boot-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = "listener.test", timeout = 500) public void onChange(String newContent) throws Exception { System.out.println("onChange : " + newContent); }
Example #8
Source File: Listeners.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = DATA_ID, timeout = 200) public void onDouble(Double value) throws Exception { Thread.sleep(100); // normal execution this.doubleValue = value; }
Example #9
Source File: Listeners.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = DATA_ID, timeout = 50) public void onInteger(Integer value) throws Exception { Thread.sleep(100); // timeout of execution this.integerValue = value; }
Example #10
Source File: NacosConfigListenerTest.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = "convert_map.yaml", timeout = 2000L) public void onMessageYaml(Map config) { System.out.println("onMessage: " + config); receiveThree = true; }
Example #11
Source File: NacosConfigListenerTest.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = "convert_map.properties", timeout = 2000L) public void onMessage(Map config) { System.out.println("onMessage: " + config); receiveTwo = true; }
Example #12
Source File: NacosConfigListenerTest.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = "com.alibaba.nacos.example.properties", timeout = 2000L) public void onMessage(String config) { System.out.println("onMessage: " + config); receiveOne = true; content = config; }
Example #13
Source File: NacosConfigListenerMethodProcessorTest.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = "user", converter = UserNacosConfigConverter.class) public void onUser(User user) { assertEquals(Long.valueOf(1L), user.getId()); assertEquals("mercyblitz", user.getName()); received = true; }
Example #14
Source File: NacosConfigListenerMethodProcessorTest.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = DATA_ID) public void onInt(int value) { assertEquals(9527, value); // asserts true }
Example #15
Source File: NacosConfigListenerMethodProcessorTest.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = DATA_ID) public void onInteger(Integer value) { assertEquals(Integer.valueOf(9527), value); // asserts true }
Example #16
Source File: ConsumerBootstrap.java From tech-weekly with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = "nacos-consumer.properties") public void onChange(Properties properties){ System.out.println("onChange(Properties) : "+properties); }
Example #17
Source File: NacosConfigListenerMethodProcessor.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@Override protected void processListenerMethod(String beanName, final Object bean, Class<?> beanClass, final NacosConfigListener listener, final Method method, ApplicationContext applicationContext) { final String dataId = NacosUtils.readFromEnvironment(listener.dataId(), environment); final String groupId = NacosUtils.readFromEnvironment(listener.groupId(), environment); final String type = StringUtils.isEmpty(NacosUtils.readTypeFromDataId(dataId)) ? listener.type().getType() : NacosUtils.readTypeFromDataId(dataId); long timeout = listener.timeout(); Assert.isTrue(StringUtils.hasText(dataId), "dataId must have content"); Assert.isTrue(StringUtils.hasText(groupId), "groupId must have content"); Assert.isTrue(timeout > 0, "timeout must be greater than zero"); ConfigService configService = configServiceBeanBuilder .build(listener.properties()); try { configService.addListener(dataId, groupId, new TimeoutNacosConfigListener(dataId, groupId, timeout) { @Override protected void onReceived(String config) { Class<?> targetType = method.getParameterTypes()[0]; NacosConfigConverter configConverter = determineNacosConfigConverter( targetType, listener, type); Object parameterValue = configConverter.convert(config); // Execute target method ReflectionUtils.invokeMethod(method, bean, parameterValue); } }); } catch (NacosException e) { logger.error("ConfigService can't add Listener for dataId : " + dataId + " , groupId : " + groupId, e); } publishMetadataEvent(beanName, bean, beanClass, dataId, groupId, listener, method); }
Example #18
Source File: TimeoutNacosConfigListener.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = CURRENT_TIME_DATA_ID, timeout = 100) public void onReceivedWithoutTimeout(String value) throws InterruptedException { Thread.sleep(50); logger.info("onReceivedWithoutTimeout(String) : {}", value); }
Example #19
Source File: TimeoutNacosConfigListener.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = CURRENT_TIME_DATA_ID, timeout = 100) public void onReceivedWithTimeout(String value) throws InterruptedException { Thread.sleep(200); logger.info("onReceivedWithTimeout(String) : {}", value); // Never executes }
Example #20
Source File: PojoNacosConfigListener.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = CURRENT_TIME_DATA_ID, converter = PojoNacosConfigConverter.class) public void onReceived(Pojo value) throws InterruptedException { logger.info("onReceived(Pojo) : {}", value); }
Example #21
Source File: SimpleNacosConfigListener.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = PROPERTIES_DATA_ID) public void onReceived(Properties value) { logger.info("onReceived(Properties) : {}", value); }
Example #22
Source File: SimpleNacosConfigListener.java From nacos-spring-project with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = PROPERTIES_DATA_ID) public void onReceived(String value) { logger.info("onReceived(String) : {}", value); }
Example #23
Source File: ConsumerBootstrap.java From tech-weekly with Apache License 2.0 | 4 votes |
@NacosConfigListener(dataId = "nacos-consumer.properties") public void onChange(String properties){ System.out.println("onChange(String) : "+properties); }