Removed the Subscription class.

This commit is contained in:
Mark Fisher
2008-07-04 22:39:18 +00:00
parent 96b22fd01b
commit 71da892bcb
11 changed files with 82 additions and 133 deletions

View File

@@ -45,6 +45,7 @@ import org.springframework.integration.channel.DefaultChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.factory.ChannelFactory;
import org.springframework.integration.channel.factory.QueueChannelFactory;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.endpoint.DefaultEndpointRegistry;
import org.springframework.integration.endpoint.EndpointRegistry;
import org.springframework.integration.endpoint.HandlerEndpoint;
@@ -60,7 +61,6 @@ import org.springframework.integration.scheduling.PollingSchedule;
import org.springframework.integration.scheduling.TaskScheduler;
import org.springframework.integration.scheduling.Schedule;
import org.springframework.integration.scheduling.SimpleTaskScheduler;
import org.springframework.integration.scheduling.Subscription;
import org.springframework.util.Assert;
/**
@@ -238,24 +238,34 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry,
return this.channelRegistry.unregisterChannel(name);
}
public void registerHandler(String name, MessageHandler handler, Subscription subscription) {
public void registerHandler(String name, MessageHandler handler, Object input, Schedule schedule) {
Assert.notNull(handler, "'handler' must not be null");
HandlerEndpoint endpoint = new HandlerEndpoint(handler);
endpoint.setName(name);
endpoint.setInputChannelName(subscription.getChannelName());
endpoint.setSchedule(subscription.getSchedule());
this.configureEndpoint(endpoint, name, input, schedule);
this.registerEndpoint(endpoint);
}
public void registerTarget(String name, MessageTarget target, Subscription subscription) {
public void registerTarget(String name, MessageTarget target, Object input, Schedule schedule) {
Assert.notNull(target, "'target' must not be null");
TargetEndpoint endpoint = new TargetEndpoint(target);
endpoint.setName(name);
endpoint.setInputChannelName(subscription.getChannelName());
endpoint.setSchedule(subscription.getSchedule());
this.configureEndpoint(endpoint, name, input, schedule);
this.registerEndpoint(endpoint);
}
private void configureEndpoint(AbstractEndpoint endpoint, String name, Object input, Schedule schedule) {
endpoint.setName(name);
if (input instanceof MessageChannel) {
endpoint.setInputChannel((MessageChannel) input);
}
else if (input instanceof String) {
endpoint.setInputChannelName((String) input);
}
else {
throw new ConfigurationException("'input' must be a MessageChannel or String");
}
endpoint.setSchedule(schedule);
}
public void registerEndpoint(MessageEndpoint endpoint) {
if (!this.initialized) {
this.initialize();

View File

@@ -27,15 +27,13 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Polled;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.scheduling.PollingSchedule;
import org.springframework.integration.scheduling.Subscription;
import org.springframework.integration.scheduling.Schedule;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* Base class for post-processing annotated methods.
@@ -99,20 +97,16 @@ public abstract class AbstractAnnotationMethodPostProcessor<T> implements Annota
return null;
}
protected Subscription createSubscription(final Object bean, final String beanName, MessageEndpoint annotation, Polled polledAnnotation) {
String channelName = annotation.input();
if (StringUtils.hasText(channelName)) {
PollingSchedule schedule = null;
if (polledAnnotation != null) {
schedule = new PollingSchedule(polledAnnotation.period());
schedule.setInitialDelay(polledAnnotation.initialDelay());
schedule.setFixedRate(polledAnnotation.fixedRate());
schedule.setTimeUnit(polledAnnotation.timeUnit());
}
Subscription subscription = new Subscription(channelName, schedule);
return subscription;
protected Schedule extractSchedule(Class<?> originalBeanClass) {
PollingSchedule schedule = null;
Polled polledAnnotation = AnnotationUtils.findAnnotation(originalBeanClass, Polled.class);
if (polledAnnotation != null) {
schedule = new PollingSchedule(polledAnnotation.period());
schedule.setInitialDelay(polledAnnotation.initialDelay());
schedule.setFixedRate(polledAnnotation.fixedRate());
schedule.setTimeUnit(polledAnnotation.timeUnit());
}
return null;
return schedule;
}

View File

@@ -32,7 +32,6 @@ import org.springframework.integration.ConfigurationException;
import org.springframework.integration.annotation.Aggregator;
import org.springframework.integration.annotation.Concurrency;
import org.springframework.integration.annotation.Handler;
import org.springframework.integration.annotation.Polled;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.annotation.Splitter;
import org.springframework.integration.annotation.Transformer;
@@ -49,7 +48,7 @@ import org.springframework.integration.handler.config.MessageHandlerCreator;
import org.springframework.integration.router.config.AggregatorMessageHandlerCreator;
import org.springframework.integration.router.config.RouterMessageHandlerCreator;
import org.springframework.integration.router.config.SplitterMessageHandlerCreator;
import org.springframework.integration.scheduling.Subscription;
import org.springframework.integration.scheduling.Schedule;
import org.springframework.integration.transformer.config.TransformerMessageHandlerCreator;
import org.springframework.util.StringUtils;
@@ -137,11 +136,13 @@ public class HandlerAnnotationPostProcessor extends AbstractAnnotationMethodPost
if (StringUtils.hasText(outputChannelName)) {
endpoint.setOutputChannelName(outputChannelName);
}
Polled polledAnnotation = AnnotationUtils.findAnnotation(originalBeanClass, Polled.class);
Subscription subscription = this.createSubscription(bean, beanName, endpointAnnotation, polledAnnotation);
if (subscription != null) {
endpoint.setSchedule(subscription.getSchedule());
endpoint.setInputChannelName(subscription.getChannelName());
String inputChannelName = endpointAnnotation.input();
if (StringUtils.hasText(inputChannelName)) {
endpoint.setInputChannelName(inputChannelName);
}
Schedule schedule = this.extractSchedule(originalBeanClass);
if (schedule != null) {
endpoint.setSchedule(schedule);
}
Concurrency concurrencyAnnotation = AnnotationUtils.findAnnotation(originalBeanClass, Concurrency.class);
if (concurrencyAnnotation != null) {

View File

@@ -29,7 +29,6 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.integration.annotation.Subscriber;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
import org.springframework.integration.scheduling.Subscription;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
@@ -92,8 +91,7 @@ public class SubscriberAnnotationPostProcessor implements BeanPostProcessor {
adapter.afterPropertiesSet();
String adapterName = ClassUtils.getShortNameAsProperty(targetClass) +
"-" + method.getName() + "-endpoint";
Subscription subscription = new Subscription(channelName);
messageBus.registerHandler(adapterName, adapter, subscription);
messageBus.registerHandler(adapterName, adapter, channelName, null);
}
}
});

View File

@@ -23,7 +23,6 @@ import java.util.List;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.annotation.Concurrency;
import org.springframework.integration.annotation.Polled;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.endpoint.ConcurrencyPolicy;
import org.springframework.integration.endpoint.MessageEndpoint;
@@ -31,7 +30,8 @@ import org.springframework.integration.endpoint.TargetEndpoint;
import org.springframework.integration.endpoint.interceptor.ConcurrencyInterceptor;
import org.springframework.integration.handler.MethodInvokingTarget;
import org.springframework.integration.message.MessageTarget;
import org.springframework.integration.scheduling.Subscription;
import org.springframework.integration.scheduling.Schedule;
import org.springframework.util.StringUtils;
/**
* Post-processor for classes annotated with {@link MessageTarget @MessageTarget}.
@@ -62,10 +62,14 @@ public class TargetAnnotationPostProcessor extends AbstractAnnotationMethodPostP
public MessageEndpoint createEndpoint(Object bean, String beanName, Class<?> originalBeanClass,
org.springframework.integration.annotation.MessageEndpoint endpointAnnotation) {
TargetEndpoint endpoint = new TargetEndpoint((MessageTarget) bean);
Polled polledAnnotation = AnnotationUtils.findAnnotation(originalBeanClass, Polled.class);
Subscription subscription = this.createSubscription(bean, beanName, endpointAnnotation, polledAnnotation);
endpoint.setSchedule(subscription.getSchedule());
endpoint.setInputChannelName(subscription.getChannelName());
String inputChannelName = endpointAnnotation.input();
if (StringUtils.hasText(inputChannelName)) {
endpoint.setInputChannelName(inputChannelName);
}
Schedule schedule = this.extractSchedule(originalBeanClass);
if (schedule != null) {
endpoint.setSchedule(schedule);
}
Concurrency concurrencyAnnotation = AnnotationUtils.findAnnotation(originalBeanClass, Concurrency.class);
if (concurrencyAnnotation != null) {
ConcurrencyPolicy concurrencyPolicy = new ConcurrencyPolicy(concurrencyAnnotation.coreSize(),

View File

@@ -1,71 +0,0 @@
/*
* Copyright 2002-2007 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.scheduling;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.util.Assert;
/**
* Configuration metadata for activating a subscription. Immutable.
*
* @author Mark Fisher
*/
public class Subscription {
private final MessageChannel channel;
private final String channelName;
private final Schedule schedule;
public Subscription(MessageChannel channel) {
this(channel, null);
}
public Subscription(String channelName) {
this(channelName, null);
}
public Subscription(MessageChannel channel, Schedule schedule) {
Assert.notNull(channel, "'channel' must not be null");
this.channel = channel;
this.schedule = schedule;
this.channelName = this.channel.getName();
}
public Subscription(String channelName, Schedule schedule) {
Assert.notNull(channelName, "'channelName' must not be null");
this.channelName = channelName;
this.schedule = schedule;
this.channel = null;
}
public MessageChannel getChannel() {
return this.channel;
}
public String getChannelName() {
return (this.channel != null) ? this.channel.getName() : this.channelName;
}
public Schedule getSchedule() {
return this.schedule;
}
}

View File

@@ -39,7 +39,6 @@ import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageSource;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.scheduling.PollingSchedule;
import org.springframework.integration.scheduling.Subscription;
/**
* @author Mark Fisher
@@ -47,7 +46,7 @@ import org.springframework.integration.scheduling.Subscription;
public class MessageBusTests {
@Test
public void testOutputChannel() {
public void testRegistrationWithInputChannelReference() {
MessageBus bus = new MessageBus();
MessageChannel sourceChannel = new QueueChannel();
MessageChannel targetChannel = new QueueChannel();
@@ -61,8 +60,29 @@ public class MessageBusTests {
return message;
}
};
Subscription subscription = new Subscription(sourceChannel);
bus.registerHandler("handler", handler, subscription);
bus.registerHandler("handler", handler, sourceChannel, null);
bus.start();
Message<?> result = targetChannel.receive(3000);
assertEquals("test", result.getPayload());
bus.stop();
}
@Test
public void testRegistrationWithInputChannelName() {
MessageBus bus = new MessageBus();
MessageChannel sourceChannel = new QueueChannel();
MessageChannel targetChannel = new QueueChannel();
bus.registerChannel("sourceChannel", sourceChannel);
StringMessage message = new StringMessage("test");
message.getHeader().setReturnAddress("targetChannel");
sourceChannel.send(message);
bus.registerChannel("targetChannel", targetChannel);
MessageHandler handler = new MessageHandler() {
public Message<?> handle(Message<?> message) {
return message;
}
};
bus.registerHandler("handler", handler, "sourceChannel", null);
bus.start();
Message<?> result = targetChannel.receive(3000);
assertEquals("test", result.getPayload());
@@ -117,8 +137,8 @@ public class MessageBusTests {
bus.registerChannel("input", inputChannel);
bus.registerChannel("output1", outputChannel1);
bus.registerChannel("output2", outputChannel2);
bus.registerHandler("handler1", handler1, new Subscription(inputChannel));
bus.registerHandler("handler2", handler2, new Subscription(inputChannel));
bus.registerHandler("handler1", handler1, inputChannel, null);
bus.registerHandler("handler2", handler2, inputChannel, null);
bus.start();
inputChannel.send(new StringMessage(1, "testing"));
Message<?> message1 = outputChannel1.receive(100);
@@ -151,8 +171,8 @@ public class MessageBusTests {
bus.registerChannel("input", inputChannel);
bus.registerChannel("output1", outputChannel1);
bus.registerChannel("output2", outputChannel2);
bus.registerHandler("handler1", handler1, new Subscription(inputChannel));
bus.registerHandler("handler2", handler2, new Subscription(inputChannel));
bus.registerHandler("handler1", handler1, inputChannel, null);
bus.registerHandler("handler2", handler2, inputChannel, null);
bus.start();
inputChannel.send(new StringMessage(1, "testing"));
latch.await(500, TimeUnit.MILLISECONDS);
@@ -207,7 +227,7 @@ public class MessageBusTests {
return null;
}
};
bus.registerHandler("testHandler", handler, new Subscription(MessageBus.ERROR_CHANNEL_NAME));
bus.registerHandler("testHandler", handler, MessageBus.ERROR_CHANNEL_NAME, null);
bus.start();
errorChannel.send(new ErrorMessage(new RuntimeException("test-exception")));
latch.await(1000, TimeUnit.MILLISECONDS);

View File

@@ -40,7 +40,6 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.dispatcher.DirectChannel;
import org.springframework.integration.handler.TestHandlers;
import org.springframework.integration.scheduling.SimpleTaskScheduler;
import org.springframework.integration.scheduling.Subscription;
/**
* @author Mark Fisher
@@ -71,8 +70,7 @@ public class MessageBusParserTests {
ApplicationContext context = new ClassPathXmlApplicationContext(
"messageBusWithDefaults.xml", this.getClass());
MessageBus bus = (MessageBus) context.getBean(MessageBusParser.MESSAGE_BUS_BEAN_NAME);
Subscription subscription = new Subscription("unknownChannel");
bus.registerHandler("handler", TestHandlers.nullHandler(), subscription);
bus.registerHandler("handler", TestHandlers.nullHandler(), "unknownChannel", null);
}
@Test
@@ -80,8 +78,7 @@ public class MessageBusParserTests {
ApplicationContext context = new ClassPathXmlApplicationContext(
"messageBusWithAutoCreateChannels.xml", this.getClass());
MessageBus bus = (MessageBus) context.getBean(MessageBusParser.MESSAGE_BUS_BEAN_NAME);
Subscription subscription = new Subscription("channelToCreate");
bus.registerHandler("handler", TestHandlers.nullHandler(), subscription);
bus.registerHandler("handler", TestHandlers.nullHandler(), "channelToCreate", null);
bus.start();
assertNotNull(bus.lookupChannel("channelToCreate"));
bus.stop();

View File

@@ -33,7 +33,6 @@ import org.springframework.integration.handler.ReplyHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageHeader;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.scheduling.Subscription;
/**
* @author Mark Fisher
@@ -51,7 +50,7 @@ public class RequestReplyTemplateTests {
};
MessageBus bus = new MessageBus();
bus.registerChannel("requestChannel", requestChannel);
bus.registerHandler("testHandler", testHandler, new Subscription(requestChannel));
bus.registerHandler("testHandler", testHandler, requestChannel, null);
bus.start();
}

View File

@@ -34,7 +34,6 @@ import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.scheduling.Subscription;
/**
* @author Mark Fisher
@@ -88,13 +87,12 @@ public class MethodInvokingTargetTests {
target.setMethodName("foo");
target.afterPropertiesSet();
QueueChannel channel = new QueueChannel();
Subscription subscription = new Subscription(channel);
Message<String> message = new GenericMessage<String>("123", "testing");
channel.send(message);
assertNull(queue.poll());
MessageBus bus = new MessageBus();
bus.registerChannel("channel", channel);
bus.registerHandler("targetAdapter", target, subscription);
bus.registerHandler("targetAdapter", target, channel, null);
bus.start();
String result = queue.poll(500, TimeUnit.MILLISECONDS);
assertNotNull(result);