zoukankan      html  css  js  c++  java
  • Shiro Filter中利用Callable和Runnable的委派模式

    Callable模式

    package com.wjz.core;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.Callable;
    
    public class SubjectCallable<V> implements Callable<V> {
        
        private Callable<V> delegate;
        
        private List<V> resouces;
        
        public SubjectCallable(Callable<V> delegate) {
            this.delegate = delegate;
        }
        
        @Override
        public V call() throws Exception {
            preCall();
            V v = doCall(this.delegate);
            postCall(v);
            return v;
        }
    
        private void add(V v) {
            resouces.add(v);
        }
    
        private void postCall(V v) {
            if (v == null) {
                throw new RuntimeException();
            }
            add(v);
        }
    
        private void preCall() {
            if (resouces == null) {
                resouces = new ArrayList<V>();
            }
        }
    
        private V doCall(Callable<V> target) throws Exception {
            return target.call();
        }
    
    }

    Runnable模式

    package com.wjz.core;
    
    public class SubjectRunnable<V> implements Runnable {
        
        private Runnable delegate;
    
        @Override
        public void run() {
            doRun(this.delegate);
        }
    
        private void doRun(Runnable target) {
            target.run();
        }
    
    }

    Demo

    package com.wjz.core;
    
    import java.util.concurrent.Callable;
    
    public class DelegatingSubject {
    
        public <V> V execute(Callable<V> delegate) {
            Callable<V> associated = associateWith(delegate);
            try {
                return associated.call();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    
        private <V> Callable<V> associateWith(Callable<V> delegate) {
            return new SubjectCallable<>(delegate);
        }
    }
  • 相关阅读:
    ThreadLocal
    mysql查看和修改密码策略
    synchronized双重校验问题
    多线程下双重检查锁的问题及解决方法
    compiler explorer网站
    隐藏表格部分内容,开启宏自动显现
    powershell系列学习笔记二:变量
    强制用户启用宏
    poweshell系列学习笔记一:基础
    Cobalt Strike修改证书
  • 原文地址:https://www.cnblogs.com/BINGJJFLY/p/9377582.html
Copyright © 2011-2022 走看看