INT-1018 added namespace support for operation-invoking-outbound-gateway

This commit is contained in:
Oleg Zhurakousky
2010-07-14 16:16:03 +00:00
parent 08d7b8c90e
commit 5db5f5fb7a
6 changed files with 248 additions and 0 deletions

View File

@@ -22,12 +22,14 @@ import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHa
* Namespace handler for Spring Integration's <em>jmx</em> namespace.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 2.0
*/
public class JmxNamespaceHandler extends AbstractIntegrationNamespaceHandler {
public void init() {
this.registerBeanDefinitionParser("operation-invoking-channel-adapter", new OperationInvokingChannelAdapterParser());
this.registerBeanDefinitionParser("operation-invoking-outbound-gateway", new OperationInvokingOutboundGatewayParser());
this.registerBeanDefinitionParser("attribute-polling-channel-adapter", new AttributePollingChannelAdapterParser());
this.registerBeanDefinitionParser("notification-listening-channel-adapter", new NotificationListeningChannelAdapterParser());
this.registerBeanDefinitionParser("notification-publishing-channel-adapter", new NotificationPublishingChannelAdapterParser());

View File

@@ -0,0 +1,47 @@
/*
* 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.ParserContext;
import org.springframework.integration.config.xml.AbstractConsumerEndpointParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.w3c.dom.Element;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
public class OperationInvokingOutboundGatewayParser extends AbstractConsumerEndpointParser{
@Override
protected String getInputChannelAttributeName() {
return "request-channel";
}
@Override
protected BeanDefinitionBuilder parseHandler(Element element,
ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(
"org.springframework.integration.jmx.OperationInvokingMessageHandler");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "mbean-server", "server");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-object-name");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-operation-name");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel", "outputChannel");
return builder;
}
}

View File

@@ -37,6 +37,21 @@
<xsd:attribute name="auto-startup" type="xsd:string" default="true"/>
</xsd:complexType>
</xsd:element>
<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
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="id" 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:attribute name="mbean-server" type="xsd:string" default="mbeanServer"/>
<xsd:attribute name="default-object-name" type="xsd:string" use="optional"/>
<xsd:attribute name="default-operation-name" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="operation-invoking-channel-adapter">
<xsd:annotation>

View File

@@ -0,0 +1,54 @@
<?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-2.0.xsd">
<context:mbean-export/>
<context:mbean-server/>
<si:channel id="noDefaultInput"/>
<si:channel id="noDefaultOutput">
<si:queue/>
</si:channel>
<si:channel id="noDefaultInputA"/>
<si:channel id="noDefaultOutputA">
<si:queue/>
</si:channel>
<si:channel id="defaultInput"/>
<si:channel id="defaultOutput">
<si:queue/>
</si:channel>
<jmx:operation-invoking-outbound-gateway request-channel="defaultInput"
reply-channel="defaultOutput"
default-object-name="org.springframework.integration.jmx.config:type=TestBean,name=testBeanForDefaultsGateway"
default-operation-name="testWithReturn"/>
<jmx:operation-invoking-outbound-gateway request-channel="noDefaultInput"
reply-channel="noDefaultOutput"/>
<jmx:operation-invoking-outbound-gateway request-channel="noDefaultInputA"/>
<bean id="testBeanForNoDefaultsGateway" class="org.springframework.integration.jmx.config.TestBean"/>
<bean id="testBeanForDefaultsGateway" class="org.springframework.integration.jmx.config.TestBean"/>
</beans>

View File

@@ -0,0 +1,123 @@
/*
* 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 java.util.List;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.jmx.JmxHeaders;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.StringMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Oleg Zhurakousky
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class OperationInvokingOutboundGatewayTests {
@Autowired
@Qualifier("noDefaultInput")
private MessageChannel noDefaultInput;
@Autowired
@Qualifier("noDefaultOutput")
private PollableChannel noDefaultOutput;
@Autowired
@Qualifier("noDefaultInputA")
private MessageChannel noDefaultInputA;
@Autowired
@Qualifier("noDefaultOutputA")
private PollableChannel noDefaultOutputA;
@Autowired
@Qualifier("defaultInput")
private MessageChannel defaultInput;
@Autowired
@Qualifier("defaultOutput")
private PollableChannel defaultOutput;
@Autowired
private TestBean testBeanForDefaultsGateway;
@Autowired
private TestBean testBeanForNoDefaultsGateway;
@After
public void resetLists() {
testBeanForDefaultsGateway.messages.clear();
testBeanForNoDefaultsGateway.messages.clear();
}
@Test
public void adapterWithoutNoDefaultsAndReturn() throws Exception {
noDefaultInput.send(createMessageWithHeadersForReturnCase("1"));
assertEquals(1, ((List<?>)noDefaultOutput.receive().getPayload()).size());
noDefaultInput.send(createMessageWithHeadersForReturnCase("2"));
assertEquals(2, ((List<?>)noDefaultOutput.receive().getPayload()).size());
noDefaultInput.send(createMessageWithHeadersForReturnCase("3"));
assertEquals(3, ((List<?>)noDefaultOutput.receive().getPayload()).size());
}
@Test
public void adapterWithoutNoDefaultsAndReturnAndReplyChannel() throws Exception {
noDefaultInputA.send(createMessageWithHeadersForReturnCaseAndReplyChannel("1", noDefaultOutputA));
assertEquals(1, ((List<?>)noDefaultOutputA.receive().getPayload()).size());
noDefaultInputA.send(createMessageWithHeadersForReturnCaseAndReplyChannel("2", noDefaultOutputA));
assertEquals(2, ((List<?>)noDefaultOutputA.receive().getPayload()).size());
noDefaultInputA.send(createMessageWithHeadersForReturnCaseAndReplyChannel("3", noDefaultOutputA));
assertEquals(3, ((List<?>)noDefaultOutputA.receive().getPayload()).size());
}
@Test
public void adapterWithoutDefaultsAndReturn() throws Exception {
defaultInput.send(new StringMessage("1"));
assertEquals(1, ((List<?>)defaultOutput.receive().getPayload()).size());
defaultInput.send(new StringMessage("2"));
assertEquals(2, ((List<?>)defaultOutput.receive().getPayload()).size());
defaultInput.send(new StringMessage("3"));
assertEquals(3, ((List<?>)defaultOutput.receive().getPayload()).size());
}
private static Message<String> createMessageWithHeadersForReturnCase(String payload) {
String objectName = "org.springframework.integration.jmx.config:name=testBeanForNoDefaultsGateway,type=TestBean";
return MessageBuilder.withPayload(payload)
.setHeader(JmxHeaders.OBJECT_NAME, objectName)
.setHeader(JmxHeaders.OPERATION_NAME, "testWithReturn")
.build();
}
private static Message<String> createMessageWithHeadersForReturnCaseAndReplyChannel(String payload, MessageChannel replyChannel) {
String objectName = "org.springframework.integration.jmx.config:name=testBeanForNoDefaultsGateway,type=TestBean";
return MessageBuilder.withPayload(payload)
.setHeader(JmxHeaders.OBJECT_NAME, objectName)
.setHeader(JmxHeaders.OPERATION_NAME, "testWithReturn")
.setReplyChannel(replyChannel)
.build();
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.jmx.export.annotation.ManagedResource;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 2.0
*/
@ManagedResource
@@ -41,5 +42,11 @@ public class TestBean {
public void test(String text) {
this.messages.add(text);
}
@ManagedOperation
public List<String> testWithReturn(String text) {
this.messages.add(text);
return messages;
}
}