INT-1418: add control-bus

This commit is contained in:
David Syer
2010-09-03 13:39:49 +00:00
parent 3f390274db
commit 2e4f1e5b5e
14 changed files with 297 additions and 92 deletions

View File

@@ -30,6 +30,11 @@ import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
*/
public class AttributePollingChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected String parseSource(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2002-2010 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.integration.jmx.config;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.control.ControlBus;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
/**
* @author Dave Syer
* @since 2.0
*
*/
public class ControlBusFactoryBean implements FactoryBean<ControlBus> {
private final SubscribableChannel operationChannel;
private final IntegrationMBeanExporter exporter;
public ControlBusFactoryBean(IntegrationMBeanExporter exporter, SubscribableChannel operationChannel) {
this.exporter = exporter;
this.operationChannel = operationChannel;
}
public ControlBusFactoryBean(IntegrationMBeanExporter exporter) {
this(exporter, new DirectChannel());
}
public ControlBus getObject() throws Exception {
return new ControlBus(exporter, exporter.getServer(), operationChannel);
}
public Class<?> getObjectType() {
return ControlBus.class;
}
public boolean isSingleton() {
return true;
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2002-2010 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.integration.jmx.config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author Mark Fisher
* @since 2.0
*/
public class ControlBusParser extends AbstractSingleBeanDefinitionParser {
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected String getBeanClassName(Element element) {
return "org.springframework.integration.jmx.config.ControlBusFactoryBean";
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
builder.addConstructorArgReference(element.getAttribute("mbean-exporter"));
if (StringUtils.hasLength(element.getAttribute("operation-channel"))) {
builder.addConstructorArgReference(element.getAttribute("operation-channel"));
}
}
}

View File

@@ -34,6 +34,7 @@ public class JmxNamespaceHandler extends AbstractIntegrationNamespaceHandler {
this.registerBeanDefinitionParser("notification-listening-channel-adapter", new NotificationListeningChannelAdapterParser());
this.registerBeanDefinitionParser("notification-publishing-channel-adapter", new NotificationPublishingChannelAdapterParser());
this.registerBeanDefinitionParser("mbean-exporter", new MBeanExporterParser());
this.registerBeanDefinitionParser("control-bus", new ControlBusParser());
}
}

View File

@@ -19,9 +19,8 @@ package org.springframework.integration.jmx.config;
import javax.management.MBeanServerFactory;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.util.StringUtils;
@@ -31,23 +30,24 @@ import org.w3c.dom.Element;
* @author Mark Fisher
* @since 2.0
*/
public class MBeanExporterParser extends AbstractBeanDefinitionParser {
public class MBeanExporterParser extends AbstractSingleBeanDefinitionParser {
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
protected String getBeanClassName(Element element) {
return "org.springframework.integration.monitor.IntegrationMBeanExporter";
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
Object mbeanServer = getMBeanServer(element, parserContext);
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition("org.springframework.integration.monitor.IntegrationMBeanExporter");
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "domain");
builder.addPropertyValue("server", mbeanServer);
if (StringUtils.hasText(element.getAttribute("operation-channel"))) {
AbstractBeanDefinition controlBus = getControlBus(element, parserContext, mbeanServer, builder
.getRawBeanDefinition());
parserContext.getRegistry().registerBeanDefinition(
parserContext.getReaderContext().generateBeanName(controlBus), controlBus);
}
return builder.getBeanDefinition();
}
private Object getMBeanServer(Element element, ParserContext parserContext) {
@@ -60,15 +60,4 @@ public class MBeanExporterParser extends AbstractBeanDefinitionParser {
}
}
private AbstractBeanDefinition getControlBus(Element element, ParserContext parserContext, Object mbeanServer,
Object mbeanExporter) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition("org.springframework.integration.control.ControlBus");
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
builder.addConstructorArgValue(mbeanExporter);
builder.addConstructorArgValue(mbeanServer);
builder.addConstructorArgReference(element.getAttribute("operation-channel"));
return builder.getBeanDefinition();
}
}

View File

@@ -30,6 +30,11 @@ import org.springframework.util.StringUtils;
*/
public class NotificationListeningChannelAdapterParser extends AbstractSimpleBeanDefinitionParser {
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected String getBeanClassName(Element element) {
return "org.springframework.integration.jmx.NotificationListeningMessageProducer";

View File

@@ -30,6 +30,11 @@ import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
*/
public class NotificationPublishingChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(

View File

@@ -30,6 +30,11 @@ import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
*/
public class OperationInvokingChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(

View File

@@ -25,7 +25,7 @@ import org.w3c.dom.Element;
* @author Oleg Zhurakousky
* @since 2.0
*/
public class OperationInvokingOutboundGatewayParser extends AbstractConsumerEndpointParser{
public class OperationInvokingOutboundGatewayParser extends AbstractConsumerEndpointParser {
@Override
protected String getInputChannelAttributeName() {

View File

@@ -1,17 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration/jmx"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:integration="http://www.springframework.org/schema/integration"
targetNamespace="http://www.springframework.org/schema/integration/jmx"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:schema xmlns="http://www.springframework.org/schema/integration/jmx" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:integration="http://www.springframework.org/schema/integration" targetNamespace="http://www.springframework.org/schema/integration/jmx"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
<xsd:import namespace="http://www.springframework.org/schema/integration"
schemaLocation="http://www.springframework.org/schema/integration/spring-integration-2.0.xsd"/>
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" />
<xsd:import namespace="http://www.springframework.org/schema/integration" schemaLocation="http://www.springframework.org/schema/integration/spring-integration-2.0.xsd" />
<xsd:annotation>
<xsd:documentation><![CDATA[
@@ -22,18 +17,17 @@
<xsd:element name="attribute-polling-channel-adapter">
<xsd:annotation>
<xsd:documentation>
Defines an inbound Channel Adapter that polls for JMX attribute values.
Defines an inbound Channel Adapter that polls for JMX attribute values.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="adapterType">
<xsd:sequence minOccurs="0" maxOccurs="1">
<xsd:element ref="integration:poller"/>
<xsd:element ref="integration:poller" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" />
<xsd:attribute name="attribute-name" type="xsd:string" use="required"/>
<xsd:attribute name="auto-startup" type="xsd:string" default="true"/>
<xsd:attribute name="attribute-name" type="xsd:string" use="required" />
<xsd:attribute name="auto-startup" type="xsd:string" default="true" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -41,15 +35,15 @@
<xsd:element name="operation-invoking-outbound-gateway">
<xsd:annotation>
<xsd:documentation>
Defines an outbound Gateway which allows for Message-driven invocation of managed operations that return values
Defines an outbound Gateway which allows for Message-driven invocation of managed operations that
return values
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="operationInvokingType">
<xsd:attribute name="id" type="xsd:ID"/>
<xsd:attribute name="request-channel" type="xsd:string" use="required"/>
<xsd:attribute name="reply-channel" type="xsd:string" use="optional"/>
<xsd:attribute name="request-channel" type="xsd:string" use="required" />
<xsd:attribute name="reply-channel" type="xsd:string" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -58,14 +52,13 @@
<xsd:element name="operation-invoking-channel-adapter">
<xsd:annotation>
<xsd:documentation>
Defines an outbound Channel Adapter for invoking JMX operations.
Defines an outbound Channel Adapter for invoking JMX operations.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="operationInvokingType">
<xsd:attribute name="id" type="xsd:ID"/>
<xsd:attribute name="channel" type="xsd:string" use="optional"/>
<xsd:attribute name="channel" type="xsd:string" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -74,16 +67,15 @@
<xsd:element name="notification-listening-channel-adapter">
<xsd:annotation>
<xsd:documentation>
Defines an inbound Channel Adapter that listens for JMX notifications.
Defines an inbound Channel Adapter that listens for JMX notifications.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="adapterType">
<xsd:attribute name="id" type="xsd:ID" />
<xsd:attribute name="notification-filter" type="xsd:string" use="optional"/>
<xsd:attribute name="handback" type="xsd:string" use="optional"/>
<xsd:attribute name="send-timeout" type="xsd:string" use="optional"/>
<xsd:attribute name="notification-filter" type="xsd:string" use="optional" />
<xsd:attribute name="handback" type="xsd:string" use="optional" />
<xsd:attribute name="send-timeout" type="xsd:string" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -92,14 +84,13 @@
<xsd:element name="notification-publishing-channel-adapter">
<xsd:annotation>
<xsd:documentation>
Defines an outbound Channel Adapter that publishes JMX notifications.
Defines an outbound Channel Adapter that publishes JMX notifications.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="adapterType">
<xsd:attribute name="id" type="xsd:ID" />
<xsd:attribute name="default-notification-type" type="xsd:string" use="optional"/>
<xsd:attribute name="default-notification-type" type="xsd:string" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -108,40 +99,63 @@
<xsd:element name="mbean-exporter">
<xsd:annotation>
<xsd:documentation>
Exports Message Channels and Endpoints as MBeans
and optionally connects to an "operation channel".
Exports Message Channels and Endpoints as MBeans.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="mbeanServerIdentifyerType">
<xsd:attribute name="id" type="xsd:ID" />
<xsd:attribute name="domain" use="optional">
<xsd:annotation>
<xsd:documentation>
The domain name for the MBeans exported by this Control Bus.
The domain name for the MBeans exported by this Exporter.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="operation-channel" use="optional">
<xsd:annotation>
<xsd:documentation>
If provided a control bus will be created and subscribed to the Message Channel.
The channel can then be used to send operation commands to
this Control Bus. It must implement SubscribableChannel.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:expected-type type="org.springframework.integration.core.SubscribableChannel"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="control-bus">
<xsd:annotation>
<xsd:documentation>
Control bus accepts control messages for channels and endpoints on an (optional) "operation
channel".
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="id" type="xsd:ID" use="optional" />
<xsd:attribute name="mbean-exporter" use="required">
<xsd:annotation>
<xsd:documentation>
A reference to the MBeanExporter created using &lt;mbean-exporter/&gt; in this namespace.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:expected-type type="org.springframework.integration.monitor.IntegrationMBeanExporter" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="operation-channel" use="optional">
<xsd:annotation>
<xsd:documentation>
If provided a control bus will be created and subscribed to the Message Channel.
The channel can
then be used to send operation commands to
this Control Bus. It must implement SubscribableChannel.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:expected-type type="org.springframework.integration.core.SubscribableChannel" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="adapterType">
<xsd:annotation>
<xsd:documentation>
@@ -150,12 +164,12 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="mbeanServerIdentifyerType">
<xsd:attribute name="channel" type="xsd:string" use="required"/>
<xsd:attribute name="object-name" type="xsd:string" use="required"/>
<xsd:attribute name="channel" type="xsd:string" use="required" />
<xsd:attribute name="object-name" type="xsd:string" use="required" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="operationInvokingType">
<xsd:annotation>
<xsd:documentation>
@@ -164,19 +178,20 @@
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="mbeanServerIdentifyerType">
<xsd:attribute name="object-name" type="xsd:string" use="required"/>
<xsd:attribute name="operation-name" type="xsd:string" use="required"/>
<xsd:attribute name="object-name" type="xsd:string" use="required" />
<xsd:attribute name="operation-name" type="xsd:string" use="required" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="mbeanServerIdentifyerType">
<xsd:attribute name="id" type="xsd:ID" use="optional" />
<xsd:attribute name="mbean-server" type="xsd:string" default="mbeanServer">
<xsd:annotation>
<xsd:documentation>
Defines the name of the MBeanServer bean to connect to.
<xsd:annotation>
<xsd:documentation>
Defines the name of the MBeanServer bean to connect to.
</xsd:documentation>
</xsd:annotation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jmx
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
<context:mbean-server id="mbs"/>
<si:channel id="testChannel"/>
<jmx:control-bus mbean-exporter="mbeanExporter" operation-channel="testChannel"/>
<jmx:mbean-exporter id="mbeanExporter" mbean-server="mbs" domain="tests.ControlBusParser"/>
</beans>

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2010 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.integration.jmx.config;
import static org.junit.Assert.assertEquals;
import javax.management.MBeanServer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.control.ControlBus;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ControlBusParserTests {
@Autowired
private ApplicationContext context;
@Test
public void test() throws InterruptedException {
ControlBus controlBus = this.context.getBean(ControlBus.class);
assertEquals(controlBus.getOperationChannel(), this.context.getBean("testChannel"));
MBeanServer server = this.context.getBean("mbs", MBeanServer.class);
MBeanExporter exporter = (MBeanExporter) new DirectFieldAccessor(controlBus).getPropertyValue("exporter");
assertEquals(server, exporter.getServer());
exporter.destroy();
}
}

View File

@@ -17,6 +17,6 @@
<si:channel id="testChannel"/>
<jmx:mbean-exporter id="controlBus" mbean-server="mbs" operation-channel="testChannel" domain="tests.ControlBusParser"/>
<jmx:mbean-exporter id="mbeanExporter" mbean-server="mbs" domain="tests.MBeanExpoerterParser"/>
</beans>

View File

@@ -22,11 +22,9 @@ import javax.management.MBeanServer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.control.ControlBus;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -43,10 +41,8 @@ public class MBeanExporterParserTests {
@Test
public void test() throws InterruptedException {
ControlBus controlBus = this.context.getBean(ControlBus.class);
assertEquals(controlBus.getOperationChannel(), this.context.getBean("testChannel"));
IntegrationMBeanExporter exporter = this.context.getBean(IntegrationMBeanExporter.class);
MBeanServer server = this.context.getBean("mbs", MBeanServer.class);
MBeanExporter exporter = (MBeanExporter) new DirectFieldAccessor(controlBus).getPropertyValue("exporter");
assertEquals(server, exporter.getServer());
exporter.destroy();
}