【问题标题】:Supplying call latency as a IntStream以 IntStream 的形式提供呼叫延迟
【发布时间】:2014-07-04 13:20:47
【问题描述】:

我正在尝试使用 Java 8 和流,而我想要替换的一件事是我们拥有的系统

  • 使用方面来测量调用延迟(每个配置时间段)到网络服务,然后
  • 将这些结果输入复杂事件处理器 (esper),以便
  • 我们可以发送警报通知

所以,一次一步。第一步,我需要生成一个流(我认为),它允许我将这些延迟数字提供给现有的侦听器。了解这一点,获得下一个系列号码可能必须等到有电话。

我该怎么做?这是 cmets 的延迟方面。

public class ProfilingAspect {

    private ProfilingAction action;

    public ProfilingAspect(ProfilingAction action) {
        this.action = action;
    }

    public Object doAroundAdvice(ProceedingJoinPoint jp) throws Throwable{
        long startTime = System.currentTimeMillis();

        Object retVal = null;
        Throwable error = null;
        try{
            retVal = jp.proceed();
        }catch (Throwable t){
            error = t;
        }

        Class withinType = jp.getSourceLocation().getWithinType();
        String methodName = jp.getSignature().getName();

        long endTime = System.currentTimeMillis();
        long runningTime = endTime - startTime;
        // Let the IntStream know we have a new latency. Or really, we have an object 
        // stream with all this extra data
        action.perform(withinType, methodName, jp.getArgs(), runningTime, error);

        if( error != null ){
            throw error;
        }

        return retVal;
    }
}

【问题讨论】:

    标签: java java-8 java-stream


    【解决方案1】:

    好的,我有一个工作示例。它不能处理我必须缓冲结果的情况,尽管流的读取速度不够快。我愿意接受一些改进

    public class LatencySupplier implements Supplier<SomeFancyObject> {
    
        private Random r = new Random();
    
        @Override
        public SomeFancyObject get() {
            try {
                Thread.sleep(100 + r.nextInt(1000));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
    
            return new SomeFancyObject(10 + r.nextInt(1000));
        }
    }
    
    public class SomeFancyObject {
    
        private static String[] someGroups = {"Group1","Group2","Group3"};
        private final String group;
    
        private int value;
    
        public SomeFancyObject(int value) {
            this.value = value;
            this.group = WSRandom.selectOne(someGroups);
        }
    
        public String getGroup() {
            return group;
        }
    
        public int getValue() {
            return value;
        }
    
        @Override
        public String toString() {
            return value + "";
        }
    }
    

    我的下一步是按时间创建一个流,这样我就可以做 avg/5 min 等。

    public class Sample {
    
        public static void main(String[] args) throws InterruptedException {
    
    
            Stream<SomeFancyObject> latencyStream = Stream.generate(new LatencySupplier());
            Map<Object,List<SomeFancyObject>> collect = latencyStream.limit(10).collect(Collectors.groupingBy(sfo -> sfo.getGroup()));
            System.out.println(collect);
            Object o = new Object();
            synchronized (o){
                o.wait();
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多