Skip to content

Commit

Permalink
Merge pull request #235 from trydofor/develop
Browse files Browse the repository at this point in the history
3.2.120 previewed
  • Loading branch information
trydofor committed May 14, 2024
2 parents 116d30f + 3e98ab8 commit aeb2021
Show file tree
Hide file tree
Showing 50 changed files with 1,151 additions and 310 deletions.
4 changes: 4 additions & 0 deletions WingsBoot.t.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Use `t.md` as local [Test Management](https://www.jetbrains.com/help/idea/test-m
* 11028 WingsEnabledFalseTest: disable config and bean
* 11029 WingsEnabledTopFalseTest: disable top config
* 11030 TtlMDCAdapterTest: ttl MDC in multiple thread
* 11031 WingsReorderDefaultTest: default Order/Ordered
* 11032 WingsReorderEnableTest: configed Order/Ordered

## 12 Faceless

Expand Down Expand Up @@ -309,6 +311,8 @@ Use `t.md` as local [Test Management](https://www.jetbrains.com/help/idea/test-m
* 13118 EventPublishHelperTest: async global AttributeRidEvent
* 13119 WingsCacheInterceptorTest: evict multiple cache keys
* 13120 AsyncControllerTest: Future request mapping with async service
* 13121 BootAdminServerTest: servlet mapping order
* 13122 AsyncHelperTest: async ttl decorator

## 14 Warlock

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<url>https://wings.fessional.pro</url>

<properties>
<!-- https://maven.apache.org/maven-ci-friendly.html -->
<revision>3.2.111-SNAPSHOT</revision>
<!-- (#.#).(##)(#) (SPRING-MINOR).(MINOR)(PATCH) https://maven.apache.org/maven-ci-friendly.html -->
<revision>3.2.120-SNAPSHOT</revision>
<!-- https://docs.spring.io/spring-boot/docs/current/reference/html/dependency-versions.html -->
<spring-boot.version>3.2.2</spring-boot.version>
<!-- -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.scheduling.support.SimpleTriggerContext;
Expand Down Expand Up @@ -107,7 +106,8 @@ public boolean force(long id) {
}

final boolean fast = BoxedCastUtil.orTrue(td.getTaskerFast());
TaskSchedulerHelper.referScheduler(fast).schedule(() -> {
final var scheduler = fast ? TaskSchedulerHelper.Fast() : TaskSchedulerHelper.Scheduled();
scheduler.schedule(() -> {
long execTms = ThreadNow.millis();
long doneTms = -1;
long failTms = -1;
Expand Down Expand Up @@ -213,7 +213,7 @@ private boolean relaunch(long id) {
saveNextExec(next, td);

final boolean fast = BoxedCastUtil.orTrue(td.getTaskerFast());
final ThreadPoolTaskScheduler taskScheduler = TaskSchedulerHelper.referScheduler(fast);
final var taskScheduler = fast ? TaskSchedulerHelper.Fast() : TaskSchedulerHelper.Scheduled();

if (taskScheduler.getScheduledExecutor().isShutdown()) {
log.error("TaskScheduler={} is shutdown, name={} id={}", fast, td.getTaskerName(), td.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class TinyTaskServiceImpl implements TinyTaskService {
@Override
@NotNull
public ThreadPoolTaskScheduler referScheduler(boolean fast) {
return TaskSchedulerHelper.referScheduler(fast);
return fast ? TaskSchedulerHelper.Fast() : TaskSchedulerHelper.Scheduled();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void cancelSchedule() {
al.rule(".. cancel", event -> event.getFormattedMessage().contains(".. cancel=true"));
al.start();

final ThreadPoolTaskScheduler scheduler = TaskSchedulerHelper.referScheduler(false);
final ThreadPoolTaskScheduler scheduler = TaskSchedulerHelper.Scheduled();
final ScheduledFuture<?> f1 = scheduler.schedule(() -> log.info("-1 run={}", System.currentTimeMillis()),
Instant.ofEpochMilli(System.currentTimeMillis() - 1000));
Sleep.ignoreInterrupt(500);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pro.fessional.wings.silencer.enhance;

/**
* <pre>
* &#64;Setter(onMethod_ = {&#64;Autowired, &#64;Lazy})
* protected RuntimeConfServiceImpl thisLazy = this;
* </pre>
*
* @author trydofor
* @since 2024-05-10
*/
@SuppressWarnings("unchecked")
public class ThisLazy<T> implements ThisLazyAware<T> {

protected T thisLazy = (T) this;

@Override
public void setThisLazy(T self) {
this.thisLazy = self;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pro.fessional.wings.silencer.enhance;

/**
* <pre>
* &#64;Setter(onMethod_ = {&#64;Autowired, &#64;Lazy})
* protected RuntimeConfServiceImpl thisLazy = this;
* </pre>
*
* @author trydofor
* @since 2024-05-10
*/
public interface ThisLazyAware<T> {

/**
* inject enhanced this before Bean Initialization
*
* @param thisLazy enhanced bean
*/
void setThisLazy(T thisLazy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import pro.fessional.wings.silencer.enhance.ThisLazyAware;
import pro.fessional.wings.silencer.message.MessageSourceHelper;
import pro.fessional.wings.silencer.runner.ApplicationInspectRunner;
import pro.fessional.wings.silencer.runner.ApplicationRunnerOrdered;
import pro.fessional.wings.silencer.spring.boot.ConditionalWingsEnabled;
import pro.fessional.wings.silencer.spring.boot.WingsReorderProcessor;
import pro.fessional.wings.silencer.spring.prop.SilencerEnabledProp;

import java.util.Map;

Expand All @@ -24,11 +30,20 @@ public class SilencerConfiguration {

private static final Log log = LogFactory.getLog(SilencerConfiguration.class);

/**
* reorder beans by config
*/
@Bean
@ConditionalWingsEnabled(abs = SilencerEnabledProp.Key$beanReorder)
public static WingsReorderProcessor wingsReorderProcessor() {
log.info("Silencer spring-auto wingsReorderProcessor");
return new WingsReorderProcessor();
}

/**
* @link <a href="https://docs.spring.io/spring-boot/docs/3.0.3/reference/htmlsingle/#features.internationalization">Internationalization</a>
* @see org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration
*/

@Bean
@ConditionalWingsEnabled
public MessageSourceHelper messageSourceHelper(MessageSource messageSource) {
Expand All @@ -42,6 +57,22 @@ public MessageSourceHelper messageSourceHelper(MessageSource messageSource) {
return bean;
}

@Bean
@ConditionalWingsEnabled
@SuppressWarnings("all")
public static BeanPostProcessor thisLazyAwarePostProcessor() {
log.info("Silencer spring-auto thisLazyAwarePostProcessor");
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(@NotNull Object bean, @NotNull String beanName) throws BeansException {
if (bean instanceof ThisLazyAware self) {
self.setThisLazy(self);
}
return bean;
}
};
}

/**
* applicationRunner are executed before commandLineRunner in SpringApplication#callRunners
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package pro.fessional.wings.silencer.spring.boot;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.Environment;

import java.util.Collections;
import java.util.Map;

/**
* (&#64;Order | &#64;Priority) &gt; Ordered.getOrder
*
* @author trydofor
* @see org.springframework.core.annotation.AnnotationAwareOrderComparator
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#getBeanProvider(ResolvableType, boolean)
* @since 2024-05-11
*/
public class WingsReorderProcessor implements BeanFactoryPostProcessor, EnvironmentAware {

private static final Log log = LogFactory.getLog(WingsReorderProcessor.class);

/**
* reorder bean by beanName(one-one) or beanClass(one-many).
*/
public static final String PrefixReorder = "wings.reorder";

/**
* set bean as primary by beanName
*/
public static final String PrefixPrimary = "wings.primary";

private Environment environment;

@Override
public void setEnvironment(@NotNull Environment env) {
this.environment = env;
}

@Override
public void postProcessBeanFactory(@NotNull ConfigurableListableBeanFactory beanFactory) throws BeansException {
Map<String, Integer> reorderProp = Binder.get(environment)
.bind(PrefixReorder, Bindable.mapOf(String.class, Integer.class))
.orElseGet(Collections::emptyMap);

Map<String, Boolean> primaryProp = Binder.get(environment)
.bind(PrefixPrimary, Bindable.mapOf(String.class, Boolean.class))
.orElseGet(Collections::emptyMap);

if (reorderProp.isEmpty() && primaryProp.isEmpty()) {
log.info("WingsReorderProcessor skipped, for no properties under " + PrefixReorder + " and " + PrefixPrimary);
return;
}

for (String bn : beanFactory.getBeanDefinitionNames()) {
BeanDefinition definition = beanFactory.getBeanDefinition(bn);
Integer order = reorderProp.get(bn);
String logExt = "";
if (order == null) {
String clz = definition.getBeanClassName();
order = reorderProp.get(clz);
logExt = ", class=" + clz;
}

if (order != null) {
log.info("WingsReorderProcessor reorder bean=" + bn + ", order=" + order + logExt);
definition.setAttribute(AbstractBeanDefinition.ORDER_ATTRIBUTE, order);
}

Boolean primary = primaryProp.get(bn);
if (primary != null) {
log.info("WingsReorderProcessor reorder bean=" + bn + ", primary=" + primary);
definition.setPrimary(primary);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ public class SilencerEnabledProp {
*/
private boolean tweakStack = true;
public static final String Key$tweakStack = Key + ".tweak-stack";

/**
* Whether to enable bean reorder with `wings.reorder.*`
*
* @see #Key$beanReorder
*/
private boolean beanReorder = true;
public static final String Key$beanReorder = Key + ".bean-reorder";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ wings.enabled.silencer.scanner=false
#wings.enabled.silencer.tweak-logback=true

## Whether to tweak the CodeException stack in global or thread
#wings.enabled.silencer.tweak-stack=true
#wings.enabled.silencer.tweak-stack=true

## Whether to enable bean reorder with `wings.reorder.*`
#wings.enabled.silencer.bean-reorder=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package pro.fessional.wings.silencer.app.bean;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import pro.fessional.wings.silencer.spring.boot.ConditionalWingsEnabled;

/**
* @author trydofor
* @since 2024-05-11
*/
@ConditionalWingsEnabled
@Configuration(proxyBeanMethods = false)
public class TestReorderConfiguration {

@Bean
@ConditionalWingsEnabled
@Order(2)
public PlainClass plainClass1() {
return new PlainClass(1);
}

@Bean
@ConditionalWingsEnabled
@Order(1)
public PlainClass plainClass2() {
return new PlainClass(2);
}

@Bean
@ConditionalWingsEnabled
@Order(2)
public GetterClass getterClass1() {
return new GetterClass(1);
}

@Bean
@ConditionalWingsEnabled
@Order(1)
public GetterClass getterClass2() {
return new GetterClass(2);
}


@Bean
@ConditionalWingsEnabled
@Order(2)
public OrderedClass orderedClass1() {
return new OrderedClass(1);
}

@Bean
@ConditionalWingsEnabled
@Order(1)
public OrderedClass orderedClass2() {
return new OrderedClass(2);
}


@RequiredArgsConstructor
public static class PlainClass {
private final int order;

@Override
public String toString() {
return String.valueOf(order);
}
}

@RequiredArgsConstructor
@Getter
public static class GetterClass {
private final int order;

@Override
public String toString() {
return String.valueOf(order);
}
}

@RequiredArgsConstructor
@Getter
public static class OrderedClass implements Ordered {
private final int order;

@Override
public String toString() {
return String.valueOf(order);
}
}
}
Loading

0 comments on commit aeb2021

Please sign in to comment.