Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems using TransmittableThreadLocal in NameMapper #5727

Open
yixiaco opened this issue Mar 27, 2024 · 1 comment
Open

Problems using TransmittableThreadLocal in NameMapper #5727

yixiaco opened this issue Mar 27, 2024 · 1 comment
Labels

Comments

@yixiaco
Copy link

yixiaco commented Mar 27, 2024

NameMapper cannot get the value in InheritableThreadLocal. Even if I use TransmittableThreadLocal and set setExecutor and setNettyExecutor, I cannot get the variable in the child thread in unmap.

public class KeyPrefixHandler implements NameMapper {
  public String map(String name) { return name; }
  public String unmap(String name) {
    // Neither in the main thread nor in the child thread
    System.out.println(TEMP.get()); // null
    return name;
  }
}

@AutoConfiguration
public class RedisConfig {
  @Bean
  public RedissonAutoConfigurationCustomizer redissonCustomizer(RedissonProperties redissonProperties) {
    return config -> {
      config.useSingleServer().setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix()))
      int threads = Runtime.getRuntime().availableProcessors() * 2;
      if (config.getThreads() != 0) {
          threads = config.getThreads();
      }
      config.setExecutor(TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(threads, new DefaultThreadFactory("redisson"))));
      config.setNettyExecutor(TtlExecutors.getTtlExecutor(new ThreadPerTaskExecutor(new DefaultThreadFactory("redisson-netty"))));
      // ...
    }
  }
}

public class Test {
  private static final ThreadLocal<Boolean> TEMP = new TransmittableThreadLocal<>();

  public static void main(String[] args) {
    TEMP.set(true);
    Stream<String> stream = redissonClient.getKeys().getKeysStreamByPattern("test:*");
    stream.collect(Collectors.toList());
    TEMP.remove();
  }
}
@yixiaco
Copy link
Author

yixiaco commented Apr 27, 2024

@mrniko Can you help me with this question and see if there is any progress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant