feat: 增加ttl 使用实例
This commit is contained in:
@@ -23,6 +23,15 @@
|
||||
<version>5.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>transmittable-thread-local</artifactId>
|
||||
<version>2.13.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,29 @@
|
||||
package concurrent.ttl.context;
|
||||
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
|
||||
/**
|
||||
* @author: wangyl
|
||||
* @date: 2022/8/7
|
||||
* @description: 上线文存储
|
||||
*/
|
||||
public class ContextUtil {
|
||||
static TransmittableThreadLocal<String> transmittableThreadLocal = new TransmittableThreadLocal<>();
|
||||
static ThreadLocal<String> threadLocal = new ThreadLocal<>();
|
||||
|
||||
public static String getTransmittableThreadLocal() {
|
||||
return transmittableThreadLocal.get();
|
||||
}
|
||||
|
||||
public static void setTransmittableThreadLocal(String value) {
|
||||
transmittableThreadLocal.set(value);
|
||||
}
|
||||
|
||||
public static String getThreadLocal() {
|
||||
return threadLocal.get();
|
||||
}
|
||||
|
||||
public static void setThreadLocal(String value) {
|
||||
threadLocal.set(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package concurrent.ttl.task;
|
||||
|
||||
import concurrent.ttl.context.ContextUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@Slf4j
|
||||
public class CallableTask implements Callable<String> {
|
||||
|
||||
/**
|
||||
* Computes a result, or throws an exception if unable to do so.
|
||||
*
|
||||
* @return computed result
|
||||
* @throws Exception if unable to compute a result
|
||||
*/
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
log.info("ThreadLocal 的值为{}", ContextUtil.getThreadLocal());
|
||||
log.info("TransmittableThreadLocal 的值为{}", ContextUtil.getTransmittableThreadLocal());
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package concurrent.ttl.task;
|
||||
|
||||
import concurrent.ttl.context.ContextUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class RunnableTask implements Runnable {
|
||||
|
||||
/**
|
||||
* When an object implementing interface <code>Runnable</code> is used
|
||||
* to create a thread, starting the thread causes the object's
|
||||
* <code>run</code> method to be called in that separately executing
|
||||
* thread.
|
||||
* <p>
|
||||
* The general contract of the method <code>run</code> is that it may
|
||||
* take any action whatsoever.
|
||||
*
|
||||
* @see Thread#run()
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
log.info("ThreadLocal 的值为{}", ContextUtil.getThreadLocal());
|
||||
log.info("TransmittableThreadLocal 的值为{}", ContextUtil.getTransmittableThreadLocal());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
package concurrent.ttl;
|
||||
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import com.alibaba.ttl.TtlCallable;
|
||||
import com.alibaba.ttl.TtlRunnable;
|
||||
import concurrent.ttl.context.ContextUtil;
|
||||
import concurrent.ttl.task.CallableTask;
|
||||
import concurrent.ttl.task.RunnableTask;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class TransmittableThreadLocalTest {
|
||||
|
||||
static ExecutorService executorService;
|
||||
static AtomicInteger atomicInteger;
|
||||
|
||||
@BeforeAll
|
||||
public static void before() {
|
||||
ContextUtil.setThreadLocal("wyl-01");
|
||||
ContextUtil.setTransmittableThreadLocal("wyl-02");
|
||||
executorService = ThreadUtil.newExecutor(3);
|
||||
atomicInteger = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不修改值
|
||||
*
|
||||
* @param
|
||||
* @return void
|
||||
* @Date 2022/8/7
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Test
|
||||
public void runnableTest() throws InterruptedException {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
RunnableTask runnableTask = new RunnableTask();
|
||||
executorService.execute(runnableTask);
|
||||
}
|
||||
Thread.sleep(10000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改值 线程池里值用的旧值
|
||||
*
|
||||
* @param
|
||||
* @return void
|
||||
* @Date 2022/8/7
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Test
|
||||
public void runnableChangeErrorTest() throws InterruptedException {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
RunnableTask runnableTask = new RunnableTask();
|
||||
ContextUtil.setTransmittableThreadLocal("wyl-02-" + atomicInteger.incrementAndGet());
|
||||
executorService.execute(runnableTask);
|
||||
}
|
||||
Thread.sleep(10000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改值线程池用的新值
|
||||
*
|
||||
* @param
|
||||
* @return void
|
||||
* @Date 2022/8/7
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Test
|
||||
public void runnableChangeRightTest() throws InterruptedException {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
RunnableTask runnableTask = new RunnableTask();
|
||||
ContextUtil.setTransmittableThreadLocal("wyl-02-" + atomicInteger.incrementAndGet());
|
||||
TtlRunnable ttlRunnable = TtlRunnable.get(runnableTask);
|
||||
executorService.execute(ttlRunnable);
|
||||
}
|
||||
Thread.sleep(10000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不修改值
|
||||
*
|
||||
* @param
|
||||
* @return void
|
||||
* @Date 2022/8/7
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Test
|
||||
public void callableTest() throws InterruptedException {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
CallableTask runnableTask = new CallableTask();
|
||||
executorService.submit(runnableTask);
|
||||
}
|
||||
Thread.sleep(10000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改值线程池用的旧值
|
||||
*
|
||||
* @param
|
||||
* @return void
|
||||
* @Date 2022/8/7
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Test
|
||||
public void callableChangeErrorTest() throws InterruptedException {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
CallableTask runnableTask = new CallableTask();
|
||||
ContextUtil.setTransmittableThreadLocal("wyl-02-" + atomicInteger.incrementAndGet());
|
||||
executorService.submit(runnableTask);
|
||||
}
|
||||
Thread.sleep(10000L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改值线程池里用的新值
|
||||
*
|
||||
* @param
|
||||
* @return void
|
||||
* @Date 2022/8/7
|
||||
* @Author wangyl
|
||||
*/
|
||||
@Test
|
||||
public void callableChangeRightTest() throws InterruptedException {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
CallableTask runnableTask = new CallableTask();
|
||||
ContextUtil.setTransmittableThreadLocal("wyl-02-" + atomicInteger.incrementAndGet());
|
||||
TtlCallable<String> stringTtlCallable = TtlCallable.get(runnableTask);
|
||||
executorService.submit(stringTtlCallable);
|
||||
}
|
||||
Thread.sleep(10000L);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,6 +46,14 @@
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-bom</artifactId>
|
||||
<version>5.8.5</version>
|
||||
<type>pom</type>
|
||||
<!-- 注意这里是import -->
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<module>spring-boot-xxjob</module>
|
||||
<module>spring-boot-common</module>
|
||||
<module>spring-boot-rabbitmq</module>
|
||||
<module>spring-boot-mybatis</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>spring-boot</artifactId>
|
||||
|
||||
@@ -21,10 +21,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
|
||||
-3
@@ -1,18 +1,15 @@
|
||||
package com.wyl.spring.boot.common.spel;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.wyl.spring.boot.common.spel.annotation.SpelTest;
|
||||
import com.wyl.spring.boot.common.spel.service.EvaluationContext;
|
||||
import com.wyl.spring.boot.common.spel.service.Evaluator;
|
||||
import com.wyl.spring.boot.common.spel.service.RootObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.aop.framework.AopProxyUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
Reference in New Issue
Block a user