Skip to content

Destroy RmiInboundGateway.RmiServiceExporter #2481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -31,12 +31,14 @@
*
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
*/
public abstract class AbstractInboundGatewayParser extends AbstractSimpleBeanDefinitionParser {

@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
throws BeanDefinitionStoreException {

String id = super.resolveId(element, definition, parserContext);
if (!StringUtils.hasText(id)) {
id = element.getAttribute("name");
Expand All @@ -58,21 +60,20 @@ protected boolean isEligibleAttribute(String attributeName) {
protected final void postProcess(BeanDefinitionBuilder builder, Element element) {
String requestChannelRef = element.getAttribute("request-channel");
Assert.hasText(requestChannelRef, "a 'request-channel' reference is required");
builder.addPropertyReference("requestChannel", requestChannelRef);
builder.addPropertyValue("requestChannelName", requestChannelRef);
String replyChannel = element.getAttribute("reply-channel");
if (StringUtils.hasText(replyChannel)) {
builder.addPropertyReference("replyChannel", replyChannel);
builder.addPropertyValue("replyChannelName", replyChannel);
}
String errorChannel = element.getAttribute("error-channel");
if (StringUtils.hasText(errorChannel)) {
builder.addPropertyReference("errorChannel", errorChannel);
builder.addPropertyValue("errorChannelName", errorChannel);
}
this.doPostProcess(builder, element);
}

/**
* Subclasses may add to the bean definition by overriding this method.
*
* @param builder The builder.
* @param element The element.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -372,7 +372,12 @@ public MessageChannel getRequestChannel() {
return this.requestChannel;
}

protected MessageChannel getReplyChannel() {
/**
* Return this gateway's reply channel if any.
* @return the reply channel instance
* @since 5.1
*/
public MessageChannel getReplyChannel() {
if (this.replyChannelName != null) {
synchronized (this) {
if (this.replyChannelName != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -444,7 +444,7 @@ public void testInGateway1() {
assertEquals(456L, dfa.getPropertyValue("replyTimeout"));
assertEquals("inGateway1", tcpInboundGateway1.getComponentName());
assertEquals("ip:tcp-inbound-gateway", tcpInboundGateway1.getComponentType());
assertEquals(errorChannel, dfa.getPropertyValue("errorChannel"));
assertEquals(errorChannel, tcpInboundGateway1.getErrorChannel());
assertTrue(cfS2.isLookupHost());
assertFalse(tcpInboundGateway1.isAutoStartup());
assertEquals(126, tcpInboundGateway1.getPhase());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -501,11 +501,6 @@ private class GatewayDelegate extends MessagingGatewaySupport {
super();
}

@Override
public MessageChannel getErrorChannel() {
return super.getErrorChannel();
}

@Override
protected void send(Object request) {
super.send(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void testDefaultConfig() throws Exception {
assertFalse(TestUtils.getPropertyValue(this.defaultGateway, "extractPayload", Boolean.class));
assertSame(this.serializer, TestUtils.getPropertyValue(this.defaultGateway, "serializer"));
assertTrue(TestUtils.getPropertyValue(this.defaultGateway, "serializerExplicitlySet", Boolean.class));
assertSame(this.receiveChannel, TestUtils.getPropertyValue(this.defaultGateway, "replyChannel"));
assertSame(this.requestChannel, TestUtils.getPropertyValue(this.defaultGateway, "requestChannel"));
assertSame(this.receiveChannel, this.defaultGateway.getReplyChannel());
assertSame(this.requestChannel, this.defaultGateway.getRequestChannel());
assertEquals(2000L, TestUtils.getPropertyValue(this.defaultGateway, "replyTimeout"));
assertNotNull(TestUtils.getPropertyValue(this.defaultGateway, "taskExecutor"));
assertFalse(TestUtils.getPropertyValue(this.defaultGateway, "autoStartup", Boolean.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -33,25 +33,25 @@
* An inbound Messaging Gateway for RMI-based remoting.
*
* @author Mark Fisher
* @author Artem Bilan
*/
public class RmiInboundGateway extends MessagingGatewaySupport implements RequestReplyExchanger, InitializingBean {
public class RmiInboundGateway extends MessagingGatewaySupport
implements RequestReplyExchanger, InitializingBean {

public static final String SERVICE_NAME_PREFIX = "org.springframework.integration.rmiGateway.";


private volatile String requestChannelName;
private final RmiServiceExporter exporter = new RmiServiceExporter();

private volatile String registryHost;
private String requestChannelName;

private volatile int registryPort = Registry.REGISTRY_PORT;
private String registryHost;

private volatile boolean expectReply = true;
private int registryPort = Registry.REGISTRY_PORT;

private volatile RemoteInvocationExecutor remoteInvocationExecutor;
private boolean expectReply = true;

private volatile RmiServiceExporter exporter;

private final Object initializationMonitor = new Object();
private RemoteInvocationExecutor remoteInvocationExecutor;


/**
Expand All @@ -62,16 +62,21 @@ public class RmiInboundGateway extends MessagingGatewaySupport implements Reques
public void setRequestChannel(MessageChannel requestChannel) {
Assert.notNull(requestChannel, "requestChannel must not be null");
Assert.isTrue(requestChannel instanceof NamedComponent &&
StringUtils.hasText(((NamedComponent) requestChannel).getComponentName()),
StringUtils.hasText(((NamedComponent) requestChannel).getComponentName()),
"RmiGateway's request channel must have a name.");
this.requestChannelName = ((NamedComponent) requestChannel).getComponentName();
super.setRequestChannel(requestChannel);
}

@Override
public void setRequestChannelName(String requestChannelName) {
this.requestChannelName = requestChannelName;
super.setRequestChannelName(requestChannelName);
}

/**
* Specify whether the gateway should be expected to return a reply.
* The default is '<code>true</code>'.
*
* @param expectReply true when a reply is expected.
*/
public void setExpectReply(boolean expectReply) {
Expand All @@ -97,33 +102,36 @@ public String getComponentType() {

@Override
protected void onInit() throws Exception {
synchronized (this.initializationMonitor) {
if (this.exporter == null) {
RmiServiceExporter exporter = new RmiServiceExporter();
if (this.registryHost != null) {
exporter.setRegistryHost(this.registryHost);
}
exporter.setRegistryPort(this.registryPort);
if (this.remoteInvocationExecutor != null) {
exporter.setRemoteInvocationExecutor(this.remoteInvocationExecutor);
}
exporter.setService(this);
exporter.setServiceInterface(RequestReplyExchanger.class);
exporter.setServiceName(SERVICE_NAME_PREFIX + this.requestChannelName);
exporter.afterPropertiesSet();
this.exporter = exporter;
}
}
super.onInit();

if (this.registryHost != null) {
this.exporter.setRegistryHost(this.registryHost);
}
this.exporter.setRegistryPort(this.registryPort);
if (this.remoteInvocationExecutor != null) {
this.exporter.setRemoteInvocationExecutor(this.remoteInvocationExecutor);
}
this.exporter.setService(this);
this.exporter.setServiceInterface(RequestReplyExchanger.class);
this.exporter.setServiceName(SERVICE_NAME_PREFIX + this.requestChannelName);
this.exporter.afterPropertiesSet();
}

@Override
public Message<?> exchange(Message<?> message) {
if (this.expectReply) {
return this.sendAndReceiveMessage(message);
return sendAndReceiveMessage(message);
}
else {
send(message);
return null;
}
this.send(message);
return null;
}

@Override
public void destroy() throws Exception {
super.destroy();
this.exporter.destroy();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</int:channel>

<bean id="port" class="java.lang.Integer">
<constructor-arg value="#{T(org.springframework.integration.test.util.SocketUtils).findAvailableServerSocket(11099)}" />
<constructor-arg value="#{T(org.springframework.integration.test.util.SocketUtils).findAvailableServerSocket()}" />
</bean>

<!-- Bad -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:rmi="http://www.springframework.org/schema/integration/rmi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:rmi="http://www.springframework.org/schema/integration/rmi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
Expand All @@ -16,18 +16,22 @@

<channel id="testErrorChannel"/>

<rmi:inbound-gateway id="gatewayWithDefaults" request-channel="testChannel"/>
<rmi:inbound-gateway id="gatewayWithDefaults" request-channel="testChannel" auto-startup="false"/>

<rmi:inbound-gateway id="gatewayWithCustomProperties" request-channel="testChannel"
expect-reply="false" request-timeout="123" reply-timeout="456"/>
expect-reply="false" request-timeout="123" reply-timeout="456" auto-startup="false"/>

<rmi:inbound-gateway id="gatewayWithHostAndErrorChannel" request-channel="testChannel" registry-host="localhost"
error-channel="testErrorChannel"/>
error-channel="testErrorChannel" auto-startup="false"/>

<rmi:inbound-gateway id="gatewayWithPort" request-channel="testChannel" registry-port="1234"/>
<rmi:inbound-gateway id="gatewayWithPort" request-channel="testChannel" registry-port="1234" auto-startup="false"/>

<rmi:inbound-gateway id="gatewayWithExecutorRef" request-channel="testChannel" remote-invocation-executor="invocationExecutor"/>
<rmi:inbound-gateway id="gatewayWithExecutorRef"
request-channel="testChannel"
remote-invocation-executor="invocationExecutor"
auto-startup="false"/>

<beans:bean id="invocationExecutor" class="org.springframework.integration.rmi.config.StubRemoteInvocationExecutor"/>
<beans:bean id="invocationExecutor"
class="org.springframework.integration.rmi.config.StubRemoteInvocationExecutor"/>

</beans:beans>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -59,7 +59,7 @@ public void gatewayWithDefaultsAndHistory() {
assertEquals("gatewayWithDefaults", gateway.getComponentName());
assertEquals("rmi:inbound-gateway", gateway.getComponentType());
assertTrue(TestUtils.getPropertyValue(gateway, "expectReply", Boolean.class));
assertSame(this.channel, TestUtils.getPropertyValue(gateway, "requestChannel"));
assertSame(this.channel, gateway.getRequestChannel());
assertEquals(1000L, TestUtils.getPropertyValue(gateway, "messagingTemplate.sendTimeout"));
assertEquals(1000L, TestUtils.getPropertyValue(gateway, "messagingTemplate.receiveTimeout"));
}
Expand All @@ -69,7 +69,7 @@ public void gatewayWithCustomProperties() {
RmiInboundGateway gateway = (RmiInboundGateway) context.getBean("gatewayWithCustomProperties");

assertFalse(TestUtils.getPropertyValue(gateway, "expectReply", Boolean.class));
assertSame(this.channel, TestUtils.getPropertyValue(gateway, "requestChannel"));
assertSame(this.channel, gateway.getRequestChannel());
assertEquals(123L, TestUtils.getPropertyValue(gateway, "messagingTemplate.sendTimeout"));
assertEquals(456L, TestUtils.getPropertyValue(gateway, "messagingTemplate.receiveTimeout"));
}
Expand All @@ -78,7 +78,7 @@ public void gatewayWithCustomProperties() {
public void gatewayWithHost() {
RmiInboundGateway gateway = (RmiInboundGateway) context.getBean("gatewayWithHostAndErrorChannel");
assertEquals("localhost", TestUtils.getPropertyValue(gateway, "registryHost"));
assertSame(context.getBean("testErrorChannel"), TestUtils.getPropertyValue(gateway, "errorChannel"));
assertSame(context.getBean("testErrorChannel"), gateway.getErrorChannel());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,42 @@
request-channel="localChannel"
remote-channel="testChannel"
configurer="configurer"
host="localhost"/>
host="localhost"
port="#{T(org.springframework.integration.rmi.config.RmiOutboundGatewayParserTests).port}"/>

<channel id="advisedChannel"/>

<rmi:outbound-gateway id="advised"
request-channel="advisedChannel"
remote-channel="testChannel"
requires-reply="false"
host="localhost">
host="localhost"
port="#{T(org.springframework.integration.rmi.config.RmiOutboundGatewayParserTests).port}">
<rmi:request-handler-advice-chain>
<beans:ref bean="advice" />
<beans:ref bean="advice"/>
</rmi:request-handler-advice-chain>
</rmi:outbound-gateway>

<beans:bean id="configurer" class="org.mockito.Mockito" factory-method="mock">
<beans:constructor-arg
value="org.springframework.integration.rmi.RmiOutboundGateway$RmiProxyFactoryBeanConfigurer" />
value="org.springframework.integration.rmi.RmiOutboundGateway$RmiProxyFactoryBeanConfigurer"/>
</beans:bean>

<beans:bean id="advice"
class="org.springframework.integration.rmi.config.RmiOutboundGatewayParserTests$FooAdvice" />
class="org.springframework.integration.rmi.config.RmiOutboundGatewayParserTests$FooAdvice"/>

<chain input-channel="rmiOutboundGatewayInsideChain">
<rmi:outbound-gateway remote-channel="testChannel" host="localhost" requires-reply="false"/>
<rmi:outbound-gateway remote-channel="testChannel"
host="localhost"
port="#{T(org.springframework.integration.rmi.config.RmiOutboundGatewayParserTests).port}"
requires-reply="false"/>
</chain>


<channel id="remoteChannel"/>

<rmi:inbound-gateway request-channel="remoteChannel"/>
<rmi:inbound-gateway request-channel="remoteChannel"
registry-port="#{T(org.springframework.integration.rmi.config.RmiOutboundGatewayParserTests).port}"/>

<service-activator input-channel="remoteChannel" expression="payload.toUpperCase()"/>

Expand All @@ -56,7 +62,9 @@
</channel>

<chain input-channel="requestReplyRmiWithChainChannel" output-channel="replyChannel">
<rmi:outbound-gateway remote-channel="remoteChannel" host="localhost"/>
<rmi:outbound-gateway remote-channel="remoteChannel"
host="localhost"
port="#{T(org.springframework.integration.rmi.config.RmiOutboundGatewayParserTests).port}"/>
</chain>

</beans:beans>
Loading