From 8e5689c29825ad6986589324d8520f70557cf6ee Mon Sep 17 00:00:00 2001 From: Marius Bogoevici Date: Fri, 2 Dec 2016 18:05:53 -0500 Subject: [PATCH] Do not check if the Lifecycle argument is null Fixes #607 Signed-off-by: Marius Bogoevici Address PR comments - Expand Javadoc - Rename Lifecycle field Signed-off-by: Marius Bogoevici --- .../stream/binder/AbstractBinderTests.java | 10 +++--- .../binder/PartitionCapableBinderTests.java | 2 +- .../cloud/stream/binder/DefaultBinding.java | 35 ++++++++++++------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/spring-cloud-stream-binder-test/src/main/java/org/springframework/cloud/stream/binder/AbstractBinderTests.java b/spring-cloud-stream-binder-test/src/main/java/org/springframework/cloud/stream/binder/AbstractBinderTests.java index 8e859420f..e5a7354d4 100644 --- a/spring-cloud-stream-binder-test/src/main/java/org/springframework/cloud/stream/binder/AbstractBinderTests.java +++ b/spring-cloud-stream-binder-test/src/main/java/org/springframework/cloud/stream/binder/AbstractBinderTests.java @@ -99,19 +99,19 @@ public abstract class AbstractBinderTests foo2ProducerBinding = binder.bindProducer("foo.2", new DirectChannel(), createProducerProperties()); foo0ProducerBinding.unbind(); - assertThat(TestUtils.getPropertyValue(foo0ProducerBinding, "endpoint", Lifecycle.class).isRunning()) + assertThat(TestUtils.getPropertyValue(foo0ProducerBinding, "lifecycle", Lifecycle.class).isRunning()) .isFalse(); foo0ConsumerBinding.unbind(); foo1ProducerBinding.unbind(); - assertThat(TestUtils.getPropertyValue(foo0ConsumerBinding, "endpoint", Lifecycle.class).isRunning()) + assertThat(TestUtils.getPropertyValue(foo0ConsumerBinding, "lifecycle", Lifecycle.class).isRunning()) .isFalse(); - assertThat(TestUtils.getPropertyValue(foo1ProducerBinding, "endpoint", Lifecycle.class).isRunning()) + assertThat(TestUtils.getPropertyValue(foo1ProducerBinding, "lifecycle", Lifecycle.class).isRunning()) .isFalse(); foo1ConsumerBinding.unbind(); foo2ProducerBinding.unbind(); - assertThat(TestUtils.getPropertyValue(foo1ConsumerBinding, "endpoint", Lifecycle.class).isRunning()) + assertThat(TestUtils.getPropertyValue(foo1ConsumerBinding, "lifecycle", Lifecycle.class).isRunning()) .isFalse(); - assertThat(TestUtils.getPropertyValue(foo2ProducerBinding, "endpoint", Lifecycle.class).isRunning()) + assertThat(TestUtils.getPropertyValue(foo2ProducerBinding, "lifecycle", Lifecycle.class).isRunning()) .isFalse(); } diff --git a/spring-cloud-stream-binder-test/src/main/java/org/springframework/cloud/stream/binder/PartitionCapableBinderTests.java b/spring-cloud-stream-binder-test/src/main/java/org/springframework/cloud/stream/binder/PartitionCapableBinderTests.java index e537b2517..9e271c48f 100644 --- a/spring-cloud-stream-binder-test/src/main/java/org/springframework/cloud/stream/binder/PartitionCapableBinderTests.java +++ b/spring-cloud-stream-binder-test/src/main/java/org/springframework/cloud/stream/binder/PartitionCapableBinderTests.java @@ -339,6 +339,6 @@ public abstract class PartitionCapableBinderTests binding) { DirectFieldAccessor accessor = new DirectFieldAccessor(binding); - return (Lifecycle) accessor.getPropertyValue("endpoint"); + return (Lifecycle) accessor.getPropertyValue("lifecycle"); } } diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinding.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinding.java index f58681ef1..1d18f70e9 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinding.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinding.java @@ -16,7 +16,6 @@ package org.springframework.cloud.stream.binder; - import org.springframework.context.Lifecycle; import org.springframework.integration.support.context.NamedComponent; import org.springframework.util.Assert; @@ -38,18 +37,26 @@ public class DefaultBinding implements Binding { protected final T target; - protected final Lifecycle endpoint; + protected final Lifecycle lifecycle; - public DefaultBinding(String name, String group, T target, Lifecycle endpoint) { + /** + * Creates an instance that associates a given name, group and binding target with an + * optional {@link Lifecycle} component, which will be stopped during unbinding. + * + * @param name the name of the binding target + * @param group the group (only for input targets) + * @param target the binding target + * @param lifecycle {@link Lifecycle} that runs while the + * binding is active and will be stopped during unbinding + */ + public DefaultBinding(String name, String group, T target, Lifecycle lifecycle) { Assert.notNull(target, "target must not be null"); - Assert.notNull(endpoint, "endpoint must not be null"); this.name = name; this.group = group; this.target = target; - this.endpoint = endpoint; + this.lifecycle = lifecycle; } - public String getName() { return this.name; } @@ -58,23 +65,27 @@ public class DefaultBinding implements Binding { return this.group; } - @Override public final void unbind() { - if (this.endpoint != null) { - this.endpoint.stop(); + if (this.lifecycle != null) { + this.lifecycle.stop(); } afterUnbind(); } + /** + * Listener method that executes after unbinding. Subclasses can implement their own + * behaviour on unbinding by overriding this method. + */ protected void afterUnbind() { } @Override public String toString() { - return " Binding [name=" + this.name + ", target=" + this.target + ", endpoint=" + - ((this.endpoint instanceof NamedComponent) ? ((NamedComponent) this.endpoint).getComponentName() : - ObjectUtils.nullSafeToString(this.endpoint)) + return " Binding [name=" + this.name + ", target=" + this.target + ", lifecycle=" + + ((this.lifecycle instanceof NamedComponent) + ? ((NamedComponent) this.lifecycle).getComponentName() + : ObjectUtils.nullSafeToString(this.lifecycle)) + "]"; } }