From 69fc0a0cb32ad4311096734d5b3860ad9cd53d6e Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 11 Dec 2008 23:38:48 +0000 Subject: [PATCH] INT-507 --- .../HttpInvokerInboundGateway.java | 19 ++++++---- .../integration/rmi/RmiInboundGateway.java | 35 ++++++++++++------- .../gateway/AbstractMessagingGateway.java | 16 +++++++++ 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/org.springframework.integration.httpinvoker/src/main/java/org/springframework/integration/httpinvoker/HttpInvokerInboundGateway.java b/org.springframework.integration.httpinvoker/src/main/java/org/springframework/integration/httpinvoker/HttpInvokerInboundGateway.java index 432cdb2500..e7bf9a21b5 100644 --- a/org.springframework.integration.httpinvoker/src/main/java/org/springframework/integration/httpinvoker/HttpInvokerInboundGateway.java +++ b/org.springframework.integration.httpinvoker/src/main/java/org/springframework/integration/httpinvoker/HttpInvokerInboundGateway.java @@ -62,14 +62,21 @@ public class HttpInvokerInboundGateway extends RemotingInboundGatewaySupport imp private volatile HttpInvokerServiceExporter exporter; + private final Object initializationMonitor = new Object(); + @Override - protected void onInit() { - HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter(); - exporter.setService(this); - exporter.setServiceInterface(RemoteMessageHandler.class); - exporter.afterPropertiesSet(); - this.exporter = exporter; + protected void onInit() throws Exception { + synchronized (this.initializationMonitor) { + if (this.exporter == null) { + HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter(); + exporter.setService(this); + exporter.setServiceInterface(RemoteMessageHandler.class); + exporter.afterPropertiesSet(); + this.exporter = exporter; + } + } + super.onInit(); } public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { diff --git a/org.springframework.integration.rmi/src/main/java/org/springframework/integration/rmi/RmiInboundGateway.java b/org.springframework.integration.rmi/src/main/java/org/springframework/integration/rmi/RmiInboundGateway.java index 0ded7c0bef..599e978d06 100644 --- a/org.springframework.integration.rmi/src/main/java/org/springframework/integration/rmi/RmiInboundGateway.java +++ b/org.springframework.integration.rmi/src/main/java/org/springframework/integration/rmi/RmiInboundGateway.java @@ -16,7 +16,6 @@ package org.springframework.integration.rmi; -import java.rmi.RemoteException; import java.rmi.registry.Registry; import org.springframework.beans.factory.InitializingBean; @@ -46,6 +45,10 @@ public class RmiInboundGateway extends RemotingInboundGatewaySupport implements private volatile RemoteInvocationExecutor remoteInvocationExecutor; + private volatile RmiServiceExporter exporter; + + private final Object initializationMonitor = new Object(); + /** * Specify the request channel where messages will be sent. @@ -73,19 +76,25 @@ public class RmiInboundGateway extends RemotingInboundGatewaySupport implements } @Override - protected void onInit() throws RemoteException { - RmiServiceExporter exporter = new RmiServiceExporter(); - if (this.registryHost != null) { - exporter.setRegistryHost(this.registryHost); + 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(RemoteMessageHandler.class); + exporter.setServiceName(SERVICE_NAME_PREFIX + this.requestChannelName); + exporter.afterPropertiesSet(); + this.exporter = exporter; + } } - exporter.setRegistryPort(this.registryPort); - if (this.remoteInvocationExecutor != null) { - exporter.setRemoteInvocationExecutor(this.remoteInvocationExecutor); - } - exporter.setService(this); - exporter.setServiceInterface(RemoteMessageHandler.class); - exporter.setServiceName(SERVICE_NAME_PREFIX + this.requestChannelName); - exporter.afterPropertiesSet(); + super.onInit(); } } diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java b/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java index 9527f994df..9b8d86c347 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java @@ -52,6 +52,8 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint implemen private volatile boolean shouldThrowErrors = true; + private volatile boolean initialized; + private volatile AbstractEndpoint replyMessageCorrelator; private final Object replyMessageCorrelatorMonitor = new Object(); @@ -106,7 +108,19 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint implemen this.shouldThrowErrors = shouldThrowErrors; } + @Override + protected void onInit() throws Exception { + this.initialized = true; + } + + private void initializeIfNecessary() { + if (!this.initialized) { + this.afterPropertiesSet(); + } + } + public void send(Object object) { + this.initializeIfNecessary(); Assert.state(this.requestChannel != null, "send is not supported, because no request channel has been configured"); Message message = this.toMessage(object); @@ -117,6 +131,7 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint implemen } public Object receive() { + this.initializeIfNecessary(); Assert.state(this.replyChannel != null && (this.replyChannel instanceof PollableChannel), "receive is not supported, because no pollable reply channel has been configured"); Message message = this.channelTemplate.receive((PollableChannel) this.replyChannel); @@ -141,6 +156,7 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint implemen } private Message sendAndReceiveMessage(Message message) { + this.initializeIfNecessary(); Assert.notNull(message, "request message must not be null"); if (this.requestChannel == null) { throw new MessageDeliveryException(message,