zoukankan      html  css  js  c++  java
  • apollo更新bean的属性

    public class FileRefTest {
    
    
        static class Lbw{
            private String name;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
        }
        @Test
        public void test() throws NoSuchFieldException, IllegalAccessException, InterruptedException, InstantiationException {
    //        Lbw lbw = new Lbw();
    //        Class<? extends Lbw> aClass = lbw.getClass();
    //        Field filed = aClass.getDeclaredField("name");
    //
    //        aClass.getFields();
    //        lbw.setName("test123");
    
    
            ScheduledExecutorService scheduleExecutor = Executors.newScheduledThreadPool(2);
            Class<?> clazz = Lbw.class;
    
            Field field = null;
            field = clazz.getDeclaredField("name");
            field.setAccessible(true);
    
            Lbw bean = (Lbw) clazz.newInstance();
            field.set(bean, "newVal");
    
            Field finalField = field;
            scheduleExecutor.scheduleAtFixedRate(()->{
                String mySourceFileName = "f://test.txt";
                byte[] bytes = new byte[1024];
                ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
                boolean accessible = finalField.isAccessible();
                finalField.setAccessible(true);
                finalField.setAccessible(accessible);
                
                try (FileChannel inputChannel = new FileInputStream(new File(mySourceFileName)).getChannel();
                ){
    
                    while (inputChannel.read(byteBuffer) > 0) {
                        byteBuffer.flip();
                        String str = new String(bytes, "utf-8");
                        System.out.println(str);
                        byteBuffer.clear(); // 清空buffer
                        finalField.set(bean, str);
                        System.out.println("bean.getName()---"+bean.getName());
                    }
    
                } catch (Exception e) {
    
                }
                System.out.println("======");
            }, 2, 2, TimeUnit.SECONDS);
            CountDownLatch countDownLatch = new CountDownLatch(1);
            countDownLatch.await();
        }
    
    
    }
    
    
  • 相关阅读:
    linux删除目录的命令
    Windows XP下git通过代理下载android代码
    白话算法希尔排序
    操作系统——存储技术
    如何理解Linus Torvalds的“什么才是优秀程序员”的话
    程序员自我修养读书随笔——目标文件
    面试求职:大数据处理总结
    持久化与Session定义
    java中byte转换int时为何与0xff进行与运算
    OSI七层相关协议
  • 原文地址:https://www.cnblogs.com/kltsee/p/15513628.html
Copyright © 2011-2022 走看看