Skip to content

Commit

Permalink
BootController.login will call preLogin() and postLogin() before and …
Browse files Browse the repository at this point in the history
…after signing JWT (#290)
  • Loading branch information
SummerBootFramework authored Dec 26, 2024
1 parent fcd5f98 commit e0aed41
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 98 deletions.
66 changes: 34 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.summerboot</groupId>
<artifactId>jexpress</artifactId>
<version>2.4.17</version>
<version>2.4.18</version>
<packaging>jar</packaging>
<name>Summer Boot jExpress</name>
<description>Summer Boot jExpress focuses on solving non-functional and operational maintainability requirements,
Expand Down Expand Up @@ -58,20 +58,29 @@
</profile>
<profile>
<id>release</id>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
<maven-scm.version>2.1.0</maven-scm.version>
<maven-source.version>3.3.1</maven-source.version>
<maven-javadoc.version>3.11.2</maven-javadoc.version>
<maven-deploy.version>3.1.3</maven-deploy.version>
<maven-gpg.version>3.2.7</maven-gpg.version>
<nexus-staging-maven-plugin.version>1.7.0</nexus-staging-maven-plugin.version>
</properties>
<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<!-- <version>1.12.2</version> -->
<version>2.1.0</version>
<version>${maven-scm.version}</version>
</plugin>
<!--Source-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<version>${maven-source.version}</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -86,15 +95,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<!-- <aggregate>true</aggregate>-->
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
<!-- <additionalparam>-Xdoclint:none</additionalparam>-->
</configuration>
<version>${maven-javadoc.version}</version>
<executions>
<execution>
<phase>package</phase>
Expand All @@ -110,7 +111,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<!-- <version>2.8.2</version> -->
<version>3.1.3</version>
<version>${maven-deploy.version}</version>
<executions>
<execution>
<id>default-deploy</id>
Expand All @@ -125,7 +126,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.2.7</version>
<version>${maven-gpg.version}</version>
<executions>
<execution>
<phase>verify</phase>
Expand All @@ -140,7 +141,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.7.0</version>
<version>${nexus-staging-maven-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand Down Expand Up @@ -178,14 +179,15 @@
<properties>
<!-- common properties and dependencies -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<!-- <maven.compiler.source>21</maven.compiler.source>-->
<!-- <maven.compiler.target>21</maven.compiler.target>-->
<maven.compiler.release>17</maven.compiler.release>
<maven-clean.version>3.4.0</maven-clean.version>
<maven-compiler.version>3.13.0</maven-compiler.version>
<maven-dependency.version>3.8.0</maven-dependency.version>
<maven-dependency.version>3.8.1</maven-dependency.version>
<maven-jar.version>3.4.2</maven-jar.version>
<maven-resources.version>3.3.1</maven-resources.version>
<maven-surefire.version>3.5.1</maven-surefire.version>
<maven-surefire.version>3.5.2</maven-surefire.version>
<maven-install.version>3.1.3</maven-install.version>
<!-- Commons -->
<commons-lang3.version>3.17.0</commons-lang3.version>
Expand All @@ -194,7 +196,7 @@
<!-- <commons-text.version>1.11.0</commons-text.version>-->
<!-- <owasp.encoder.version>1.2.3</owasp.encoder.version>-->
<!-- Logging -->
<log4j-api.version>2.24.2</log4j-api.version>
<log4j-api.version>2.24.3</log4j-api.version>
<log4j-disruptor.version>4.0.0</log4j-disruptor.version>
<!-- Mail -->
<jakarta-mail.version>2.0.1</jakarta-mail.version>
Expand All @@ -204,14 +206,14 @@
<jwt.version>0.12.6</jwt.version>

<!-- NIO Netty -->
<netty.version>4.1.115.Final</netty.version>
<netty.version>4.1.116.Final</netty.version>
<netty-tcnative.version>2.0.69.Final</netty-tcnative.version>
<!-- gRPC and protobuf -->
<grpc.version>1.68.1</grpc.version>
<guava.version>33.3.1-jre</guava.version>
<protobuf.version>4.28.3</protobuf.version>
<grpc.version>1.69.0</grpc.version>
<guava.version>33.4.0-jre</guava.version>
<protobuf.version>4.29.2</protobuf.version>
<!-- Web JAX-RS -->
<swagger.core.version>2.2.26</swagger.core.version>
<swagger.core.version>2.2.27</swagger.core.version>
<!--<elastic-apm.version>1.36.0</elastic-apm.version>-->


Expand All @@ -223,17 +225,17 @@
<reflections.version>0.10.2</reflections.version>

<!-- JSON/XML/Bean Validation -->
<jackson.version>2.18.1</jackson.version>
<jackson.version>2.18.2</jackson.version>
<!-- Bean Validation -->
<jakarta.el.version>6.0.1</jakarta.el.version>
<tomcat-embed-el.version>11.0.1</tomcat-embed-el.version>
<hibernate-validator.version>8.0.1.Final</hibernate-validator.version>
<tomcat-embed-el.version>11.0.2</tomcat-embed-el.version>
<hibernate-validator.version>8.0.2.Final</hibernate-validator.version>

<!-- IOC Injection -->
<guice.version>7.0.0</guice.version>

<!-- JPA -->
<hibernate.version>6.6.3.Final</hibernate.version>
<hibernate.version>6.6.4.Final</hibernate.version>
<hikari-cp.version>6.2.1</hikari-cp.version>

<!-- Cache -->
Expand All @@ -243,7 +245,7 @@
<mqtt.version>1.2.5</mqtt.version>

<!-- Template Engine -->
<freemarker.version>2.3.33</freemarker.version>
<freemarker.version>2.3.34</freemarker.version>
<!-- Barcode -->
<zxing.version>3.5.3</zxing.version>
<!-- PDF - PDFBox
Expand Down Expand Up @@ -770,7 +772,7 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
<!-- <scope>test</scope>-->
</dependency>
</dependencies>

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/summerboot/jexpress/boot/BackOffice.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected void loadCustomizedConfigs(File cfgFile, boolean isReal, ConfigUtil he
tpeMax = CPU_CORE + 1;
}

tpe = buildThreadPoolExecutor(tpe, "BackOffice", ThreadingMode.Mixed,
tpe = buildThreadPoolExecutor(tpe, "BackOffice", tpeThreadingMode,
tpeCore, tpeMax, tpeQueue, tpeKeepAliveSeconds, new ThreadPoolExecutor.DiscardPolicy(),
prestartAllCoreThreads, allowCoreThreadTimeOut, false);

Expand Down Expand Up @@ -191,6 +191,12 @@ public Map<Integer, Integer> getBootErrorCodeMapping() {
@Config(key = "portinuse.alert.message", defaultValue = ALERT_MSG_PORT_IN_USE)
private String portInUseAlertMessage = ALERT_MSG_PORT_IN_USE;

@Config(key = "backoffice.executor.mode", defaultValue = "VirtualThread",
desc = "valid value = VirtualThread (default for Java 21+), CPU, IO and Mixed (default for old Java) \n use CPU core + 1 when application is CPU bound\n"
+ "use CPU core x 2 + 1 when application is I/O bound\n"
+ "need to find the best value based on your performance test result when nio.server.BizExecutor.mode=Mixed")
protected volatile ThreadingMode tpeThreadingMode = ThreadingMode.VirtualThread;

@Config(key = "backoffice.executor.core", defaultValue = "3",
desc = "0 = current computer/VM's available processors + 1")
private int tpeCore = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface BootConstant {
String APP_ID = String.format("%06d", new Random().nextInt(999999));

//version
String VERSION = "jExpress 2.4.17";
String VERSION = "jExpress 2.4.18";
String JEXPRESS_PACKAGE_NAME = "org.summerboot.jexpress";

String DEFAULT_ADMIN_MM = "changeit";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ public void start() {
int port = bindingAddress.getPort();
log.trace("5a. binding gRPC on {}:{}", host, port);
try (var a = Timeout.watch("starting gRPCServer at " + host + ":" + port, timeoutMs).withDesc(timeoutDesc)) {
GRPCServer gRPCServer = new GRPCServer(host, port, gRPCCfg.getKmf(), gRPCCfg.getTmf(), serverInterceptor, gRPCCfg.getTpe(), nioListener);
boolean useVirtualThread = gRPCCfg.getTpeThreadingMode().equals(GRPCServerConfig.ThreadingMode.VirtualThread);
GRPCServer gRPCServer = new GRPCServer(host, port, gRPCCfg.getKmf(), gRPCCfg.getTmf(), serverInterceptor, gRPCCfg.getTpe(), useVirtualThread, nioListener);
ServerBuilder serverBuilder = gRPCServer.getServerBuilder();
for (Class<? extends BindableService> c : gRPCBindableServiceImplClasses) {
BindableService impl = guiceInjector.getInstance(c);
Expand Down
43 changes: 29 additions & 14 deletions src/main/java/org/summerboot/jexpress/boot/config/BootConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.summerboot.jexpress.boot.config.annotation.Config;
import org.summerboot.jexpress.boot.config.annotation.ConfigHeader;
import org.summerboot.jexpress.boot.config.annotation.ImportResource;
import org.summerboot.jexpress.nio.server.AbortPolicyWithReport;
import org.summerboot.jexpress.security.SecurityUtil;
import org.summerboot.jexpress.util.ApplicationUtil;
import org.summerboot.jexpress.util.BeanUtil;
Expand Down Expand Up @@ -59,6 +60,7 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -588,22 +590,23 @@ protected static int getLength(String s) {
protected static final int CPU_CORE = Runtime.getRuntime().availableProcessors();

public enum ThreadingMode {
CPU, IO, Mixed
VirtualThread, CPU, IO, Mixed
}

public static ThreadPoolExecutor buildThreadPoolExecutor(String tpeName) {
return buildThreadPoolExecutor(null, tpeName, ThreadingMode.VirtualThread, 0, 0, Integer.MAX_VALUE, 60, null, false, true, false);
}

public static ThreadPoolExecutor buildThreadPoolExecutor(String tpeName, ThreadingMode threadingMode, int core, int max, int queue, long keepAliveSec) {
return buildThreadPoolExecutor(null, tpeName, threadingMode, core, max, queue, keepAliveSec, null, false, false, false);
}

public static ThreadPoolExecutor buildThreadPoolExecutor(ThreadPoolExecutor tpe, String tpeName, ThreadingMode threadingMode,
int core, int max, int queue, long keepAliveSec, RejectedExecutionHandler rejectedExecutionHandler,
boolean prestartAllCoreThreads, boolean allowCoreThreadTimeOut, boolean isSingleton) {
boolean useVirtualThread = false;
switch (threadingMode) {
case CPU:// use CPU core + 1 when application is CPU bound
core = CPU_CORE + 1;
max = CPU_CORE + 1;
break;
case IO:// use CPU core x 2 + 1 when application is I/O bound
core = CPU_CORE * 2 + 1;
max = CPU_CORE * 2 + 1;
break;
case Mixed:// manual config is required when it is mixed
case Mixed, VirtualThread -> {// manual config is required when it is mixed
if (core < 1) {
core = CPU_CORE * 2 + 1;
}
Expand All @@ -614,7 +617,15 @@ public static ThreadPoolExecutor buildThreadPoolExecutor(ThreadPoolExecutor tpe,
//helper.addError("BizExecutor.MaxSize should not less than BizExecutor.CoreSize");
max = core;
}
break;
}
case CPU -> {// use CPU core + 1 when application is CPU bound
core = CPU_CORE + 1;
max = CPU_CORE + 1;
}
case IO -> {// use CPU core x 2 + 1 when application is I/O bound
core = CPU_CORE * 2 + 1;
max = CPU_CORE * 2 + 1;
}
}

boolean isQueueChanged = false;
Expand All @@ -627,9 +638,13 @@ public static ThreadPoolExecutor buildThreadPoolExecutor(ThreadPoolExecutor tpe,
//backup old
ThreadPoolExecutor old = tpe;
//create new
BlockingQueue<Runnable> bq = queue > 0 ? new LinkedBlockingQueue<>(queue) : new EmptyBlockingQueue();
tpe = new ThreadPoolExecutor(core, max, keepAliveSec, TimeUnit.SECONDS, bq,
new NamedDefaultThreadFactory(tpeName), rejectedExecutionHandler);//.DiscardOldestPolicy()
ThreadFactory factory = NamedDefaultThreadFactory.build(tpeName, useVirtualThread);
BlockingQueue<Runnable> workQueue = queue > 0 ? new LinkedBlockingQueue<>(queue) : new EmptyBlockingQueue();
if (rejectedExecutionHandler == null) {
rejectedExecutionHandler = new AbortPolicyWithReport(tpeName);
}
tpe = new ThreadPoolExecutor(core, max, keepAliveSec, TimeUnit.SECONDS, workQueue,
factory, rejectedExecutionHandler);//.DiscardOldestPolicy()
// then shotdown old tpe
if (old != null) {
old.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ public class NamedDefaultThreadFactory implements ThreadFactory {

protected static final AtomicInteger poolNumber = new AtomicInteger(1);
protected final ThreadGroup group;
protected final AtomicInteger threadNumber = new AtomicInteger(1);
protected final AtomicInteger threadCounter = new AtomicInteger(1);
protected final String namePrefix;

public NamedDefaultThreadFactory(String name) {
private NamedDefaultThreadFactory(String tpeName) {
group = Thread.currentThread().getThreadGroup();
namePrefix = name + "-"
+ poolNumber.getAndIncrement()
+ "-thread-";
namePrefix = tpeName;
}

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
Thread t = new Thread(group, r, namePrefix + threadCounter.getAndIncrement(), 0);
if (t.isDaemon()) {
t.setDaemon(false);
}
Expand All @@ -28,4 +26,11 @@ public Thread newThread(Runnable r) {
}
return t;
}

public static ThreadFactory build(String tpeName, boolean useVirtualThread) {
String namePrefix = tpeName + "-"
+ poolNumber.getAndIncrement()
+ (useVirtualThread ? "-vt-" : "-pt-");
return new NamedDefaultThreadFactory(namePrefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ServerStatus extends NotificationBroadcasterSupport implements NIOS

protected static final DateTimeFormatter DTF = DateTimeFormatter.ISO_LOCAL_DATE_TIME;//DateTimeFormatter.ofPattern("yyyy-MM-dd E HH:mm:ss");

protected static final ExecutorService QPS_SERVICE = Executors.newSingleThreadExecutor(new NamedDefaultThreadFactory("ServerStatus"));
protected static final ExecutorService QPS_SERVICE = Executors.newSingleThreadExecutor(NamedDefaultThreadFactory.build("ServerStatus", true));

protected final LinkedList<BootIOStatusData> events;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
import com.google.inject.Singleton;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.summerboot.jexpress.boot.config.NamedDefaultThreadFactory;
import org.summerboot.jexpress.boot.config.BootConfig;
import org.summerboot.jexpress.integration.cache.domain.FlashSale;
import org.summerboot.jexpress.integration.smtp.PostOffice;
import org.summerboot.jexpress.integration.smtp.SMTPClientConfig;
import org.summerboot.jexpress.nio.server.AbortPolicyWithReport;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
Expand All @@ -36,7 +35,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -86,7 +84,9 @@ public class BootCache_RedisImple implements AuthTokenCache, BootCache {

protected static final Logger log = LogManager.getLogger(BootCache_RedisImple.class.getName());

protected static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1), new NamedDefaultThreadFactory("Redis"), new AbortPolicyWithReport("Cahce.BackofficeExecutor"));
protected static final ThreadPoolExecutor tpe = BootConfig.buildThreadPoolExecutor(null, "Redis", BootConfig.ThreadingMode.VirtualThread,
1, 1, 1, 60, null,
false, false, false);

protected static final RuntimeException REDIS_MASTER_NULL_EX = new RuntimeException("Redis master is null");

Expand Down
Loading

0 comments on commit e0aed41

Please sign in to comment.