• 巧妙的设计


    1. 判空逻辑,如果为空,抛异常,下面代码来自kafka client:

    Assert.notNull(queue, () -> "No cache found for " + txIdPrefix);

    Assert的另一个用法,来自springframework:org.springframework.boot.context.config.StandardConfigDataLocationResolver#validateConfigName

    1. private void validateConfigName(String name) {
    2. Assert.state(!name.contains("*"), () -> "Config name '" + name + "' cannot contain '*'");
    3. }

     这个是校验参数是否合法的用法,如果 !name.contains("*") 返回的是false,就会抛出异常,代码如下:

    1. public static void state(boolean expression, Supplier messageSupplier) {
    2. if (!expression) {
    3. throw new IllegalStateException(nullSafeGet(messageSupplier));
    4. }
    5. }

    2. 本地cache设计,一下代码来自kafka client:

    1. private final Map>> cache = new ConcurrentHashMap<>();
    2. protected BlockingQueue> getCache(String txIdPrefix) {
    3. if (txIdPrefix == null) {
    4. return null;
    5. }
    6. // 下面这个方法是针对每个txIdPrefix,都创建一个LinkedBlockingQueue,并缓存起来,这里第二个参数用到了Supplier函数式接口
    7. return this.cache.computeIfAbsent(txIdPrefix, txId -> new LinkedBlockingQueue<>());
    8. }

    3. kafka consumer单线程的控制逻辑CAS:

    1. /**
    2. * Acquire the light lock and ensure that the consumer hasn't been closed.
    3. * @throws IllegalStateException If the consumer has been closed
    4. */
    5. private void acquireAndEnsureOpen() {
    6. acquire();
    7. if (this.closed) {
    8. release();
    9. throw new IllegalStateException("This consumer has already been closed.");
    10. }
    11. }
    1. // currentThread holds the threadId of the current thread accessing KafkaConsumer
    2. // and is used to prevent multi-threaded access
    3. /**
    4. * Acquire the light lock protecting this consumer from multi-threaded access. Instead of blocking
    5. * when the lock is not available, however, we just throw an exception (since multi-threaded usage is not
    6. * supported).
    7. * @throws ConcurrentModificationException if another thread already has the lock
    8. */
    9. private final AtomicInteger refcount = new AtomicInteger(0);
    10. private static final long NO_CURRENT_THREAD = -1L;
    11. private final AtomicLong currentThread = new AtomicLong(NO_CURRENT_THREAD);
    12. private void acquire() {
    13. long threadId = Thread.currentThread().getId();
    14. if (threadId != currentThread.get() && !currentThread.compareAndSet(NO_CURRENT_THREAD, threadId))
    15. throw new ConcurrentModificationException("KafkaConsumer is not safe for multi-threaded access");
    16. refcount.incrementAndGet();
    17. }
    1. /**
    2. * Release the light lock protecting the consumer from multi-threaded access.
    3. */
    4. private void release() {
    5. if (refcount.decrementAndGet() == 0)
    6. currentThread.set(NO_CURRENT_THREAD);
    7. }

    解释一下以上逻辑:

    首先是acquire 方法尝试获取锁,在acquire方法里,先判断 threadId是否为 currentThread,如果不是(第一次判断的时候,currentThread = -1,所以肯定是不相等的),就尝试把 currentThread 设置为当前线程的 threadId,设置成功就不会抛异常,表示获取锁成功,否则抛异常。

    如果加锁成功,此时再来一个线程,会满足 threadId != currentThread.get(),但是后面的那个CAS的判断就不满足,因为CAS是当currentThread是期望值:NO_CURRENT_THREAD的时候,才去把currentThread设置为当前线程id,现在期望值是上一个线程的id,所以会抛异常,就阻止了当前线程执行

    restful api 路径定义

    当需要定义一个restful api 接口的时候,一般最好追寻按模块定义,比如用户的创建,一般用:

    /user/create 而非直接使用 /create,这样做有很多好处,比如springsecurity 对某一类路径做一些权限控制的时候,这样写的作用就非常好了

    • 获取HttpServletRequest里request param:
    1. public static MultiValueMap getParameters(HttpServletRequest request) {
    2. Map parameterMap = request.getParameterMap();
    3. MultiValueMap parameters = new LinkedMultiValueMap<>(parameterMap.size());
    4. parameterMap.forEach((key, values) -> {
    5. if (values.length > 0) {
    6. for (String value : values) {
    7. parameters.add(key, value);
    8. }
    9. }
    10. });
    11. return parameters;
    12. }

  • 相关阅读:
    时间管理:我需要利用好自己的时间
    2.vscode 配置python开发环境
    【784. 字母大小写全排列】
    JavaScript语法基础03
    input的一些输入限制
    智能视频已上线,提供50+AI算法
    现代图片性能优化及体验优化指南
    搭建react-typescript-webpack开发环境
    【软件工程】四、编码 & 维护
    Vue2源码学习笔记 - 19.渲染与编译—createElement 函数
  • 原文地址:https://blog.csdn.net/Maxiao1204/article/details/132841224