RouterMessageHandlerAdapter now sets the ChannelRegistry on its target Object if that target implements ChannelRegistryAware, and DefaultMessageEndpoint now sets the ChannelRegistry on any ChannelRegistryAware handler (INT-125).

This commit is contained in:
Mark Fisher
2008-04-03 21:44:56 +00:00
parent 07edd8f423
commit eb8b5666c8
4 changed files with 68 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -185,6 +185,12 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA
}
public void afterPropertiesSet() {
if (this.handler == null) {
return;
}
if (this.handler instanceof ChannelRegistryAware) {
((ChannelRegistryAware) this.handler).setChannelRegistry(this.channelRegistry);
}
if (this.concurrencyPolicy != null || this.handler instanceof ConcurrentHandler) {
if (!(this.handler instanceof ConcurrentHandler)) {
int capacity = concurrencyPolicy.getQueueCapacity();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -60,6 +60,10 @@ public abstract class AbstractMessageHandlerAdapter<T> implements MessageHandler
this.object = object;
}
protected Object getObject() {
return this.object;
}
public void setMethodName(String methodName) {
Assert.notNull(methodName, "'methodName' must not be null");
this.methodName = methodName;
@@ -82,6 +86,7 @@ public abstract class AbstractMessageHandlerAdapter<T> implements MessageHandler
this.invoker = new SimpleMethodInvoker<T>(this.object, this.methodName);
this.initialized = true;
}
this.initialize();
}
public final Message<?> handle(Message<?> message) {
@@ -109,6 +114,12 @@ public abstract class AbstractMessageHandlerAdapter<T> implements MessageHandler
protected void validate() {
}
/**
* Subclasses may override this method to provide additional initialization.
*/
protected void initialize() {
}
protected Message<?> createReplyMessage(Object payload, MessageHeader originalMessageHeader) {
return new GenericMessage(payload, originalMessageHeader);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -65,6 +65,14 @@ public class RouterMessageHandlerAdapter extends AbstractMessageHandlerAdapter i
this.channelRegistry = channelRegistry;
}
@Override
protected void initialize() {
Object target = this.getObject();
if (target != null && this.channelRegistry != null && (target instanceof ChannelRegistryAware)) {
((ChannelRegistryAware) target).setChannelRegistry(this.channelRegistry);
}
}
@Override
protected Object doHandle(Message message, SimpleMethodInvoker invoker) {
if (method.getParameterTypes().length != 1) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -30,6 +30,7 @@ import org.junit.Test;
import org.springframework.integration.MessagingConfigurationException;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.ChannelRegistryAware;
import org.springframework.integration.channel.DefaultChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.SimpleChannel;
@@ -440,6 +441,26 @@ public class RouterMessageHandlerAdapterTests {
assertNull(result6);
}
@Test
public void testChannelRegistryAwareTarget() throws Exception {
SimpleChannel fooChannel = new SimpleChannel();
ChannelRegistry channelRegistry = new DefaultChannelRegistry();
channelRegistry.registerChannel("foo-channel", fooChannel);
ChannelRegistryAwareTestBean testBean = new ChannelRegistryAwareTestBean();
Method routingMethod = testBean.getClass().getMethod("route", String.class);
Map<String, Object> attribs = new ConcurrentHashMap<String, Object>();
RouterMessageHandlerAdapter adapter = new RouterMessageHandlerAdapter(testBean, routingMethod, attribs);
adapter.setChannelRegistry(channelRegistry);
assertNull(testBean.getChannelRegistry());
adapter.afterPropertiesSet();
assertNotNull(testBean.getChannelRegistry());
Message<String> message = new StringMessage("foo-channel");
adapter.handle(message);
Message<?> result = fooChannel.receive(0);
assertNotNull(result);
assertEquals("foo-channel", result.getPayload());
}
public static class SingleChannelNameRoutingTestBean {
@@ -552,4 +573,22 @@ public class RouterMessageHandlerAdapterTests {
}
}
public static class ChannelRegistryAwareTestBean implements ChannelRegistryAware {
private ChannelRegistry channelRegistry;
public void setChannelRegistry(ChannelRegistry channelRegistry) {
this.channelRegistry = channelRegistry;
}
public ChannelRegistry getChannelRegistry() {
return this.channelRegistry;
}
public MessageChannel route(String channelName) {
return this.channelRegistry.lookupChannel(channelName);
}
}
}