From 9983453b777a6e091898c415c19e22e47c6f7e77 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Thu, 12 Jan 2017 08:37:10 +0000 Subject: [PATCH] Add target caching to BindableProxyFactory - Prevent new reflection calls to channels for Sources, Sinks and Processors, cache bound target if it's found and try to use it next time as target should not change. - One test for checking caching. - Fixes #759 --- .../stream/binding/BindableProxyFactory.java | 19 ++++++-- .../stream/aggregation/AggregationTest.java | 43 ++++++++++++++++++- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindableProxyFactory.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindableProxyFactory.java index 0ba5a7315..acf6167db 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindableProxyFactory.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindableProxyFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,6 +74,8 @@ public class BindableProxyFactory implements MethodInterceptor, FactoryBean outputHolders = new HashMap<>(); + private final Map targetCache = new HashMap<>(2); + public BindableProxyFactory(Class type) { this.type = type; } @@ -81,16 +83,27 @@ public class BindableProxyFactory implements MethodInterceptor, FactoryBean factories = context.getBeansOfType(BindableProxyFactory.class); + assertThat(factories).hasSize(2); + + Map sources = context.getBeansOfType(Source.class); + assertThat(sources).hasSize(2); + for (Source source : sources.values()) { + source.output(); + } + + Map processors = context.getBeansOfType(Processor.class); + assertThat(processors).hasSize(1); + for (Processor processor : processors.values()) { + processor.input(); + processor.output(); + } + + for (BindableProxyFactory factory : factories.values()) { + Field field = ReflectionUtils.findField(BindableProxyFactory.class, "targetCache"); + ReflectionUtils.makeAccessible(field); + Map targetCache = (Map) ReflectionUtils.getField(field, factory); + if (factory.getObjectType() == Source.class) { + assertThat(targetCache).hasSize(1); + } else if (factory.getObjectType() == Processor.class) { + assertThat(targetCache).hasSize(2); + } else { + Assert.fail("Found unexpected type"); + } + } + context.close(); + } + @EnableBinding(Source.class) @EnableAutoConfiguration public static class TestSource {