-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from dromara/optimize
- Loading branch information
Showing
7 changed files
with
184 additions
and
141 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
common/src/main/java/org/dromara/dynamictp/common/spring/SpringBeanHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.dromara.dynamictp.common.spring; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.collections4.MapUtils; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.springframework.beans.factory.ListableBeanFactory; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* SpringBeanHelper related | ||
* | ||
* @author yanhom | ||
* @since 1.0.4 | ||
**/ | ||
@Slf4j | ||
public final class SpringBeanHelper { | ||
|
||
private SpringBeanHelper() { } | ||
|
||
public static void register(BeanDefinitionRegistry registry, | ||
String beanName, | ||
Class<?> clazz, | ||
Map<String, Object> propertyValues, | ||
Object... constructorArgs) { | ||
register(registry, beanName, clazz, propertyValues, null, constructorArgs); | ||
} | ||
|
||
public static void register(BeanDefinitionRegistry registry, | ||
String beanName, | ||
Class<?> clazz, | ||
Map<String, Object> propertyValues, | ||
List<String> dependsOnBeanNames, | ||
Object... constructorArgs) { | ||
if (ifPresent(registry, beanName, clazz) || registry.containsBeanDefinition(beanName)) { | ||
log.info("DynamicTp registrar, bean [{}] already exists and will be overwritten", beanName); | ||
registry.removeBeanDefinition(beanName); | ||
} | ||
doRegister(registry, beanName, clazz, propertyValues, dependsOnBeanNames, constructorArgs); | ||
} | ||
|
||
public static void registerIfAbsent(BeanDefinitionRegistry registry, | ||
String beanName, | ||
Class<?> clazz, | ||
Object... constructorArgs) { | ||
registerIfAbsent(registry, beanName, clazz, null, null, constructorArgs); | ||
} | ||
|
||
public static void registerIfAbsent(BeanDefinitionRegistry registry, | ||
String beanName, | ||
Class<?> clazz, | ||
Map<String, Object> propertyValues, | ||
Object... constructorArgs) { | ||
registerIfAbsent(registry, beanName, clazz, propertyValues, null, constructorArgs); | ||
} | ||
|
||
public static void registerIfAbsent(BeanDefinitionRegistry registry, | ||
String beanName, | ||
Class<?> clazz, | ||
Map<String, Object> propertyValues, | ||
List<String> dependsOnBeanNames, | ||
Object... constructorArgs) { | ||
if (!ifPresent(registry, beanName, clazz) && !registry.containsBeanDefinition(beanName)) { | ||
doRegister(registry, beanName, clazz, propertyValues, dependsOnBeanNames, constructorArgs); | ||
} | ||
} | ||
|
||
public static boolean ifPresent(BeanDefinitionRegistry registry, String beanName, Class<?> clazz) { | ||
String[] beanNames = getBeanNames((ListableBeanFactory) registry, clazz); | ||
return ArrayUtils.contains(beanNames, beanName); | ||
} | ||
|
||
public static String[] getBeanNames(ListableBeanFactory beanFactory, Class<?> clazz) { | ||
return beanFactory.getBeanNamesForType(clazz, true, false); | ||
} | ||
|
||
private static void doRegister(BeanDefinitionRegistry registry, | ||
String beanName, | ||
Class<?> clazz, | ||
Map<String, Object> propertyValues, | ||
List<String> dependsOnBeanNames, | ||
Object... constructorArgs) { | ||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz); | ||
for (Object constructorArg : constructorArgs) { | ||
builder.addConstructorArgValue(constructorArg); | ||
} | ||
if (MapUtils.isNotEmpty(propertyValues)) { | ||
propertyValues.forEach(builder::addPropertyValue); | ||
} | ||
if (CollectionUtils.isNotEmpty(dependsOnBeanNames)) { | ||
dependsOnBeanNames.forEach(builder::addDependsOn); | ||
} | ||
registry.registerBeanDefinition(beanName, builder.getBeanDefinition()); | ||
} | ||
} |
76 changes: 0 additions & 76 deletions
76
common/src/main/java/org/dromara/dynamictp/common/util/BeanUtil.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
core/src/main/java/org/dromara/dynamictp/core/spring/DtpBaseProcessorRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.dromara.dynamictp.core.spring; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.dromara.dynamictp.common.spring.ApplicationContextHolder; | ||
import org.dromara.dynamictp.common.spring.SpringBeanHelper; | ||
import org.dromara.dynamictp.common.timer.HashedWheelTimer; | ||
import org.dromara.dynamictp.core.thread.NamedThreadFactory; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* DtpBaseProcessorRegistrar related | ||
* | ||
* @author yanhom | ||
* @since 1.0.4 | ||
**/ | ||
public class DtpBaseProcessorRegistrar implements ImportBeanDefinitionRegistrar { | ||
|
||
private static final String APPLICATION_CONTEXT_HOLDER = "dtpApplicationContextHolder"; | ||
|
||
private static final String HASHED_WHEEL_TIMER = "dtpHashedWheelTimer"; | ||
|
||
private static final String DTP_POST_PROCESSOR = "dtpPostProcessor"; | ||
|
||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | ||
registerHashedWheelTimer(registry); | ||
SpringBeanHelper.registerIfAbsent(registry, APPLICATION_CONTEXT_HOLDER, ApplicationContextHolder.class); | ||
|
||
// ApplicationContextHolder and HashedWheelTimer are required in DtpExecutor execute method, so they must be registered first | ||
SpringBeanHelper.registerIfAbsent(registry, DTP_POST_PROCESSOR, DtpPostProcessor.class, | ||
null, Lists.newArrayList(APPLICATION_CONTEXT_HOLDER, HASHED_WHEEL_TIMER)); | ||
} | ||
|
||
private void registerHashedWheelTimer( BeanDefinitionRegistry registry) { | ||
Object[] constructorArgs = new Object[] { | ||
new NamedThreadFactory("dtp-runnable-timeout", true), | ||
10, | ||
TimeUnit.MILLISECONDS | ||
}; | ||
SpringBeanHelper.registerIfAbsent(registry, HASHED_WHEEL_TIMER, HashedWheelTimer.class, constructorArgs); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
core/src/main/java/org/dromara/dynamictp/core/spring/DtpPostProcessorRegistrar.java
This file was deleted.
Oops, something went wrong.