MessageBus now recognizes MessagingGateway implementations within the ApplicationContext, and if a gateway also implements Lifecycle (e.g. JmsGateway), that lifecycle can now be managed by the MessageBus (INT-237).

This commit is contained in:
Mark Fisher
2008-06-02 18:54:51 +00:00
parent 6fe0e44331
commit aab138cb10
3 changed files with 63 additions and 1 deletions

View File

@@ -51,6 +51,7 @@ import org.springframework.integration.endpoint.DefaultEndpointRegistry;
import org.springframework.integration.endpoint.EndpointRegistry;
import org.springframework.integration.endpoint.HandlerEndpoint;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.endpoint.MessagingGateway;
import org.springframework.integration.endpoint.SourceEndpoint;
import org.springframework.integration.endpoint.TargetEndpoint;
import org.springframework.integration.handler.MessageHandler;
@@ -194,6 +195,15 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio
}
}
@SuppressWarnings("unchecked")
private void registerGateways(ApplicationContext context) {
Map<String, MessagingGateway> gatewayBeans =
(Map<String, MessagingGateway>) context.getBeansOfType(MessagingGateway.class);
for (Map.Entry<String, MessagingGateway> entry : gatewayBeans.entrySet()) {
this.registerGateway(entry.getKey(), entry.getValue());
}
}
public void initialize() {
synchronized (this.lifecycleMonitor) {
if (this.initialized || this.initializing) {
@@ -416,6 +426,18 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio
}
}
private void registerGateway(String name, MessagingGateway gateway) {
if (gateway instanceof Lifecycle) {
this.lifecycleEndpoints.add((Lifecycle) gateway);
if (this.isRunning()) {
((Lifecycle) gateway).start();
}
}
if (logger.isInfoEnabled()) {
logger.info("registered gateway '" + name + "'");
}
}
private void activateSubscription(MessageChannel channel, Target target, Schedule schedule) {
SubscriptionManager manager = this.subscriptionManagers.get(channel);
if (manager == null) {
@@ -496,6 +518,7 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio
if (event instanceof ContextRefreshedEvent) {
ApplicationContext context = ((ContextRefreshedEvent) event).getApplicationContext();
this.registerEndpoints(context);
this.registerGateways(context);
if (this.configureAsyncEventMulticaster) {
this.initialize();
this.doConfigureAsyncEventMulticaster(context);

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2002-2008 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.endpoint;
import org.springframework.integration.message.Message;
/**
* Base interface for gateway adapters. In Spring Integration, a "gateway" is a
* component that sends messages to and receives messages from message channels
* so that application code does not need to be aware of channels or even messages.
*
* @author Mark Fisher
*/
public interface MessagingGateway {
void send(Object object);
Object receive();
Object sendAndReceive(Object object);
Message<?> sendAndReceiveMessage(Object object);
}

View File

@@ -17,6 +17,7 @@
package org.springframework.integration.gateway;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.endpoint.MessagingGateway;
import org.springframework.integration.message.DefaultMessageCreator;
import org.springframework.integration.message.DefaultMessageMapper;
import org.springframework.integration.message.Message;
@@ -34,7 +35,7 @@ import org.springframework.util.Assert;
*
* @author Mark Fisher
*/
public class SimpleMessagingGateway extends MessagingGatewaySupport {
public class SimpleMessagingGateway extends MessagingGatewaySupport implements MessagingGateway {
private MessageCreator messageCreator = new DefaultMessageCreator();