diff --git a/spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/MessageBusAdapter.java b/spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/MessageBusAdapter.java index f5f5fcfa0..5e77bcc99 100644 --- a/spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/MessageBusAdapter.java +++ b/spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/MessageBusAdapter.java @@ -116,6 +116,57 @@ public class MessageBusAdapter implements Lifecycle, ApplicationContextAware { this.inputChannels = new LinkedHashSet(inputChannels); } + public Collection getOutputChannels() { + return outputChannels; + } + + public OutputChannelSpec getOutputChannel(String name) { + if (name==null) { + return null; + } + for (OutputChannelSpec spec : outputChannels) { + if (name.equals(spec.getName())) { + return spec; + } + } + return null; + } + + public InputChannelSpec getInputChannel(String name) { + if (name==null) { + return null; + } + for (InputChannelSpec spec : inputChannels) { + if (name.equals(spec.getName())) { + return spec; + } + } + return null; + } + + public Collection getInputChannels() { + return inputChannels; + } + + public void tap(String outputChannel) { + OutputChannelSpec channel = getOutputChannel(outputChannel); + if (channel==null || channel.isTapped()) { + return; + } + createAndBindTapChannel(channel.getTapChannelName(), channel.getMessageChannel()); + channel.setTapped(true); + } + + public void untap(String outputChannel) { + OutputChannelSpec channel = getOutputChannel(outputChannel); + if (channel==null || !channel.isTapped()) { + return; + } + String tapChannelName = channel.getTapChannelName(); + messageBus.unbindProducers(tapChannelName); + channel.setTapped(false); + } + @Override @ManagedOperation public void start() { @@ -289,4 +340,5 @@ public class MessageBusAdapter implements Lifecycle, ApplicationContextAware { }); } } + } diff --git a/spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/OutputChannelSpec.java b/spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/OutputChannelSpec.java index 0fe0c2481..710f839f9 100644 --- a/spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/OutputChannelSpec.java +++ b/spring-bus-core/src/main/java/org/springframework/bus/runner/adapter/OutputChannelSpec.java @@ -24,7 +24,7 @@ import org.springframework.messaging.MessageChannel; */ public class OutputChannelSpec extends InputChannelSpec { - private boolean tapped; + private boolean tapped = false; private String tapChannelName; public OutputChannelSpec(String name, MessageChannel channel) { diff --git a/spring-bus-core/src/main/java/org/springframework/bus/runner/config/ChannelsEndpoint.java b/spring-bus-core/src/main/java/org/springframework/bus/runner/config/ChannelsEndpoint.java new file mode 100644 index 000000000..5919d2588 --- /dev/null +++ b/spring-bus-core/src/main/java/org/springframework/bus/runner/config/ChannelsEndpoint.java @@ -0,0 +1,76 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.bus.runner.config; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.boot.actuate.endpoint.AbstractEndpoint; +import org.springframework.bus.runner.adapter.MessageBusAdapter; +import org.springframework.bus.runner.adapter.OutputChannelSpec; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ChannelsEndpoint extends AbstractEndpoint> { + + private MessageBusProperties module; + private MessageBusAdapter adapter; + + public ChannelsEndpoint(MessageBusProperties module, MessageBusAdapter adapter) { + super("channels"); + this.module = module; + this.adapter = adapter; + } + + @RequestMapping(value="/channels/taps") + public List taps() { + List list = new ArrayList(); + for (OutputChannelSpec spec : adapter.getOutputChannels()) { + if (spec.isTapped()) { + list.add(spec); + } + } + return list ; + } + + @RequestMapping(value="/channels/taps", method=RequestMethod.POST) + public OutputChannelSpec tap(@RequestParam String channel) { + adapter.tap(channel); + return adapter.getOutputChannel(channel); + } + + @RequestMapping(value="/channels/taps", method=RequestMethod.DELETE) + public OutputChannelSpec untap(@RequestParam String channel) { + adapter.untap(channel); + return adapter.getOutputChannel(channel); + } + + @Override + public Map invoke() { + LinkedHashMap map = new LinkedHashMap(); + map.put("inputChannels", adapter.getInputChannels()); + map.put("outputChannels", adapter.getOutputChannels()); + map.put("module", module); + return map; + } + +} \ No newline at end of file diff --git a/spring-bus-core/src/main/java/org/springframework/bus/runner/config/MessageBusAdapterConfiguration.java b/spring-bus-core/src/main/java/org/springframework/bus/runner/config/MessageBusAdapterConfiguration.java index 45fd4fcd4..f1531c1e2 100644 --- a/spring-bus-core/src/main/java/org/springframework/bus/runner/config/MessageBusAdapterConfiguration.java +++ b/spring-bus-core/src/main/java/org/springframework/bus/runner/config/MessageBusAdapterConfiguration.java @@ -16,16 +16,13 @@ package org.springframework.bus.runner.config; import java.util.Collection; -import java.util.LinkedHashMap; import java.util.LinkedHashSet; -import java.util.Map; import java.util.Properties; import java.util.Set; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.actuate.endpoint.AbstractEndpoint; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.bus.runner.adapter.InputChannelSpec; import org.springframework.bus.runner.adapter.MessageBusAdapter; @@ -63,17 +60,8 @@ public class MessageBusAdapterConfiguration { } @Bean - public AbstractEndpoint messageBusEndpoint() { - return new AbstractEndpoint>("messages") { - @Override - public Map invoke() { - LinkedHashMap map = new LinkedHashMap(); - map.put("inputChannels", getInputChannels()); - map.put("outputChannels", getOutputChannels()); - map.put("module", module); - return map; - } - }; + public ChannelsEndpoint channelsEndpoint(MessageBusAdapter adapter) { + return new ChannelsEndpoint(module, adapter); } protected Collection getOutputChannels() { @@ -110,7 +98,7 @@ public class MessageBusAdapterConfiguration { .getTapChannelName(getPlainChannelName(channel.getName())) : module.getTapChannelName(); channel.setTapChannelName(tapChannelName); - channel.setTapped(true); // TODO: determine when this is the case + channel.setTapped(false); channels.add(channel); } } diff --git a/spring-bus-core/src/test/java/org/springframework/bus/runner/config/MessageBusAdapterConfigurationTests.java b/spring-bus-core/src/test/java/org/springframework/bus/runner/config/MessageBusAdapterConfigurationTests.java index 3bc75dfd2..9ac516edb 100644 --- a/spring-bus-core/src/test/java/org/springframework/bus/runner/config/MessageBusAdapterConfigurationTests.java +++ b/spring-bus-core/src/test/java/org/springframework/bus/runner/config/MessageBusAdapterConfigurationTests.java @@ -30,7 +30,6 @@ import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.bus.runner.adapter.InputChannelSpec; import org.springframework.bus.runner.adapter.OutputChannelSpec; import org.springframework.bus.runner.config.MessageBusAdapterConfigurationTests.Empty; -import org.springframework.bus.runner.config.MessageBusProperties.Tap; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import;