zoukankan      html  css  js  c++  java
  • JAVA一个接口多个实现逐个调用

    经测试确认,当一个接口有多个实现时,调用时只会执行一个

    有时候需要多个实现调用,方法示例如下:

    public interface TransCallbackInterface {
        
        public void callback(String taskId, int code, String fixed);
        
    }
    @Component
    public class TransCallbackDy implements  InitializingBean,TransCallbackInterface{
    
        @Override
        public void callback(String taskId, int code, String fixed) {
            System.out.println("TransCallback");
        }

    @Override
    public void afterPropertiesSet() throws Exception {
    // TODO Auto-generated method stub
    System.out.println("callback registerCallbackProcessor .");

    
    
    FileTransferShedule.registerCallbackProcessor(this);
    
    

    }

    }
    @Component
    public class TransCallbackDy implements  InitializingBean, TransCallbackInterface{
    
        @Override
        public void callback(String taskId, int code, String fixedInfo) {
            System.out.println("TransCallback");
        }

    @Override
    public void afterPropertiesSet() throws Exception {
    // TODO Auto-generated method stub
    System.out.println("callback registerCallbackProcessor .");

    FileTransferShedule.registerCallbackProcessor(this);

    }

    
    }

    调用方式:

    @Component
    public class FileTransferShedule implements InitializingBean, DisposableBean {
    
    @Override
        public void afterPropertiesSet() throws Exception {
            
        }
    
    @Override
        public void destroy() throws Exception {
            logger.debug("service closed");
    
        }
    
    
        private static List<TransCallbackInterface> processors = new ArrayList<TransCallbackInterface>();
        
        public static void registerCallbackProcessor(
                TransCallbackInterface processor) {
            synchronized (processors) {
                processors.add(processor);
            }
        }
    
        public static void unregisterCallbackProcessor(
                TransCallbackInterface processor) {
            synchronized (processors) {
                processors.remove(processor);
            }
        }
    
    public void callback(HttpServletRequest request)  {
        
            logger.debug("回调接口测试");
            try {
                Throwable t = null;
                synchronized (processors) {
                    for (TestCallbackInterface processor : processors) {
                        try {
                            processor.callback();
                        } catch (Throwable e) {
                            t = e;
                        }
                    }
                }
                if (t != null) {
                    throw new IOException(t);
                }
                System.out.println("test");
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    echarts学习:简单柱状图和折线图
    pymongo的正则查询
    nginx学习:配置文件及其组成
    nginx实战:flaks + uwgsi + nginx部署
    nginx:学习三
    celery使用实例
    luoguP2742 二维凸包 / 圈奶牛Fencing the Cows
    AtCoder Grand Contest 025 Problem D
    luoguP3960 [noip2017]列队(树状数组)
    bzoj3223: Tyvj 1729 文艺平衡树(splay翻转操作)
  • 原文地址:https://www.cnblogs.com/liangblog/p/12896871.html
Copyright © 2011-2022 走看看