• 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();
            }
        }
    }
  • 相关阅读:
    oracle数据段详解
    监听静态注册与动态注册
    Oracle网络相关概念与常用配置文件
    pycharm社区版安装及遇到的问题
    强化学习-K摇臂赌博机
    概率图模型
    半监督学习
    卷积神经网络
    递归神经网络
    玻尔兹曼机及其相关模型
  • 原文地址:https://www.cnblogs.com/liangblog/p/12896871.html
Copyright © 2020-2023  润新知