Skip to content

Commit

Permalink
Merge pull request #304 from dromara/dev
Browse files Browse the repository at this point in the history
[ISSUE #303] support ttl in agent mode
  • Loading branch information
yanhom1314 committed Jul 7, 2023
2 parents 93afcd5 + 820ff83 commit dd170da
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
8 changes: 7 additions & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-flatten.version>1.3.0</maven-flatten.version>
<ttl.version>2.12.6</ttl.version>
<ttl.version>2.14.3</ttl.version>
<equator.version>1.0.4</equator.version>
</properties>

Expand Down Expand Up @@ -430,6 +430,12 @@
<version>${revision}</version>
</dependency>

<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension-ttl</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-spring-boot-starter-common</artifactId>
Expand Down
21 changes: 21 additions & 0 deletions extension/extension-ttl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dynamic-tp-extension-ttl</artifactId>

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 com.dromara.dynamictp.extension.ttl;

import com.alibaba.ttl.TtlRunnable;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.dromara.dynamictp.core.plugin.DtpInterceptor;
import org.dromara.dynamictp.core.plugin.DtpIntercepts;
import org.dromara.dynamictp.core.plugin.DtpInvocation;
import org.dromara.dynamictp.core.plugin.DtpSignature;
import org.dromara.dynamictp.core.support.task.runnable.DtpRunnable;
import org.dromara.dynamictp.core.thread.DtpExecutor;

import java.lang.reflect.InvocationTargetException;

/**
* TtlDtpInterceptor related
*
* @author yanhom
* @since 1.1.4
**/
@DtpIntercepts({
@DtpSignature(clazz = DtpExecutor.class, method = "beforeExecute", args = {Thread.class, Runnable.class}),
@DtpSignature(clazz = DtpExecutor.class, method = "afterExecute", args = {Runnable.class, Throwable.class})
})
@Slf4j
public class TtlDtpInterceptor implements DtpInterceptor {

private static final String BEFORE_EXECUTE = "beforeExecute";

private static final String AFTER_EXECUTE = "afterExecute";

@Override
public Object intercept(DtpInvocation invocation) throws InvocationTargetException, IllegalAccessException {

DtpExecutor dtpExecutor = (DtpExecutor) invocation.getTarget();
String method = invocation.getMethod().getName();
Object[] args = invocation.getArgs();
if ((BEFORE_EXECUTE.equals(method) && isDtpRunnable(args[1])) ||
(AFTER_EXECUTE.equals(method) && isDtpRunnable(args[0]))) {
return invocation.proceed();
}
if (BEFORE_EXECUTE.equals(method)) {
processBeforeExecute(dtpExecutor, args);
} else if (AFTER_EXECUTE.equals(method)) {
processAfterExecute(args);
}
return invocation.proceed();
}

private boolean isDtpRunnable(Object runnable) {
return runnable instanceof DtpRunnable;
}

private void processBeforeExecute(DtpExecutor dtpExecutor, Object[] args) {
if (!(args[1] instanceof TtlRunnable)) {
return;
}
TtlRunnable ttlRunnable = (TtlRunnable) args[1];
val innerRunnable = ttlRunnable.getRunnable();
if (innerRunnable instanceof DtpRunnable) {
DtpRunnable runnable = (DtpRunnable) innerRunnable;
runnable.cancelQueueTimeoutTask();
runnable.startRunTimeoutTask(dtpExecutor, (Thread) args[0]);
}
}

private void processAfterExecute(Object[] args) {
if (!(args[0] instanceof TtlRunnable)) {
return;
}
TtlRunnable ttlRunnable = (TtlRunnable) args[0];
val innerRunnable = ttlRunnable.getRunnable();
if (innerRunnable instanceof DtpRunnable) {
DtpRunnable runnable = (DtpRunnable) innerRunnable;
runnable.cancelRunTimeoutTask();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.dromara.dynamictp.extension.ttl.TtlDtpInterceptor
1 change: 1 addition & 0 deletions extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>extension-notify-email</module>
<module>extension-opentelemetry</module>
<module>extension-notify-yunzhijia</module>
<module>extension-ttl</module>
</modules>

<dependencies>
Expand Down

0 comments on commit dd170da

Please sign in to comment.