From 0f6bd60ee09a55f56e93dfb147c5924b53fb73fc Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 17 Apr 2008 17:25:56 +0000 Subject: [PATCH] Added Target interface and split DefaultMessageEndpoint into TargetEndpoint and HandlerEndpoint. All "one-way" adapters now implement Target instead of MessageHandler, the ConcurrentHandler is now ConcurrentTarget, and the MessageDispatcher also operates on Targets rather than MessageHandlers. --- .../event/ApplicationEventTargetAdapter.java | 24 ++- .../adapter/event/MessagingEventMapper.java | 18 +- .../adapter/file/AbstractFileMapper.java | 9 +- .../integration/adapter/file/FileSource.java | 6 +- .../adapter/file/FileTargetAdapter.java | 20 ++- .../file/config/FileTargetAdapterParser.java | 6 +- .../integration/adapter/ftp/FtpSource.java | 12 +- .../HttpInvokerTargetAdapterParser.java | 4 +- .../adapter/jms/JmsTargetAdapter.java | 13 +- .../jms/config/JmsTargetAdapterParser.java | 6 +- .../mail/ByteArrayMailMessageMapper.java | 6 +- .../adapter/mail/MailTargetAdapter.java | 20 +-- .../adapter/mail/TextMailMessageMapper.java | 8 +- .../mail/config/MailTargetAdapterParser.java | 4 +- .../rmi/config/RmiTargetAdapterParser.java | 4 +- .../stream/ByteStreamTargetAdapter.java | 25 +-- .../stream/CharacterStreamTargetAdapter.java | 32 ++-- .../ApplicationEventTargetAdapterTests.java | 3 +- .../config/FileTargetAdapterParserTests.java | 6 +- .../HttpInvokerTargetAdapterParserTests.java | 4 +- .../config/JmsTargetAdapterParserTests.java | 16 +- .../mail/MailTargetAdapterContextTests.java | 6 +- .../adapter/mail/MailTargetAdapterTests.java | 6 +- .../config/MailTargetAdapterParserTests.java | 32 ++-- .../config/RmiTargetAdapterParserTests.java | 4 +- .../stream/ByteStreamTargetAdapterTests.java | 20 +-- .../CharacterStreamTargetAdapterTests.java | 16 +- .../adapter/AbstractTargetAdapter.java | 56 ------ .../adapter/DefaultTargetAdapter.java | 42 ----- .../adapter/MethodInvokingHandler.java | 77 ++++++++ .../adapter/MethodInvokingTarget.java | 61 ++----- .../aop/MessagePublishingInterceptor.java | 28 +-- .../integration/bus/MessageBus.java | 62 ++++--- .../config/ChannelAdapterParser.java | 25 +-- .../integration/config/EndpointParser.java | 4 +- ...essageEndpointAnnotationPostProcessor.java | 26 +-- .../dispatcher/DefaultMessageDispatcher.java | 58 +++--- .../dispatcher/DefaultMessageDistributor.java | 34 ++-- .../dispatcher/MessageDispatcher.java | 8 +- .../dispatcher/MessageDistributor.java | 9 +- .../SchedulingMessageDispatcher.java | 8 +- .../dispatcher/SynchronousChannel.java | 9 +- ...rentHandler.java => ConcurrentTarget.java} | 41 ++--- .../integration/endpoint/HandlerEndpoint.java | 163 +++++++++++++++++ .../integration/endpoint/MessageEndpoint.java | 8 +- ...ssageEndpoint.java => TargetEndpoint.java} | 159 +++++------------ .../AbstractMessageHandlerAdapter.java | 3 +- .../message/AbstractMessageMapper.java | 43 ----- .../MessageCreator.java} | 10 +- .../integration/message/MessageMapper.java | 13 +- .../message/SimplePayloadMessageMapper.java | 41 ----- .../{adapter => message}/Target.java | 11 +- .../adapter/DefaultTargetAdapterTests.java | 83 --------- .../adapter/MethodInvokingTargetTests.java | 77 ++++++-- .../integration/adapter/TestSink.java | 4 +- .../integration/adapter/adapterTests.xml | 14 +- .../SynchronousChannelSubscriptionTests.java | 6 +- .../integration/bus/messageBusTests.xml | 2 +- .../config/AggregatorAnnotationTests.java | 4 +- .../config/ChannelParserTests.java | 34 ++-- .../CompletionStrategyAnnotationTests.java | 4 +- .../config/EndpointParserTests.java | 25 ++- .../DefaultMessageDispatcherTests.java | 167 ++++++++---------- .../DefaultMessageDistributorTests.java | 22 ++- .../dispatcher/SynchronousChannelTests.java | 16 +- ...ntTests.java => HandlerEndpointTests.java} | 125 +++++++------ ...eEndpointAnnotationPostProcessorTests.java | 4 +- .../config/WebServiceTargetAdapterParser.java | 4 +- .../WebServiceTargetAdapterParserTests.java | 18 +- 69 files changed, 930 insertions(+), 1008 deletions(-) delete mode 100644 spring-integration-core/src/main/java/org/springframework/integration/adapter/AbstractTargetAdapter.java delete mode 100644 spring-integration-core/src/main/java/org/springframework/integration/adapter/DefaultTargetAdapter.java create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingHandler.java rename spring-integration-core/src/main/java/org/springframework/integration/endpoint/{ConcurrentHandler.java => ConcurrentTarget.java} (69%) create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/endpoint/HandlerEndpoint.java rename spring-integration-core/src/main/java/org/springframework/integration/endpoint/{DefaultMessageEndpoint.java => TargetEndpoint.java} (51%) delete mode 100644 spring-integration-core/src/main/java/org/springframework/integration/message/AbstractMessageMapper.java rename spring-integration-core/src/main/java/org/springframework/integration/{adapter/ArgumentListPreparer.java => message/MessageCreator.java} (70%) delete mode 100644 spring-integration-core/src/main/java/org/springframework/integration/message/SimplePayloadMessageMapper.java rename spring-integration-core/src/main/java/org/springframework/integration/{adapter => message}/Target.java (70%) delete mode 100644 spring-integration-core/src/test/java/org/springframework/integration/adapter/DefaultTargetAdapterTests.java rename spring-integration-core/src/test/java/org/springframework/integration/endpoint/{DefaultMessageEndpointTests.java => HandlerEndpointTests.java} (83%) diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/event/ApplicationEventTargetAdapter.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/event/ApplicationEventTargetAdapter.java index ba0c846bb8..3abb1b3d9d 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/event/ApplicationEventTargetAdapter.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/event/ApplicationEventTargetAdapter.java @@ -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. @@ -16,35 +16,33 @@ package org.springframework.integration.adapter.event; +import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; -import org.springframework.integration.adapter.AbstractTargetAdapter; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageMapper; +import org.springframework.integration.message.Target; /** * A target adapter for publishing {@link MessagingEvent MessagingEvents}. The * {@link MessagingEvent} is a subclass of Spring's {@link ApplicationEvent} - * used by this adapter to wrap any {@link Message} received on its channel. + * used by this adapter to wrap any {@link Message} sent to this target. * * @author Mark Fisher */ -public class ApplicationEventTargetAdapter extends AbstractTargetAdapter implements - ApplicationEventPublisherAware { +public class ApplicationEventTargetAdapter implements Target, ApplicationEventPublisherAware { + + private final MessageMapper> mapper = new MessagingEventMapper(); private ApplicationEventPublisher applicationEventPublisher; - public ApplicationEventTargetAdapter() { - this.setMessageMapper(new MessagingEventMapper()); - } - - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } - @Override - protected boolean sendToTarget(MessagingEvent event) { - this.applicationEventPublisher.publishEvent(event); + public boolean send(Message message) { + this.applicationEventPublisher.publishEvent(this.mapper.mapMessage((Message) message)); return true; } diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/event/MessagingEventMapper.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/event/MessagingEventMapper.java index bb8ebf539c..47424ca665 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/event/MessagingEventMapper.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/event/MessagingEventMapper.java @@ -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. @@ -17,22 +17,22 @@ package org.springframework.integration.adapter.event; import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageCreator; import org.springframework.integration.message.MessageMapper; /** - * A {@link MessageMapper} implementation for mapping to and from - * {@link MessagingEvent MessagingEvents}. + * Maps between {@link Message Messages} and {@link MessagingEvent MessagingEvents}. * * @author Mark Fisher */ -public class MessagingEventMapper implements MessageMapper> { +public class MessagingEventMapper implements MessageCreator, T>, MessageMapper> { - public MessagingEvent fromMessage(Message message) { - return new MessagingEvent(message); - } - - public Message toMessage(MessagingEvent event) { + public Message createMessage(MessagingEvent event) { return event.getMessage(); } + public MessagingEvent mapMessage(Message message) { + return new MessagingEvent(message); + } + } diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/AbstractFileMapper.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/AbstractFileMapper.java index 23b9435d6a..cec4654cd4 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/AbstractFileMapper.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/AbstractFileMapper.java @@ -23,10 +23,11 @@ import java.io.FileWriter; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.integration.message.AbstractMessageMapper; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageCreator; import org.springframework.integration.message.MessageHandlingException; +import org.springframework.integration.message.MessageMapper; import org.springframework.integration.message.MessagingException; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; @@ -36,7 +37,7 @@ import org.springframework.util.FileCopyUtils; * * @author Mark Fisher */ -public abstract class AbstractFileMapper extends AbstractMessageMapper { +public abstract class AbstractFileMapper implements MessageCreator, MessageMapper { protected Log logger = LogFactory.getLog(this.getClass()); @@ -60,7 +61,7 @@ public abstract class AbstractFileMapper extends AbstractMessageMapper message) { + public File mapMessage(Message message) { try { File file = new File(parentDirectory, this.fileNameGenerator.generateFileName(message)); this.writeToFile(file, message.getPayload()); @@ -71,7 +72,7 @@ public abstract class AbstractFileMapper extends AbstractMessageMapper toMessage(File file) { + public Message createMessage(File file) { try { T payload = this.readMessagePayload(file); if (payload == null) { diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/FileSource.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/FileSource.java index 9bcd8c52c7..24eb59e3bf 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/FileSource.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/FileSource.java @@ -37,7 +37,7 @@ public class FileSource implements PollableSource, InitializingBean { private volatile boolean textBased = true; - private volatile AbstractFileMapper mapper; + private volatile AbstractFileMapper mapper; private volatile FileNameGenerator fileNameGenerator; @@ -84,7 +84,7 @@ public class FileSource implements PollableSource, InitializingBean { } } - public Message poll() { + public Message poll() { File[] files = null; if (this.fileFilter != null) { files = this.directory.listFiles(this.fileFilter); @@ -101,7 +101,7 @@ public class FileSource implements PollableSource, InitializingBean { } for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { - return this.mapper.toMessage(files[i]); + return this.mapper.createMessage(files[i]); } } return null; diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/FileTargetAdapter.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/FileTargetAdapter.java index 7afead73b1..97f1b4d2cd 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/FileTargetAdapter.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/FileTargetAdapter.java @@ -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. @@ -18,8 +18,8 @@ package org.springframework.integration.adapter.file; import java.io.File; -import org.springframework.integration.adapter.AbstractTargetAdapter; -import org.springframework.integration.message.MessageMapper; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.Target; import org.springframework.util.Assert; /** @@ -28,7 +28,10 @@ import org.springframework.util.Assert; * * @author Mark Fisher */ -public class FileTargetAdapter extends AbstractTargetAdapter { +public class FileTargetAdapter implements Target { + + private AbstractFileMapper mapper; + public FileTargetAdapter(File directory) { this(directory, true); @@ -36,23 +39,22 @@ public class FileTargetAdapter extends AbstractTargetAdapter { public FileTargetAdapter(File directory, boolean isTextBased) { if (isTextBased) { - this.setMessageMapper(new TextFileMapper(directory)); + this.mapper = new TextFileMapper(directory); } else { - this.setMessageMapper(new ByteArrayFileMapper(directory)); + this.mapper = new ByteArrayFileMapper(directory); } } public void setFileNameGenerator(FileNameGenerator fileNameGenerator) { Assert.notNull(fileNameGenerator, "'fileNameGenerator' must not be null"); - MessageMapper mapper = this.getMessageMapper(); if (mapper instanceof AbstractFileMapper) { ((AbstractFileMapper) mapper).setFileNameGenerator(fileNameGenerator); } } - @Override - protected boolean sendToTarget(File file) { + public boolean send(Message message) { + File file = this.mapper.mapMessage(message); return file.exists(); } diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/config/FileTargetAdapterParser.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/config/FileTargetAdapterParser.java index 1b48d9f9d3..cca236b4b5 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/config/FileTargetAdapterParser.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/file/config/FileTargetAdapterParser.java @@ -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. @@ -24,7 +24,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.adapter.file.FileTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.TargetEndpoint; import org.springframework.integration.scheduling.Subscription; /** @@ -35,7 +35,7 @@ import org.springframework.integration.scheduling.Subscription; public class FileTargetAdapterParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { - return DefaultMessageEndpoint.class; + return TargetEndpoint.class; } protected boolean shouldGenerateId() { diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/ftp/FtpSource.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/ftp/FtpSource.java index 545b043b41..a2e1707fc6 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/ftp/FtpSource.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/ftp/FtpSource.java @@ -32,8 +32,8 @@ import org.springframework.integration.adapter.file.ByteArrayFileMapper; import org.springframework.integration.adapter.file.FileNameGenerator; import org.springframework.integration.adapter.file.TextFileMapper; import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageCreator; import org.springframework.integration.message.MessageDeliveryAware; -import org.springframework.integration.message.MessageMapper; import org.springframework.integration.message.MessagingException; import org.springframework.integration.message.PollableSource; import org.springframework.util.Assert; @@ -70,7 +70,7 @@ public class FtpSource implements PollableSource, MessageDeliveryAware { private volatile boolean textBased = true; - private volatile MessageMapper mapper; + private volatile MessageCreator messageCreator; private final DirectoryContentManager directoryContentManager = new DirectoryContentManager(); @@ -113,14 +113,14 @@ public class FtpSource implements PollableSource, MessageDeliveryAware { public void afterPropertiesSet() { if (this.isTextBased()) { - this.mapper = new TextFileMapper(this.localWorkingDirectory); + this.messageCreator = new TextFileMapper(this.localWorkingDirectory); } else { - this.mapper = new ByteArrayFileMapper(this.localWorkingDirectory); + this.messageCreator = new ByteArrayFileMapper(this.localWorkingDirectory); } } - public final Message poll() { + public final Message poll() { try { this.establishConnection(); FTPFile[] fileList = this.client.listFiles(); @@ -143,7 +143,7 @@ public class FtpSource implements PollableSource, MessageDeliveryAware { FileOutputStream fileOutputStream = new FileOutputStream(file); this.client.retrieveFile(fileName, fileOutputStream); fileOutputStream.close(); - return this.mapper.toMessage(file); + return this.messageCreator.createMessage(file); } catch (Exception e) { try { diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/httpinvoker/config/HttpInvokerTargetAdapterParser.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/httpinvoker/config/HttpInvokerTargetAdapterParser.java index f1cbc90a14..ae6ea16d0b 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/httpinvoker/config/HttpInvokerTargetAdapterParser.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/httpinvoker/config/HttpInvokerTargetAdapterParser.java @@ -25,7 +25,7 @@ import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.ConfigurationException; import org.springframework.integration.adapter.httpinvoker.HttpInvokerTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.scheduling.Subscription; import org.springframework.util.StringUtils; @@ -37,7 +37,7 @@ import org.springframework.util.StringUtils; public class HttpInvokerTargetAdapterParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { - return DefaultMessageEndpoint.class; + return HandlerEndpoint.class; } protected boolean shouldGenerateId() { diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/jms/JmsTargetAdapter.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/jms/JmsTargetAdapter.java index 75ce0d4606..db2c98bd6f 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/jms/JmsTargetAdapter.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/jms/JmsTargetAdapter.java @@ -19,8 +19,8 @@ package org.springframework.integration.adapter.jms; import javax.jms.ConnectionFactory; import javax.jms.Destination; -import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.Message; +import org.springframework.integration.message.Target; import org.springframework.jms.core.JmsTemplate; /** @@ -28,7 +28,7 @@ import org.springframework.jms.core.JmsTemplate; * * @author Mark Fisher */ -public class JmsTargetAdapter extends AbstractJmsTemplateBasedAdapter implements MessageHandler { +public class JmsTargetAdapter extends AbstractJmsTemplateBasedAdapter implements Target { public JmsTargetAdapter(JmsTemplate jmsTemplate) { super(jmsTemplate); @@ -47,11 +47,12 @@ public class JmsTargetAdapter extends AbstractJmsTemplateBasedAdapter implements } - public final Message handle(final Message message) { - if (message != null) { - this.getJmsTemplate().convertAndSend(message); + public final boolean send(final Message message) { + if (message == null) { + throw new IllegalArgumentException("message must not be null"); } - return null; + this.getJmsTemplate().convertAndSend(message); + return true; } } diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParser.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParser.java index 4d8125cfd5..4a22ed9f4c 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParser.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParser.java @@ -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. @@ -26,7 +26,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.adapter.jms.JmsTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.TargetEndpoint; import org.springframework.integration.scheduling.Subscription; import org.springframework.util.StringUtils; @@ -41,7 +41,7 @@ public class JmsTargetAdapterParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { - return DefaultMessageEndpoint.class; + return TargetEndpoint.class; } protected boolean shouldGenerateId() { diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/ByteArrayMailMessageMapper.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/ByteArrayMailMessageMapper.java index 8643cece9c..d7a93ce0bb 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/ByteArrayMailMessageMapper.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/ByteArrayMailMessageMapper.java @@ -21,8 +21,8 @@ import javax.mail.internet.MimeMessage; import org.springframework.core.io.ByteArrayResource; import org.springframework.integration.adapter.MessageMappingException; -import org.springframework.integration.message.AbstractMessageMapper; import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageMapper; import org.springframework.mail.MailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMailMessage; @@ -36,7 +36,7 @@ import org.springframework.util.Assert; * * @author Marius Bogoevici */ -public class ByteArrayMailMessageMapper extends AbstractMessageMapper { +public class ByteArrayMailMessageMapper implements MessageMapper { private final JavaMailSender mailSender; @@ -63,7 +63,7 @@ public class ByteArrayMailMessageMapper extends AbstractMessageMapper message) { + public MailMessage mapMessage(Message message) { try { MimeMessage mimeMessage = this.mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, this.multipartMode); diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailTargetAdapter.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailTargetAdapter.java index 9457839593..22e89db3d6 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailTargetAdapter.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/MailTargetAdapter.java @@ -17,10 +17,9 @@ package org.springframework.integration.adapter.mail; import org.springframework.beans.factory.InitializingBean; -import org.springframework.integration.handler.MessageHandler; -import org.springframework.integration.message.AbstractMessageMapper; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessageMapper; +import org.springframework.integration.message.Target; import org.springframework.mail.MailMessage; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; @@ -33,7 +32,7 @@ import org.springframework.util.Assert; * @author Marius Bogoevici * @author Mark Fisher */ -public class MailTargetAdapter implements MessageHandler, InitializingBean { +public class MailTargetAdapter implements Target, InitializingBean { private final JavaMailSender mailSender; @@ -84,22 +83,22 @@ public class MailTargetAdapter implements MessageHandler, InitializingBean { this.objectMessageMapper = objectMessageMapper; } - public Message handle(Message message) { + public final boolean send(Message message) { MailMessage mailMessage = this.convertMessageToMailMessage(message); this.mailHeaderGenerator.populateMailMessageHeader(mailMessage, message); this.sendMailMessage(mailMessage); - return null; + return true; } @SuppressWarnings("unchecked") private MailMessage convertMessageToMailMessage(Message message) { if (message.getPayload() instanceof String) { - return this.textMessageMapper.fromMessage((Message) message); + return this.textMessageMapper.mapMessage((Message) message); } else if (message.getPayload() instanceof byte[]) { - return this.byteArrayMessageMapper.fromMessage((Message) message); + return this.byteArrayMessageMapper.mapMessage((Message) message); } - return this.objectMessageMapper.fromMessage((Message) message); + return this.objectMessageMapper.mapMessage((Message) message); } private void sendMailMessage(MailMessage mailMessage) { @@ -116,17 +115,18 @@ public class MailTargetAdapter implements MessageHandler, InitializingBean { } - private static class DefaultObjectMailMessageMapper extends AbstractMessageMapper { + private static class DefaultObjectMailMessageMapper implements MessageMapper { public Message toMessage(MailMessage source) { throw new UnsupportedOperationException("mapping from MailMessage to Object not supported"); } - public MailMessage fromMessage(Message objectMessage) { + public MailMessage mapMessage(Message objectMessage) { SimpleMailMessage message = new SimpleMailMessage(); message.setText(objectMessage.getPayload().toString()); return message; } + } } diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/TextMailMessageMapper.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/TextMailMessageMapper.java index 7085f7e9be..e6887a651d 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/TextMailMessageMapper.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/TextMailMessageMapper.java @@ -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. @@ -16,8 +16,8 @@ package org.springframework.integration.adapter.mail; -import org.springframework.integration.message.AbstractMessageMapper; import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageMapper; import org.springframework.integration.message.StringMessage; import org.springframework.mail.MailMessage; import org.springframework.mail.SimpleMailMessage; @@ -30,14 +30,14 @@ import org.springframework.util.Assert; * * @author Marius Bogoevici */ -public class TextMailMessageMapper extends AbstractMessageMapper { +public class TextMailMessageMapper implements MessageMapper { public Message toMessage(MailMessage source) { Assert.isInstanceOf(SimpleMailMessage.class, source, "source must be a SimpleMailMessage"); return new StringMessage(((SimpleMailMessage) source).getText()); } - public MailMessage fromMessage(Message stringMessage) { + public MailMessage mapMessage(Message stringMessage) { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setText(stringMessage.getPayload()); return mailMessage; diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/config/MailTargetAdapterParser.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/config/MailTargetAdapterParser.java index 02a63c9ec8..fd67fafba6 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/config/MailTargetAdapterParser.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/mail/config/MailTargetAdapterParser.java @@ -26,7 +26,7 @@ import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.ConfigurationException; import org.springframework.integration.adapter.mail.MailTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.TargetEndpoint; import org.springframework.integration.scheduling.Subscription; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.util.StringUtils; @@ -39,7 +39,7 @@ import org.springframework.util.StringUtils; public class MailTargetAdapterParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { - return DefaultMessageEndpoint.class; + return TargetEndpoint.class; } protected boolean shouldGenerateId() { diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/rmi/config/RmiTargetAdapterParser.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/rmi/config/RmiTargetAdapterParser.java index f11a8b3d47..829c068e11 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/rmi/config/RmiTargetAdapterParser.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/rmi/config/RmiTargetAdapterParser.java @@ -26,7 +26,7 @@ import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.ConfigurationException; import org.springframework.integration.adapter.rmi.RmiSourceAdapter; import org.springframework.integration.adapter.rmi.RmiTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.scheduling.Subscription; import org.springframework.util.StringUtils; @@ -38,7 +38,7 @@ import org.springframework.util.StringUtils; public class RmiTargetAdapterParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { - return DefaultMessageEndpoint.class; + return HandlerEndpoint.class; } protected boolean shouldGenerateId() { diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/stream/ByteStreamTargetAdapter.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/stream/ByteStreamTargetAdapter.java index d19e1411f0..dd3db4af71 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/stream/ByteStreamTargetAdapter.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/stream/ByteStreamTargetAdapter.java @@ -20,15 +20,21 @@ import java.io.BufferedOutputStream; import java.io.IOException; import java.io.OutputStream; -import org.springframework.integration.adapter.AbstractTargetAdapter; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.integration.message.Message; import org.springframework.integration.message.MessagingException; +import org.springframework.integration.message.Target; /** * A target adapter that writes a byte array to an {@link OutputStream}. * * @author Mark Fisher */ -public class ByteStreamTargetAdapter extends AbstractTargetAdapter { +public class ByteStreamTargetAdapter implements Target { + + private final Log logger = LogFactory.getLog(this.getClass()); private BufferedOutputStream stream; @@ -46,21 +52,20 @@ public class ByteStreamTargetAdapter extends AbstractTargetAdapter { } } - - @Override - protected boolean sendToTarget(Object object) { - if (object == null) { + public boolean send(Message message) { + Object payload = message.getPayload(); + if (payload == null) { if (logger.isWarnEnabled()) { logger.warn(this.getClass().getSimpleName() + " received null object"); } return false; } try { - if (object instanceof String) { - this.stream.write(((String) object).getBytes()); + if (payload instanceof String) { + this.stream.write(((String) payload).getBytes()); } - else if (object instanceof byte[]){ - this.stream.write((byte[]) object); + else if (payload instanceof byte[]){ + this.stream.write((byte[]) payload); } else { throw new MessagingException(this.getClass().getSimpleName() + diff --git a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/stream/CharacterStreamTargetAdapter.java b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/stream/CharacterStreamTargetAdapter.java index 615fac0b3a..85bfa09061 100644 --- a/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/stream/CharacterStreamTargetAdapter.java +++ b/spring-integration-adapters/src/main/java/org/springframework/integration/adapter/stream/CharacterStreamTargetAdapter.java @@ -23,9 +23,13 @@ import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.integration.ConfigurationException; -import org.springframework.integration.adapter.AbstractTargetAdapter; +import org.springframework.integration.message.Message; import org.springframework.integration.message.MessagingException; +import org.springframework.integration.message.Target; import org.springframework.util.Assert; /** @@ -38,7 +42,9 @@ import org.springframework.util.Assert; * * @author Mark Fisher */ -public class CharacterStreamTargetAdapter extends AbstractTargetAdapter { +public class CharacterStreamTargetAdapter implements Target { + + private final Log logger = LogFactory.getLog(this.getClass()); private final BufferedWriter writer; @@ -112,26 +118,26 @@ public class CharacterStreamTargetAdapter extends AbstractTargetAdapter { this.shouldAppendNewLine = shouldAppendNewLine; } - @Override - protected boolean sendToTarget(Object object) { - if (object == null) { + public boolean send(Message message) { + Object payload = message.getPayload(); + if (payload == null) { if (logger.isWarnEnabled()) { - logger.warn("target adapter received null object"); + logger.warn("target adapter received null payload"); } return false; } try { - if (object instanceof String) { - writer.write((String) object); + if (payload instanceof String) { + writer.write((String) payload); } - else if (object instanceof char[]) { - this.writer.write((char[]) object); + else if (payload instanceof char[]) { + this.writer.write((char[]) payload); } - else if (object instanceof byte[]) { - this.writer.write(new String((byte[]) object)); + else if (payload instanceof byte[]) { + this.writer.write(new String((byte[]) payload)); } else { - writer.write(object.toString()); + writer.write(payload.toString()); } if (this.shouldAppendNewLine) { writer.newLine(); diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/event/ApplicationEventTargetAdapterTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/event/ApplicationEventTargetAdapterTests.java index 1e4a20780c..edcd2855af 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/event/ApplicationEventTargetAdapterTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/event/ApplicationEventTargetAdapterTests.java @@ -28,6 +28,7 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.integration.bus.MessageBus; import org.springframework.integration.channel.MessageChannel; import org.springframework.integration.channel.SimpleChannel; +import org.springframework.integration.message.GenericMessage; import org.springframework.integration.message.StringMessage; import org.springframework.integration.scheduling.Subscription; @@ -49,7 +50,7 @@ public class ApplicationEventTargetAdapterTests { adapter.setApplicationEventPublisher(publisher); MessageBus bus = new MessageBus(); bus.registerChannel("channel", channel); - bus.registerHandler("adapter", adapter, new Subscription(channel)); + bus.registerTarget("adapter", adapter, new Subscription(channel)); bus.start(); assertEquals(1, latch.getCount()); channel.send(new StringMessage("123", "testing")); diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/file/config/FileTargetAdapterParserTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/file/config/FileTargetAdapterParserTests.java index feb9137f9d..c6683944ae 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/file/config/FileTargetAdapterParserTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/file/config/FileTargetAdapterParserTests.java @@ -23,7 +23,7 @@ import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.adapter.file.FileTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.TargetEndpoint; /** * @author Mark Fisher @@ -33,8 +33,8 @@ public class FileTargetAdapterParserTests { @Test public void testFileTargetAdapterParser() { ApplicationContext context = new ClassPathXmlApplicationContext("fileTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapter"); - assertEquals(FileTargetAdapter.class, endpoint.getHandler().getClass()); + TargetEndpoint endpoint = (TargetEndpoint) context.getBean("adapter"); + assertEquals(FileTargetAdapter.class, endpoint.getTarget().getClass()); assertEquals("adapter", endpoint.getName()); assertEquals("testChannel", endpoint.getSubscription().getChannelName()); } diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/httpinvoker/config/HttpInvokerTargetAdapterParserTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/httpinvoker/config/HttpInvokerTargetAdapterParserTests.java index b18c138a12..cce299f509 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/httpinvoker/config/HttpInvokerTargetAdapterParserTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/httpinvoker/config/HttpInvokerTargetAdapterParserTests.java @@ -24,7 +24,7 @@ import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.adapter.httpinvoker.HttpInvokerTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; /** * @author Mark Fisher @@ -35,7 +35,7 @@ public class HttpInvokerTargetAdapterParserTests { public void testHttpInvokerTargetAdapter() { ApplicationContext context = new ClassPathXmlApplicationContext( "httpInvokerTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapter"); + HandlerEndpoint endpoint = (HandlerEndpoint) context.getBean("adapter"); assertNotNull(endpoint); assertEquals(HttpInvokerTargetAdapter.class, endpoint.getHandler().getClass()); assertEquals("testChannel", endpoint.getSubscription().getChannelName()); diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParserTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParserTests.java index d72d0744ce..acab2e5e4c 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParserTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/jms/config/JmsTargetAdapterParserTests.java @@ -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. @@ -24,7 +24,7 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.adapter.jms.JmsTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.TargetEndpoint; /** * @author Mark Fisher @@ -35,8 +35,8 @@ public class JmsTargetAdapterParserTests { public void testTargetAdapterWithConnectionFactoryAndDestination() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "targetAdapterWithConnectionFactoryAndDestination.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapter"); - assertEquals(JmsTargetAdapter.class, endpoint.getHandler().getClass()); + TargetEndpoint endpoint = (TargetEndpoint) context.getBean("adapter"); + assertEquals(JmsTargetAdapter.class, endpoint.getTarget().getClass()); assertEquals("adapter", endpoint.getName()); assertEquals("testChannel", endpoint.getSubscription().getChannelName()); } @@ -45,8 +45,8 @@ public class JmsTargetAdapterParserTests { public void testTargetAdapterWithConnectionFactoryAndDestinationName() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "targetAdapterWithConnectionFactoryAndDestinationName.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapter"); - assertEquals(JmsTargetAdapter.class, endpoint.getHandler().getClass()); + TargetEndpoint endpoint = (TargetEndpoint) context.getBean("adapter"); + assertEquals(JmsTargetAdapter.class, endpoint.getTarget().getClass()); assertEquals("adapter", endpoint.getName()); assertEquals("testChannel", endpoint.getSubscription().getChannelName()); } @@ -55,8 +55,8 @@ public class JmsTargetAdapterParserTests { public void testTargetAdapterWithDefaultConnectionFactory() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "targetAdapterWithDefaultConnectionFactory.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapter"); - assertEquals(JmsTargetAdapter.class, endpoint.getHandler().getClass()); + TargetEndpoint endpoint = (TargetEndpoint) context.getBean("adapter"); + assertEquals(JmsTargetAdapter.class, endpoint.getTarget().getClass()); assertEquals("adapter", endpoint.getName()); assertEquals("testChannel", endpoint.getSubscription().getChannelName()); } diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/MailTargetAdapterContextTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/MailTargetAdapterContextTests.java index 70c668973d..6a1706e663 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/MailTargetAdapterContextTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/MailTargetAdapterContextTests.java @@ -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. @@ -59,7 +59,7 @@ public class MailTargetAdapterContextTests { @Test public void testStringMesssagesWithConfiguration() { - this.mailTargetAdapter.handle(new StringMessage(MailTestsHelper.MESSAGE_TEXT)); + this.mailTargetAdapter.send(new StringMessage(MailTestsHelper.MESSAGE_TEXT)); SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage(); assertEquals("no mime message should have been sent", 0, this.mailSender.getSentMimeMessages().size()); @@ -72,7 +72,7 @@ public class MailTargetAdapterContextTests { @Test public void testByteArrayMessage() throws Exception { byte[] payload = {1, 2, 3}; - mailTargetAdapter.handle(new GenericMessage(payload)); + mailTargetAdapter.send(new GenericMessage(payload)); assertEquals("no mime message should have been sent", 1, mailSender.getSentMimeMessages().size()); assertEquals("only one simple message must be sent", diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/MailTargetAdapterTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/MailTargetAdapterTests.java index 573961e453..9a84a86455 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/MailTargetAdapterTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/MailTargetAdapterTests.java @@ -64,7 +64,7 @@ public class MailTargetAdapterTests { @Test public void testTextMessage() { this.mailTargetAdapter.setHeaderGenerator(this.staticMailHeaderGenerator); - this.mailTargetAdapter.handle(new StringMessage(MailTestsHelper.MESSAGE_TEXT)); + this.mailTargetAdapter.send(new StringMessage(MailTestsHelper.MESSAGE_TEXT)); SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage(); assertEquals("no mime message should have been sent", 0, mailSender.getSentMimeMessages().size()); @@ -78,7 +78,7 @@ public class MailTargetAdapterTests { public void testByteArrayMessage() throws Exception { this.mailTargetAdapter.setHeaderGenerator(this.staticMailHeaderGenerator); byte[] payload = {1, 2, 3}; - this.mailTargetAdapter.handle(new GenericMessage(payload)); + this.mailTargetAdapter.send(new GenericMessage(payload)); byte[] buffer = new byte[1024]; MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0); assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart); @@ -99,7 +99,7 @@ public class MailTargetAdapterTests { message.getHeader().setAttribute(MailAttributeKeys.BCC, MailTestsHelper.BCC); message.getHeader().setAttribute(MailAttributeKeys.FROM, MailTestsHelper.FROM); message.getHeader().setAttribute(MailAttributeKeys.REPLY_TO, MailTestsHelper.REPLY_TO); - this.mailTargetAdapter.handle(message); + this.mailTargetAdapter.send(message); SimpleMailMessage mailMessage = MailTestsHelper.createSimpleMailMessage(); assertEquals("no mime message should have been sent", 0, mailSender.getSentMimeMessages().size()); diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/config/MailTargetAdapterParserTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/config/MailTargetAdapterParserTests.java index be36927d7b..6b315969af 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/config/MailTargetAdapterParserTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/mail/config/MailTargetAdapterParserTests.java @@ -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. @@ -27,9 +27,9 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.adapter.mail.MailHeaderGenerator; import org.springframework.integration.adapter.mail.MailTargetAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; -import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.endpoint.TargetEndpoint; import org.springframework.integration.message.Message; +import org.springframework.integration.message.Target; import org.springframework.mail.MailMessage; /** @@ -41,31 +41,31 @@ public class MailTargetAdapterParserTests { public void testAdapterWithMailSenderReference() { ApplicationContext context = new ClassPathXmlApplicationContext( "mailTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapterWithMailSenderReference"); - MessageHandler handler = endpoint.getHandler(); - assertNotNull(handler); - assertTrue(handler instanceof MailTargetAdapter); + TargetEndpoint endpoint = (TargetEndpoint) context.getBean("adapterWithMailSenderReference"); + Target target = endpoint.getTarget(); + assertNotNull(target); + assertTrue(target instanceof MailTargetAdapter); } @Test public void testAdapterWithHostProperty() { ApplicationContext context = new ClassPathXmlApplicationContext( "mailTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapterWithHostProperty"); - MessageHandler handler = endpoint.getHandler(); - assertNotNull(handler); - assertTrue(handler instanceof MailTargetAdapter); + TargetEndpoint endpoint = (TargetEndpoint) context.getBean("adapterWithHostProperty"); + Target target = endpoint.getTarget(); + assertNotNull(target); + assertTrue(target instanceof MailTargetAdapter); } @Test public void testAdapterWithHeaderGeneratorReference() { ApplicationContext context = new ClassPathXmlApplicationContext( "mailTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapterWithHeaderGeneratorReference"); - MessageHandler handler = endpoint.getHandler(); - assertNotNull(handler); - assertTrue(handler instanceof MailTargetAdapter); - DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(handler); + TargetEndpoint endpoint = (TargetEndpoint) context.getBean("adapterWithHeaderGeneratorReference"); + Target target = endpoint.getTarget(); + assertNotNull(target); + assertTrue(target instanceof MailTargetAdapter); + DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(target); MailHeaderGenerator headerGenerator = (MailHeaderGenerator) fieldAccessor.getPropertyValue("mailHeaderGenerator"); assertEquals(TestHeaderGenerator.class, headerGenerator.getClass()); diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/rmi/config/RmiTargetAdapterParserTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/rmi/config/RmiTargetAdapterParserTests.java index 3b75380108..22b40230c1 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/rmi/config/RmiTargetAdapterParserTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/rmi/config/RmiTargetAdapterParserTests.java @@ -27,7 +27,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.adapter.rmi.RmiSourceAdapter; import org.springframework.integration.adapter.rmi.RmiTargetAdapter; import org.springframework.integration.channel.SimpleChannel; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.message.StringMessage; /** @@ -50,7 +50,7 @@ public class RmiTargetAdapterParserTests { public void testRmiTargetAdapter() { ApplicationContext context = new ClassPathXmlApplicationContext( "rmiTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapter"); + HandlerEndpoint endpoint = (HandlerEndpoint) context.getBean("adapter"); assertNotNull(endpoint); assertEquals(RmiTargetAdapter.class, endpoint.getHandler().getClass()); RmiTargetAdapter adapter = (RmiTargetAdapter) endpoint.getHandler(); diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/stream/ByteStreamTargetAdapterTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/stream/ByteStreamTargetAdapterTests.java index 2664c0fbd8..1962739665 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/stream/ByteStreamTargetAdapterTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/stream/ByteStreamTargetAdapterTests.java @@ -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. @@ -40,7 +40,7 @@ public class ByteStreamTargetAdapterTests { public void testSingleByteArray() { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteStreamTargetAdapter adapter = new ByteStreamTargetAdapter(stream); - adapter.handle(new GenericMessage(new byte[] {1,2,3})); + adapter.send(new GenericMessage(new byte[] {1,2,3})); byte[] result = stream.toByteArray(); assertEquals(3, result.length); assertEquals(1, result[0]); @@ -52,7 +52,7 @@ public class ByteStreamTargetAdapterTests { public void testSingleString() { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteStreamTargetAdapter adapter = new ByteStreamTargetAdapter(stream); - adapter.handle(new StringMessage("foo")); + adapter.send(new StringMessage("foo")); byte[] result = stream.toByteArray(); assertEquals(3, result.length); assertEquals("foo", new String(result)); @@ -67,7 +67,7 @@ public class ByteStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler(1); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new GenericMessage(new byte[] {1,2,3}), 0); channel.send(new GenericMessage(new byte[] {4,5,6}), 0); channel.send(new GenericMessage(new byte[] {7,8,9}), 0); @@ -87,7 +87,7 @@ public class ByteStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler(1); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new GenericMessage(new byte[] {1,2,3}), 0); channel.send(new GenericMessage(new byte[] {4,5,6}), 0); channel.send(new GenericMessage(new byte[] {7,8,9}), 0); @@ -107,7 +107,7 @@ public class ByteStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler(1); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new GenericMessage(new byte[] {1,2,3}), 0); channel.send(new GenericMessage(new byte[] {4,5,6}), 0); channel.send(new GenericMessage(new byte[] {7,8,9}), 0); @@ -127,7 +127,7 @@ public class ByteStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler(1); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new GenericMessage(new byte[] {1,2,3}), 0); channel.send(new GenericMessage(new byte[] {4,5,6}), 0); channel.send(new GenericMessage(new byte[] {7,8,9}), 0); @@ -152,7 +152,7 @@ public class ByteStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler(1); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new GenericMessage(new byte[] {1,2,3}), 0); channel.send(new GenericMessage(new byte[] {4,5,6}), 0); channel.send(new GenericMessage(new byte[] {7,8,9}), 0); @@ -176,7 +176,7 @@ public class ByteStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler(1); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new GenericMessage(new byte[] {1,2,3}), 0); channel.send(new GenericMessage(new byte[] {4,5,6}), 0); channel.send(new GenericMessage(new byte[] {7,8,9}), 0); @@ -200,7 +200,7 @@ public class ByteStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler(1); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new GenericMessage(new byte[] {1,2,3}), 0); channel.send(new GenericMessage(new byte[] {4,5,6}), 0); channel.send(new GenericMessage(new byte[] {7,8,9}), 0); diff --git a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/stream/CharacterStreamTargetAdapterTests.java b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/stream/CharacterStreamTargetAdapterTests.java index 6303241227..d0f6d4a633 100644 --- a/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/stream/CharacterStreamTargetAdapterTests.java +++ b/spring-integration-adapters/src/test/java/org/springframework/integration/adapter/stream/CharacterStreamTargetAdapterTests.java @@ -43,7 +43,7 @@ public class CharacterStreamTargetAdapterTests { public void testSingleString() { StringWriter writer = new StringWriter(); CharacterStreamTargetAdapter adapter = new CharacterStreamTargetAdapter(writer); - adapter.handle(new StringMessage("foo")); + adapter.send(new StringMessage("foo")); assertEquals("foo", writer.toString()); } @@ -53,7 +53,7 @@ public class CharacterStreamTargetAdapterTests { StringWriter writer = new StringWriter(); CharacterStreamTargetAdapter adapter = new CharacterStreamTargetAdapter(writer); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new StringMessage("foo"), 0); channel.send(new StringMessage("bar"), 0); assertEquals(1, dispatcher.dispatch()); @@ -69,7 +69,7 @@ public class CharacterStreamTargetAdapterTests { CharacterStreamTargetAdapter adapter = new CharacterStreamTargetAdapter(writer); adapter.setShouldAppendNewLine(true); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new StringMessage("foo"), 0); channel.send(new StringMessage("bar"), 0); assertEquals(1, dispatcher.dispatch()); @@ -87,7 +87,7 @@ public class CharacterStreamTargetAdapterTests { dispatcherPolicy.setMaxMessagesPerTask(2); SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new StringMessage("foo"), 0); channel.send(new StringMessage("bar"), 0); assertEquals(2, dispatcher.dispatch()); @@ -104,7 +104,7 @@ public class CharacterStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); adapter.setShouldAppendNewLine(true); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); channel.send(new StringMessage("foo"), 0); channel.send(new StringMessage("bar"), 0); assertEquals(2, dispatcher.dispatch()); @@ -118,7 +118,7 @@ public class CharacterStreamTargetAdapterTests { StringWriter writer = new StringWriter(); CharacterStreamTargetAdapter adapter = new CharacterStreamTargetAdapter(writer); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); TestObject testObject = new TestObject("foo"); channel.send(new GenericMessage(testObject)); int count = dispatcher.dispatch(); @@ -135,7 +135,7 @@ public class CharacterStreamTargetAdapterTests { dispatcherPolicy.setMaxMessagesPerTask(2); SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); MessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); TestObject testObject1 = new TestObject("foo"); TestObject testObject2 = new TestObject("bar"); channel.send(new GenericMessage(testObject1), 0); @@ -154,7 +154,7 @@ public class CharacterStreamTargetAdapterTests { SimpleChannel channel = new SimpleChannel(5, dispatcherPolicy); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); adapter.setShouldAppendNewLine(true); - dispatcher.addHandler(adapter); + dispatcher.addTarget(adapter); TestObject testObject1 = new TestObject("foo"); TestObject testObject2 = new TestObject("bar"); channel.send(new GenericMessage(testObject1), 0); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/AbstractTargetAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/AbstractTargetAdapter.java deleted file mode 100644 index 7649a0ca93..0000000000 --- a/spring-integration-core/src/main/java/org/springframework/integration/adapter/AbstractTargetAdapter.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.adapter; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import org.springframework.integration.handler.MessageHandler; -import org.springframework.integration.message.Message; -import org.springframework.integration.message.MessageMapper; -import org.springframework.integration.message.SimplePayloadMessageMapper; -import org.springframework.util.Assert; - -/** - * Base class providing common behavior for target adapters. - * - * @author Mark Fisher - */ -public abstract class AbstractTargetAdapter implements MessageHandler { - - protected Log logger = LogFactory.getLog(this.getClass()); - - private MessageMapper mapper = new SimplePayloadMessageMapper(); - - - public void setMessageMapper(MessageMapper mapper) { - Assert.notNull(mapper, "'mapper' must not be null"); - this.mapper = mapper; - } - - protected MessageMapper getMessageMapper() { - return this.mapper; - } - - public final Message handle(Message message) { - this.sendToTarget(this.mapper.fromMessage(message)); - return null; - } - - protected abstract boolean sendToTarget(T object); - -} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/DefaultTargetAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/DefaultTargetAdapter.java deleted file mode 100644 index 3a487245b3..0000000000 --- a/spring-integration-core/src/main/java/org/springframework/integration/adapter/DefaultTargetAdapter.java +++ /dev/null @@ -1,42 +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.adapter; - -import org.springframework.integration.message.MessageMapper; -import org.springframework.util.Assert; - -/** - * Target adapter implementation that delegates to a {@link MessageMapper} - * and then passes the resulting object to the provided {@link Target}. - * - * @author Mark Fisher - */ -public class DefaultTargetAdapter extends AbstractTargetAdapter { - - private Target target; - - - public DefaultTargetAdapter(Target target) { - Assert.notNull(target, "'target' must not be null"); - this.target = target; - } - - public boolean sendToTarget(T object) { - return this.target.send(object); - } - -} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingHandler.java new file mode 100644 index 0000000000..914a7f2ee9 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingHandler.java @@ -0,0 +1,77 @@ +/* + * 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.adapter; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.integration.handler.HandlerMethodInvoker; +import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageCreator; +import org.springframework.integration.message.MessageMapper; +import org.springframework.util.Assert; + +/** + * A {@link MessageHandler} that invokes the specified method on the provided object. + * + * @author Mark Fisher + */ +public class MethodInvokingHandler implements MessageHandler, InitializingBean { + + private volatile Object object; + + private volatile String method; + + private volatile MessageMapper messageMapper; + + private volatile MessageCreator messageCreator; + + protected HandlerMethodInvoker invoker; + + + public void setObject(Object object) { + Assert.notNull(object, "'object' must not be null"); + this.object = object; + } + + public void setMethod(String method) { + Assert.notNull(method, "'method' must not be null"); + this.method = method; + } + + public void setMessageMapper(MessageMapper messageMapper) { + this.messageMapper = messageMapper; + } + + public void setMessageCreator(MessageCreator messageCreator) { + this.messageCreator = messageCreator; + } + + public void afterPropertiesSet() { + this.invoker = new HandlerMethodInvoker(this.object, this.method); + } + + public Message handle(Message message) { + Object args = (this.messageMapper != null) ? this.messageMapper.mapMessage(message) : message.getPayload(); + Object result = this.invoker.invokeMethod(args); + if (result == null) { + return null; + } + return (this.messageCreator != null) ? this.messageCreator.createMessage(result) : new GenericMessage(result); + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingTarget.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingTarget.java index 2e4d46f936..6cd60ec2a1 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingTarget.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingTarget.java @@ -16,61 +16,34 @@ package org.springframework.integration.adapter; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import java.lang.reflect.Method; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.integration.handler.HandlerMethodInvoker; -import org.springframework.util.Assert; +import org.springframework.integration.ConfigurationException; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.Target; +import org.springframework.integration.util.MethodValidator; /** * A messaging target that invokes the specified method on the provided object. * * @author Mark Fisher */ -public class MethodInvokingTarget implements Target, InitializingBean { - - private Log logger = LogFactory.getLog(this.getClass()); - - private T object; - - private String method; - - private HandlerMethodInvoker invoker; - - private ArgumentListPreparer argumentListPreparer; - - - public void setObject(T object) { - Assert.notNull(object, "'object' must not be null"); - this.object = object; - } - - public void setMethod(String method) { - Assert.notNull(method, "'method' must not be null"); - this.method = method; - } - - public void setArgumentListPreparer(ArgumentListPreparer argumentListPreparer) { - this.argumentListPreparer = argumentListPreparer; - } +public class MethodInvokingTarget extends MethodInvokingHandler implements Target { + @Override public void afterPropertiesSet() { - this.invoker = new HandlerMethodInvoker(this.object, this.method); + super.afterPropertiesSet(); + this.invoker.setMethodValidator(new MethodValidator() { + public void validate(Method method) throws Exception { + if (!method.getReturnType().equals(void.class)) { + throw new ConfigurationException("target method must have a void return"); + } + } + }); } - public boolean send(Object object) { - Object args[] = null; - if (this.argumentListPreparer != null) { - args = this.argumentListPreparer.prepare(object); - } - else { - args = new Object[] { object }; - } - Object result = this.invoker.invokeMethod(args); - if (result != null && logger.isWarnEnabled()) { - logger.warn("ignoring outbound channel adapter's return value"); - } + public boolean send(Message message) { + this.handle(message); return true; } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java b/spring-integration-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java index 397c9f4ac6..c4af15d633 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/aop/MessagePublishingInterceptor.java @@ -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. @@ -23,9 +23,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.integration.channel.MessageChannel; -import org.springframework.integration.message.MessageMapper; -import org.springframework.integration.message.SimplePayloadMessageMapper; -import org.springframework.util.Assert; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageCreator; /** * Interceptor that publishes a target method's return value to a channel. @@ -34,11 +34,11 @@ import org.springframework.util.Assert; */ public class MessagePublishingInterceptor implements MethodInterceptor { - protected Log logger = LogFactory.getLog(getClass()); + protected final Log logger = LogFactory.getLog(getClass()); - private MessageMapper mapper = new SimplePayloadMessageMapper(); + private volatile MessageCreator messageCreator; - private MessageChannel defaultChannel; + private volatile MessageChannel defaultChannel; public void setDefaultChannel(MessageChannel defaultChannel) { @@ -46,14 +46,13 @@ public class MessagePublishingInterceptor implements MethodInterceptor { } /** - * Specify the {@link MessageMapper} to use when creating a message from the - * return value Object. The default is a {@link SimplePayloadMessageMapper}. + * Specify the {@link MessageCreator} to use when creating a message from the + * return value Object. * - * @param mapper the mapper to use + * @param messageCreator the MessageCreator to use */ - public void setMessageMapper(MessageMapper mapper) { - Assert.notNull(mapper, "mapper must not be null"); - this.mapper = mapper; + public void setMessageCreator(MessageCreator messageCreator) { + this.messageCreator = messageCreator; } /** @@ -70,7 +69,8 @@ public class MessagePublishingInterceptor implements MethodInterceptor { } } else { - channel.send(mapper.toMessage(retval)); + Message message = (this.messageCreator != null) ? this.messageCreator.createMessage(retval) : new GenericMessage(retval); + channel.send(message); } } return retval; diff --git a/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBus.java b/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBus.java index 342b0e5fda..99d7e3fcd1 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBus.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/bus/MessageBus.java @@ -44,11 +44,13 @@ import org.springframework.integration.dispatcher.SchedulingMessageDispatcher; import org.springframework.integration.dispatcher.SynchronousChannel; import org.springframework.integration.endpoint.ConcurrencyPolicy; import org.springframework.integration.endpoint.DefaultEndpointRegistry; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; import org.springframework.integration.endpoint.EndpointRegistry; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.endpoint.MessageEndpoint; +import org.springframework.integration.endpoint.TargetEndpoint; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.MessagingException; +import org.springframework.integration.message.Target; import org.springframework.integration.scheduling.MessagePublishingErrorHandler; import org.springframework.integration.scheduling.MessagingTask; import org.springframework.integration.scheduling.MessagingTaskScheduler; @@ -235,17 +237,25 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio } public void registerHandler(String name, MessageHandler handler, Subscription subscription, ConcurrencyPolicy concurrencyPolicy) { - if (!this.initialized) { - this.initialize(); - } - Assert.notNull(name, "'name' must not be null"); Assert.notNull(handler, "'handler' must not be null"); - Assert.notNull(subscription, "'subscription' must not be null"); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handler); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); + this.doRegisterEndpoint(name, endpoint, subscription, concurrencyPolicy); + } + + public void registerTarget(String name, Target target, Subscription subscription) { + this.registerTarget(name, target, subscription, this.defaultConcurrencyPolicy); + } + + public void registerTarget(String name, Target target, Subscription subscription, ConcurrencyPolicy concurrencyPolicy) { + Assert.notNull(target, "'target' must not be null"); + TargetEndpoint endpoint = new TargetEndpoint(target); + this.doRegisterEndpoint(name, endpoint, subscription, concurrencyPolicy); + } + + private void doRegisterEndpoint(String name, TargetEndpoint endpoint, Subscription subscription, ConcurrencyPolicy concurrencyPolicy) { endpoint.setName(name); endpoint.setSubscription(subscription); endpoint.setConcurrencyPolicy(concurrencyPolicy); - endpoint.afterPropertiesSet(); this.registerEndpoint(name, endpoint); } @@ -257,8 +267,11 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio ((ChannelRegistryAware) endpoint).setChannelRegistry(this.channelRegistry); } if (endpoint.getConcurrencyPolicy() == null && this.defaultConcurrencyPolicy != null - && endpoint instanceof DefaultMessageEndpoint) { - ((DefaultMessageEndpoint) endpoint).setConcurrencyPolicy(this.defaultConcurrencyPolicy); + && endpoint instanceof TargetEndpoint) { + ((TargetEndpoint) endpoint).setConcurrencyPolicy(this.defaultConcurrencyPolicy); + } + if (endpoint instanceof TargetEndpoint) { + ((TargetEndpoint) endpoint).afterPropertiesSet(); } this.endpointRegistry.registerEndpoint(name, endpoint); if (this.isRunning()) { @@ -277,7 +290,7 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio Collection dispatchers = this.dispatchers.values(); boolean removed = false; for (SchedulingMessageDispatcher dispatcher : dispatchers) { - removed = (removed || dispatcher.removeHandler(endpoint)); + removed = (removed || dispatcher.removeTarget(endpoint)); } if (removed) { return endpoint; @@ -329,9 +342,9 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio this.registerChannel(channelName, channel); } } - if (endpoint instanceof DefaultMessageEndpoint) { - DefaultMessageEndpoint dme = (DefaultMessageEndpoint) endpoint; - String outputChannelName = dme.getDefaultOutputChannelName(); + if (endpoint instanceof HandlerEndpoint) { + HandlerEndpoint handlerEndpoint = (HandlerEndpoint) endpoint; + String outputChannelName = handlerEndpoint.getDefaultOutputChannelName(); if (outputChannelName != null && this.lookupChannel(outputChannelName) == null) { if (!this.autoCreateChannels) { throw new ConfigurationException("Unknown channel '" + outputChannelName + @@ -340,8 +353,11 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio } this.registerChannel(outputChannelName, new SimpleChannel()); } - if (!dme.hasErrorHandler() && this.getErrorChannel() != null && !this.getErrorChannel().equals(channel)) { - dme.setErrorHandler(new MessagePublishingErrorHandler(this.getErrorChannel())); + } + if (endpoint instanceof TargetEndpoint) { + TargetEndpoint targetEndpoint = (TargetEndpoint) endpoint; + if (!targetEndpoint.hasErrorHandler() && this.getErrorChannel() != null && !this.getErrorChannel().equals(channel)) { + targetEndpoint.setErrorHandler(new MessagePublishingErrorHandler(this.getErrorChannel())); } } this.registerWithDispatcher(channel, endpoint, subscription.getSchedule()); @@ -369,14 +385,14 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio } } - private void registerWithDispatcher(MessageChannel channel, MessageHandler handler, Schedule schedule) { + private void registerWithDispatcher(MessageChannel channel, Target target, Schedule schedule) { if (schedule == null && (channel instanceof SynchronousChannel)) { - ((SynchronousChannel) channel).addHandler(handler); - if (handler instanceof Lifecycle) { - ((Lifecycle) handler).start(); + ((SynchronousChannel) channel).addTarget(target); + if (target instanceof Lifecycle) { + ((Lifecycle) target).start(); } - if (handler instanceof DefaultMessageEndpoint) { - ((DefaultMessageEndpoint) handler).setErrorHandler(new ErrorHandler() { + if (target instanceof TargetEndpoint) { + ((TargetEndpoint) target).setErrorHandler(new ErrorHandler() { public void handle(Throwable t) { if (t instanceof MessagingException) { throw (MessagingException) t; @@ -394,7 +410,7 @@ public class MessageBus implements ChannelRegistry, EndpointRegistry, Applicatio } return; } - dispatcher.addHandler(handler, schedule); + dispatcher.addTarget(target, schedule); if (this.isRunning() && !dispatcher.isRunning()) { dispatcher.start(); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/ChannelAdapterParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/ChannelAdapterParser.java index ca558c9f35..eabae330dd 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/ChannelAdapterParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/ChannelAdapterParser.java @@ -25,11 +25,10 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.ConfigurationException; -import org.springframework.integration.adapter.DefaultTargetAdapter; import org.springframework.integration.adapter.MethodInvokingSource; import org.springframework.integration.adapter.MethodInvokingTarget; import org.springframework.integration.adapter.PollingSourceAdapter; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.scheduling.PollingSchedule; import org.springframework.integration.scheduling.Subscription; import org.springframework.util.StringUtils; @@ -78,7 +77,10 @@ public class ChannelAdapterParser implements BeanDefinitionParser { if (this.isInbound) { adapterDef = new RootBeanDefinition(PollingSourceAdapter.class); invokerDef = new RootBeanDefinition(MethodInvokingSource.class); - String invokerBeanName = this.configureAndRegisterInvoker(invokerDef, ref, method, parserContext); + invokerDef.getPropertyValues().addPropertyValue("object", new RuntimeBeanReference(ref)); + invokerDef.getPropertyValues().addPropertyValue("method", method); + String invokerBeanName = parserContext.getReaderContext().generateBeanName(invokerDef); + parserContext.registerBeanComponent(new BeanComponentDefinition(invokerDef, invokerBeanName)); String period = element.getAttribute(PERIOD_ATTRIBUTE); if (!StringUtils.hasText(period)) { throw new ConfigurationException("'period' is required"); @@ -89,10 +91,9 @@ public class ChannelAdapterParser implements BeanDefinitionParser { adapterDef.getConstructorArgumentValues().addGenericArgumentValue(schedule); } else { - adapterDef = new RootBeanDefinition(DefaultTargetAdapter.class); - invokerDef = new RootBeanDefinition(MethodInvokingTarget.class); - String invokerBeanName = this.configureAndRegisterInvoker(invokerDef, ref, method, parserContext); - adapterDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference(invokerBeanName)); + adapterDef = new RootBeanDefinition(MethodInvokingTarget.class); + adapterDef.getPropertyValues().addPropertyValue("object", new RuntimeBeanReference(ref)); + adapterDef.getPropertyValues().addPropertyValue("method", method); } adapterDef.setSource(parserContext.extractSource(element)); String beanName = element.getAttribute(ID_ATTRIBUTE); @@ -100,7 +101,7 @@ public class ChannelAdapterParser implements BeanDefinitionParser { beanName = parserContext.getReaderContext().generateBeanName(adapterDef); } if (!this.isInbound) { - RootBeanDefinition endpointDef = new RootBeanDefinition(DefaultMessageEndpoint.class); + RootBeanDefinition endpointDef = new RootBeanDefinition(HandlerEndpoint.class); RootBeanDefinition subscriptionDef = new RootBeanDefinition(Subscription.class); subscriptionDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference(channel)); String subscriptionBeanName = parserContext.getReaderContext().generateBeanName(subscriptionDef); @@ -114,12 +115,4 @@ public class ChannelAdapterParser implements BeanDefinitionParser { return adapterDef; } - private String configureAndRegisterInvoker(RootBeanDefinition invokerDef, String objectRef, String methodName, ParserContext parserContext) { - invokerDef.getPropertyValues().addPropertyValue("object", new RuntimeBeanReference(objectRef)); - invokerDef.getPropertyValues().addPropertyValue("method", methodName); - String invokerBeanName = parserContext.getReaderContext().generateBeanName(invokerDef); - parserContext.registerBeanComponent(new BeanComponentDefinition(invokerDef, invokerBeanName)); - return invokerBeanName; - } - } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java index 7c5b44d35e..37f9e8be93 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java @@ -32,7 +32,7 @@ import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.ConfigurationException; import org.springframework.integration.endpoint.ConcurrencyPolicy; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.handler.DefaultMessageHandlerAdapter; import org.springframework.integration.handler.MessageHandlerChain; import org.springframework.integration.scheduling.PollingSchedule; @@ -95,7 +95,7 @@ public class EndpointParser implements BeanDefinitionParser { public BeanDefinition parse(Element element, ParserContext parserContext) { - RootBeanDefinition endpointDef = new RootBeanDefinition(DefaultMessageEndpoint.class); + RootBeanDefinition endpointDef = new RootBeanDefinition(HandlerEndpoint.class); endpointDef.setSource(parserContext.extractSource(element)); String inputChannel = element.getAttribute(INPUT_CHANNEL_ATTRIBUTE); String defaultOutputChannel = element.getAttribute(DEFAULT_OUTPUT_CHANNEL_ATTRIBUTE); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/MessageEndpointAnnotationPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/config/MessageEndpointAnnotationPostProcessor.java index c0ed1a23c6..7b1e5effa4 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/MessageEndpointAnnotationPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/MessageEndpointAnnotationPostProcessor.java @@ -34,7 +34,6 @@ import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.integration.ConfigurationException; -import org.springframework.integration.adapter.DefaultTargetAdapter; import org.springframework.integration.adapter.MethodInvokingSource; import org.springframework.integration.adapter.MethodInvokingTarget; import org.springframework.integration.adapter.PollingSourceAdapter; @@ -51,7 +50,7 @@ import org.springframework.integration.bus.MessageBus; import org.springframework.integration.channel.ChannelRegistryAware; import org.springframework.integration.dispatcher.SynchronousChannel; import org.springframework.integration.endpoint.ConcurrencyPolicy; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.handler.AbstractMessageHandlerAdapter; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.handler.MessageHandlerChain; @@ -121,7 +120,7 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor if (handlerChain == null) { throw new ConfigurationException("@MessageEndpoint has no handler method"); } - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handlerChain); + HandlerEndpoint endpoint = new HandlerEndpoint(handlerChain); this.configureInput(bean, beanName, endpointAnnotation, endpoint); if (StringUtils.hasText(defaultOutputChannelName)) { endpoint.setDefaultOutputChannelName(defaultOutputChannelName); @@ -143,7 +142,7 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor } private void configureInput(final Object bean, final String beanName, MessageEndpoint annotation, - final DefaultMessageEndpoint endpoint) { + final HandlerEndpoint endpoint) { String channelName = annotation.input(); if (StringUtils.hasText(channelName)) { Subscription subscription = new Subscription(channelName); @@ -175,7 +174,7 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor }); } - private void configureDefaultOutput(final Object bean, final String beanName, final DefaultMessageEndpoint endpoint) { + private void configureDefaultOutput(final Object bean, final String beanName, final HandlerEndpoint endpoint) { ReflectionUtils.doWithMethods(this.getBeanClass(bean), new ReflectionUtils.MethodCallback() { boolean foundDefaultOutput = false; public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { @@ -184,23 +183,12 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor if (foundDefaultOutput) { throw new ConfigurationException("only one @DefaultOutput allowed per endpoint"); } - MethodInvokingTarget target = new MethodInvokingTarget(); + MethodInvokingTarget target = new MethodInvokingTarget(); target.setObject(bean); target.setMethod(method.getName()); target.afterPropertiesSet(); - DefaultTargetAdapter adapter = new DefaultTargetAdapter(target); MessageHandler handler = endpoint.getHandler(); - if (handler == null) { - endpoint.setHandler(adapter); - } - else if (handler instanceof MessageHandlerChain) { - ((MessageHandlerChain) handler).add(adapter); - } - else { - MessageHandlerChain chain = new MessageHandlerChain(); - chain.add(handler); - chain.add(adapter); - } + ((MessageHandlerChain) handler).add(target); foundDefaultOutput = true; return; } @@ -208,7 +196,7 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor }); } - private void configureCompletionStrategy(final Object bean, final DefaultMessageEndpoint endpoint) { + private void configureCompletionStrategy(final Object bean, final HandlerEndpoint endpoint) { ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Annotation annotation = AnnotationUtils.getAnnotation(method, CompletionStrategy.class); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/DefaultMessageDispatcher.java b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/DefaultMessageDispatcher.java index f9a298496d..8f8312cc36 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/DefaultMessageDispatcher.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/DefaultMessageDispatcher.java @@ -29,8 +29,8 @@ import org.apache.commons.logging.LogFactory; import org.springframework.context.Lifecycle; import org.springframework.integration.ConfigurationException; import org.springframework.integration.channel.MessageChannel; -import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.Message; +import org.springframework.integration.message.Target; import org.springframework.integration.scheduling.MessagingTask; import org.springframework.integration.scheduling.MessagingTaskScheduler; import org.springframework.integration.scheduling.PollingSchedule; @@ -58,7 +58,7 @@ public class DefaultMessageDispatcher implements SchedulingMessageDispatcher { private volatile Schedule defaultSchedule = new PollingSchedule(5); - private final ConcurrentMap> scheduledHandlers = new ConcurrentHashMap>(); + private final ConcurrentMap> scheduledTargets = new ConcurrentHashMap>(); private final AtomicLong totalMessagesProcessed = new AtomicLong(); @@ -81,41 +81,41 @@ public class DefaultMessageDispatcher implements SchedulingMessageDispatcher { this.defaultSchedule = defaultSchedule; } - public void addHandler(MessageHandler handler) { - this.addHandler(handler, null); + public void addTarget(Target target) { + this.addTarget(target, null); } - public void addHandler(MessageHandler handler, Schedule schedule) { - Assert.notNull(handler, "'handler' must not be null"); + public void addTarget(Target target, Schedule schedule) { + Assert.notNull(target, "'target' must not be null"); if (schedule == null) { schedule = this.defaultSchedule; } else if (this.channel.getDispatcherPolicy().isPublishSubscribe()) { if (logger.isInfoEnabled()) { logger.info("This dispatcher broadcasts messages for a publish-subscribe channel. " + - "Therefore all handlers are scheduled with its 'defaultSchedule', " + + "Therefore all targets are scheduled with its 'defaultSchedule', " + "and the provided schedule will be ignored."); } schedule = this.defaultSchedule; } - if (this.isRunning() && handler instanceof Lifecycle) { - ((Lifecycle) handler).start(); + if (this.isRunning() && target instanceof Lifecycle) { + ((Lifecycle) target).start(); } - List handlers = this.scheduledHandlers.get(schedule); - if (handlers == null) { - handlers = this.scheduledHandlers.putIfAbsent(schedule, new CopyOnWriteArrayList()); + List targets = this.scheduledTargets.get(schedule); + if (targets == null) { + targets = this.scheduledTargets.putIfAbsent(schedule, new CopyOnWriteArrayList()); } - this.scheduledHandlers.get(schedule).add(handler); - if (handlers == null && this.isRunning()) { + this.scheduledTargets.get(schedule).add(target); + if (targets == null && this.isRunning()) { this.scheduleDispatcherTask(schedule); } } - public boolean removeHandler(MessageHandler handler) { + public boolean removeTarget(Target target) { boolean removed = false; - Collection> handlerLists = this.scheduledHandlers.values(); - for (List handlers : handlerLists) { - removed = (removed || handlers.remove(handler)); + Collection> targetLists = this.scheduledTargets.values(); + for (List targets : targetLists) { + removed = (removed || targets.remove(target)); } return removed; } @@ -135,7 +135,7 @@ public class DefaultMessageDispatcher implements SchedulingMessageDispatcher { if (!this.scheduler.isRunning()) { this.scheduler.start(); } - for (Schedule schedule : this.scheduledHandlers.keySet()) { + for (Schedule schedule : this.scheduledTargets.keySet()) { scheduleDispatcherTask(schedule); } this.running = true; @@ -143,10 +143,10 @@ public class DefaultMessageDispatcher implements SchedulingMessageDispatcher { } private void scheduleDispatcherTask(Schedule schedule) { - List handlers = this.scheduledHandlers.get(schedule); - for (MessageHandler handler : handlers) { - if (handler instanceof Lifecycle) { - ((Lifecycle) handler).start(); + List targets = this.scheduledTargets.get(schedule); + for (Target target : targets) { + if (target instanceof Lifecycle) { + ((Lifecycle) target).start(); } } this.scheduler.schedule(new DispatcherTask(schedule)); @@ -157,10 +157,10 @@ public class DefaultMessageDispatcher implements SchedulingMessageDispatcher { return; } synchronized (this.lifecycleMonitor) { - for (List handlerList : scheduledHandlers.values()) { - for (MessageHandler handler : handlerList) { - if (handler instanceof Lifecycle) { - ((Lifecycle) handler).stop(); + for (List targetList : this.scheduledTargets.values()) { + for (Target target : targetList) { + if (target instanceof Lifecycle) { + ((Lifecycle) target).stop(); } } } @@ -193,8 +193,8 @@ public class DefaultMessageDispatcher implements SchedulingMessageDispatcher { schedule = this.defaultSchedule; } MessageDistributor distributor = new DefaultMessageDistributor(this.channel.getDispatcherPolicy()); - for (MessageHandler handler : this.scheduledHandlers.get(schedule)) { - distributor.addHandler(handler); + for (Target target : this.scheduledTargets.get(schedule)) { + distributor.addTarget(target); } return distributor; } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/DefaultMessageDistributor.java b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/DefaultMessageDistributor.java index ee6dfae0bc..e6f36ee9c9 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/DefaultMessageDistributor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/DefaultMessageDistributor.java @@ -25,11 +25,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.integration.channel.DispatcherPolicy; -import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.handler.MessageHandlerNotRunningException; import org.springframework.integration.handler.MessageHandlerRejectedExecutionException; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessageDeliveryException; +import org.springframework.integration.message.Target; import org.springframework.integration.message.selector.MessageSelectorRejectedException; import org.springframework.util.Assert; @@ -42,7 +42,7 @@ public class DefaultMessageDistributor implements MessageDistributor { private final Log logger = LogFactory.getLog(this.getClass()); - private final List handlers = new CopyOnWriteArrayList(); + private final List targets = new CopyOnWriteArrayList(); private final DispatcherPolicy dispatcherPolicy; @@ -53,21 +53,21 @@ public class DefaultMessageDistributor implements MessageDistributor { } - public void addHandler(MessageHandler handler) { - this.handlers.add(handler); + public void addTarget(Target target) { + this.targets.add(target); } - public boolean removeHandler(MessageHandler handler) { - return this.handlers.remove(handler); + public boolean removeTarget(Target target) { + return this.targets.remove(target); } public boolean distribute(Message message) { int attempts = 0; - List targets = new ArrayList(this.handlers); + List targets = new ArrayList(this.targets); while (attempts < this.dispatcherPolicy.getRejectionLimit()) { if (attempts > 0) { if (logger.isDebugEnabled()) { - logger.debug("handler(s) rejected message after " + attempts + + logger.debug("target(s) rejected message after " + attempts + " attempt(s), will try again after 'retryInterval' of " + this.dispatcherPolicy.getRetryInterval() + " milliseconds"); } @@ -79,37 +79,37 @@ public class DefaultMessageDistributor implements MessageDistributor { return false; } } - Iterator iter = targets.iterator(); + Iterator iter = targets.iterator(); if (!iter.hasNext()) { if (logger.isWarnEnabled()) { - logger.warn("no active handlers"); + logger.warn("no active targets"); } return false; } boolean rejected = false; while (iter.hasNext()) { - MessageHandler handler = iter.next(); + Target target = iter.next(); try { - handler.handle(message); - if (!this.dispatcherPolicy.isPublishSubscribe()) { + boolean sent = target.send(message); + if (!this.dispatcherPolicy.isPublishSubscribe() && sent) { return true; } iter.remove(); } catch (MessageSelectorRejectedException e) { if (logger.isDebugEnabled()) { - logger.debug("selector rejected message, continuing with other handlers if available", e); + logger.debug("selector rejected message, continuing with other targets if available", e); } } catch (MessageHandlerNotRunningException e) { if (logger.isDebugEnabled()) { - logger.debug("handler not running, continuing with other handlers if available", e); + logger.debug("target not running, continuing with other targets if available", e); } } catch (MessageHandlerRejectedExecutionException e) { rejected = true; if (logger.isDebugEnabled()) { - logger.debug("handler is busy, continuing with other handlers if available", e); + logger.debug("target is busy, continuing with other targets if available", e); } } } @@ -121,7 +121,7 @@ public class DefaultMessageDistributor implements MessageDistributor { if (this.dispatcherPolicy.getShouldFailOnRejectionLimit()) { throw new MessageDeliveryException(message, "Dispatcher reached rejection limit of " + this.dispatcherPolicy.getRejectionLimit() - + ". Consider increasing the handler's concurrency and/or " + + ". Consider increasing the target's concurrency and/or " + "the dispatcherPolicy's 'rejectionLimit'."); } return false; diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/MessageDispatcher.java b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/MessageDispatcher.java index 7c2f4a37d9..c077696c79 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/MessageDispatcher.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/MessageDispatcher.java @@ -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. @@ -16,7 +16,7 @@ package org.springframework.integration.dispatcher; -import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.message.Target; /** * Strategy interface for dispatching messages. @@ -25,9 +25,9 @@ import org.springframework.integration.handler.MessageHandler; */ public interface MessageDispatcher { - void addHandler(MessageHandler handler); + void addTarget(Target target); - boolean removeHandler(MessageHandler handler); + boolean removeTarget(Target target); int dispatch(); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/MessageDistributor.java b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/MessageDistributor.java index b631717472..b196987378 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/MessageDistributor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/MessageDistributor.java @@ -16,20 +16,19 @@ package org.springframework.integration.dispatcher; -import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.Message; +import org.springframework.integration.message.Target; /** - * Strategy interface for distributing a {@link Message} to one or more - * {@link MessageHandler MessageHandlers}. + * Strategy interface for distributing a {@link Message} to one or more {@link Target targets}. * * @author Mark Fisher */ public interface MessageDistributor { - void addHandler(MessageHandler handler); + void addTarget(Target target); - boolean removeHandler(MessageHandler handler); + boolean removeTarget(Target target); boolean distribute(Message message); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/SchedulingMessageDispatcher.java b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/SchedulingMessageDispatcher.java index c1239b22cd..edf486dd96 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/SchedulingMessageDispatcher.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/SchedulingMessageDispatcher.java @@ -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. @@ -17,11 +17,11 @@ package org.springframework.integration.dispatcher; import org.springframework.context.Lifecycle; -import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.message.Target; import org.springframework.integration.scheduling.Schedule; /** - * An extension to the {@link MessageDispatcher} strategy for handlers that may + * An extension to the {@link MessageDispatcher} strategy for targets that may * be scheduled. * * @author Mark Fisher @@ -30,6 +30,6 @@ public interface SchedulingMessageDispatcher extends MessageDispatcher, Lifecycl void setDefaultSchedule(Schedule defaultSchedule); - void addHandler(MessageHandler handler, Schedule schedule); + void addTarget(Target target, Schedule schedule); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/SynchronousChannel.java b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/SynchronousChannel.java index 62d9c5906f..883e434b52 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/SynchronousChannel.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dispatcher/SynchronousChannel.java @@ -27,6 +27,7 @@ import org.springframework.integration.channel.DispatcherPolicy; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.Message; import org.springframework.integration.message.PollableSource; +import org.springframework.integration.message.Target; import org.springframework.integration.message.selector.MessageSelector; /** @@ -70,13 +71,13 @@ public class SynchronousChannel extends AbstractMessageChannel { this.source = source; } - public void addHandler(MessageHandler handler) { - this.distributor.addHandler(handler); + public void addTarget(Target target) { + this.distributor.addTarget(target); this.handlerCount.incrementAndGet(); } - public boolean removeHandler(MessageHandler handler) { - if (this.distributor.removeHandler(handler)) { + public boolean removeTarget(Target target) { + if (this.distributor.removeTarget(target)) { this.handlerCount.decrementAndGet(); return true; } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/ConcurrentHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/ConcurrentTarget.java similarity index 69% rename from spring-integration-core/src/main/java/org/springframework/integration/endpoint/ConcurrentHandler.java rename to spring-integration-core/src/main/java/org/springframework/integration/endpoint/ConcurrentTarget.java index f01f56b7bd..c3366758aa 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/ConcurrentHandler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/ConcurrentTarget.java @@ -25,36 +25,34 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.handler.MessageHandlerNotRunningException; import org.springframework.integration.handler.MessageHandlerRejectedExecutionException; -import org.springframework.integration.handler.ReplyHandler; import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageDeliveryException; +import org.springframework.integration.message.Target; import org.springframework.integration.util.ErrorHandler; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.util.Assert; /** - * A {@link MessageHandler} implementation that encapsulates a - * {@link ThreadPoolTaskExecutor} and delegates to a wrapped handler for - * concurrent, asynchronous message handling. + * A {@link Target} implementation that encapsulates an Executor and delegates + * to a wrapped target for concurrent, asynchronous message handling. * * @author Mark Fisher */ -public class ConcurrentHandler implements MessageHandler, DisposableBean { +public class ConcurrentTarget implements Target, DisposableBean { private final Log logger = LogFactory.getLog(this.getClass()); - private final MessageHandler handler; + private final Target target; private final ExecutorService executor; private volatile ErrorHandler errorHandler; - private volatile ReplyHandler replyHandler; - - public ConcurrentHandler(MessageHandler handler, ExecutorService executor) { - Assert.notNull(handler, "'handler' must not be null"); + public ConcurrentTarget(Target target, ExecutorService executor) { + Assert.notNull(target, "'target' must not be null"); Assert.notNull(executor, "'executor' must not be null"); - this.handler = handler; + this.target = target; this.executor = executor; } @@ -63,21 +61,17 @@ public class ConcurrentHandler implements MessageHandler, DisposableBean { this.errorHandler = errorHandler; } - public void setReplyHandler(ReplyHandler replyHandler) { - this.replyHandler = replyHandler; - } - public void destroy() { - this.executor.shutdownNow(); + this.executor.shutdown(); } - public Message handle(Message message) { + public boolean send(Message message) { if (this.executor.isShutdown()) { throw new MessageHandlerNotRunningException(message); } try { - this.executor.execute(new HandlerTask(message)); - return null; + this.executor.execute(new TargetTask(message)); + return true; } catch (RuntimeException e) { throw new MessageHandlerRejectedExecutionException(message, e); @@ -85,19 +79,18 @@ public class ConcurrentHandler implements MessageHandler, DisposableBean { } - private class HandlerTask implements Runnable { + private class TargetTask implements Runnable { private Message message; - HandlerTask(Message message) { + TargetTask(Message message) { this.message = message; } public void run() { try { - Message reply = handler.handle(this.message); - if (replyHandler != null) { - replyHandler.handle(reply, this.message.getHeader()); + if (!target.send(this.message)) { + throw new MessageDeliveryException(message, "failed to send message to target"); } } catch (Throwable t) { diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/HandlerEndpoint.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/HandlerEndpoint.java new file mode 100644 index 0000000000..c0d8fbefcf --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/HandlerEndpoint.java @@ -0,0 +1,163 @@ +/* + * 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.channel.ChannelRegistry; +import org.springframework.integration.channel.ChannelRegistryAware; +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.handler.ReplyHandler; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageDeliveryException; +import org.springframework.integration.message.MessageHandlingException; +import org.springframework.integration.message.MessageHeader; +import org.springframework.integration.message.Target; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +/** + * Implementation of the {@link MessageEndpoint} interface for invoking + * {@link MessageHandler MessageHandlers}. + * + * @author Mark Fisher + */ +public class HandlerEndpoint extends TargetEndpoint { + + private volatile MessageHandler handler; + + private volatile ReplyHandler replyHandler = new EndpointReplyHandler(); + + private volatile long replyTimeout = 1000; + + private volatile String defaultOutputChannelName; + + + public HandlerEndpoint(MessageHandler handler) { + Assert.notNull(handler, "handler must not be null"); + this.handler = handler; + } + + + public MessageHandler getHandler() { + return this.handler; + } + + public void setReplyHandler(ReplyHandler replyHandler) { + Assert.notNull(replyHandler, "'replyHandler' must not be null"); + this.replyHandler = replyHandler; + } + + /** + * Set the timeout in milliseconds to be enforced when this endpoint sends a + * reply message. If the message is not sent successfully within the + * allotted time, then a MessageDeliveryException will be thrown. + * The default replyTimeout value is 1000 milliseconds. + */ + public void setReplyTimeout(long replyTimeout) { + this.replyTimeout = replyTimeout; + } + + /** + * Set the name of the channel to which this endpoint should send reply + * messages by default. + */ + public void setDefaultOutputChannelName(String defaultOutputChannelName) { + this.defaultOutputChannelName = defaultOutputChannelName; + } + + public String getDefaultOutputChannelName() { + return this.defaultOutputChannelName; + } + + public void afterPropertiesSet() { + Assert.notNull(this.handler, "handler must not be null"); + if (this.handler instanceof ChannelRegistryAware) { + ((ChannelRegistryAware) this.handler).setChannelRegistry(this.getChannelRegistry()); + } + super.setTarget(new HandlerInvokingTarget(this.handler, this.replyHandler)); + super.afterPropertiesSet(); + } + + + private MessageChannel resolveReplyChannel(MessageHeader originalMessageHeader) { + Object returnAddress = originalMessageHeader.getReturnAddress(); + if (returnAddress instanceof MessageChannel) { + return (MessageChannel) returnAddress; + } + ChannelRegistry registry = this.getChannelRegistry(); + if (returnAddress instanceof String && registry != null) { + String channelName = (String) returnAddress; + if (StringUtils.hasText(channelName)) { + return registry.lookupChannel(channelName); + } + } + if (this.defaultOutputChannelName != null && registry != null) { + return registry.lookupChannel(this.defaultOutputChannelName); + } + return null; + } + + + private static class HandlerInvokingTarget implements Target { + + private final MessageHandler handler; + + private final ReplyHandler replyHandler; + + + public HandlerInvokingTarget(MessageHandler handler, ReplyHandler replyHandler) { + this.handler = handler; + this.replyHandler = replyHandler; + } + + + public boolean send(Message message) { + Message replyMessage = this.handler.handle(message); + if (replyMessage != null) { + if (replyMessage.getHeader().getCorrelationId() == null) { + replyMessage.getHeader().setCorrelationId(message.getId()); + } + this.replyHandler.handle(replyMessage, message.getHeader()); + } + return true; + } + + } + + + private class EndpointReplyHandler implements ReplyHandler { + + public void handle(Message replyMessage, MessageHeader originalMessageHeader) { + if (replyMessage == null) { + return; + } + MessageChannel replyChannel = resolveReplyChannel(originalMessageHeader); + if (replyChannel == null) { + throw new MessageHandlingException(replyMessage, "Unable to determine reply channel for message. " + + "Provide a 'returnAddress' in the message header or a 'defaultOutputChannelName' on the message endpoint."); + } + if (logger.isDebugEnabled()) { + logger.debug("endpoint '" + HandlerEndpoint.this + "' replying to channel '" + replyChannel + "' with message: " + replyMessage); + } + if (!replyChannel.send(replyMessage, replyTimeout)) { + throw new MessageDeliveryException(replyMessage, + "unable to send reply message within alloted timeout of " + replyTimeout + " milliseconds"); + } + } + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageEndpoint.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageEndpoint.java index 89d30564f4..194ebc295b 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageEndpoint.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageEndpoint.java @@ -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. @@ -16,8 +16,10 @@ package org.springframework.integration.endpoint; +import org.springframework.beans.factory.InitializingBean; import org.springframework.context.Lifecycle; -import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.channel.ChannelRegistryAware; +import org.springframework.integration.message.Target; import org.springframework.integration.scheduling.Subscription; /** @@ -25,7 +27,7 @@ import org.springframework.integration.scheduling.Subscription; * * @author Mark Fisher */ -public interface MessageEndpoint extends MessageHandler, Lifecycle { +public interface MessageEndpoint extends Target, ChannelRegistryAware, InitializingBean, Lifecycle { String getName(); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/DefaultMessageEndpoint.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/TargetEndpoint.java similarity index 51% rename from spring-integration-core/src/main/java/org/springframework/integration/endpoint/DefaultMessageEndpoint.java rename to spring-integration-core/src/main/java/org/springframework/integration/endpoint/TargetEndpoint.java index ee186e4d8c..2591f47f0f 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/DefaultMessageEndpoint.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/TargetEndpoint.java @@ -29,37 +29,32 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanNameAware; -import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.DisposableBean; import org.springframework.integration.channel.ChannelRegistry; import org.springframework.integration.channel.ChannelRegistryAware; -import org.springframework.integration.channel.MessageChannel; -import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.handler.MessageHandlerNotRunningException; import org.springframework.integration.handler.MessageHandlerRejectedExecutionException; -import org.springframework.integration.handler.ReplyHandler; import org.springframework.integration.message.Message; -import org.springframework.integration.message.MessageDeliveryException; import org.springframework.integration.message.MessageHandlingException; -import org.springframework.integration.message.MessageHeader; +import org.springframework.integration.message.Target; import org.springframework.integration.message.selector.MessageSelector; import org.springframework.integration.message.selector.MessageSelectorRejectedException; import org.springframework.integration.scheduling.Subscription; import org.springframework.integration.util.ErrorHandler; import org.springframework.util.Assert; -import org.springframework.util.StringUtils; /** - * Default implementation of the {@link MessageEndpoint} interface. + * Base class for {@link MessageEndpoint} implementations. * * @author Mark Fisher */ -public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryAware, InitializingBean, BeanNameAware { +public class TargetEndpoint implements MessageEndpoint, BeanNameAware { - private final Log logger = LogFactory.getLog(this.getClass()); + protected final Log logger = LogFactory.getLog(this.getClass()); private volatile String name; - private volatile MessageHandler handler; + private volatile Target target; private volatile Subscription subscription; @@ -69,12 +64,6 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA private final List selectors = new CopyOnWriteArrayList(); - private volatile ReplyHandler replyHandler = new EndpointReplyHandler(); - - private volatile long replyTimeout = 1000; - - private volatile String defaultOutputChannelName; - private volatile ChannelRegistry channelRegistry; private volatile boolean initialized; @@ -82,9 +71,12 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA private volatile boolean running; - public DefaultMessageEndpoint(MessageHandler handler) { - Assert.notNull(handler, "handler must not be null"); - this.handler = handler; + public TargetEndpoint() { + } + + public TargetEndpoint(Target target) { + Assert.notNull(target, "target must not be null"); + this.target = target; } @@ -100,15 +92,13 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA this.setName(beanName); } - public MessageHandler getHandler() { - return this.handler; + public Target getTarget() { + return this.target; } - /** - * Set the handler to be invoked for each consumed message. - */ - public void setHandler(MessageHandler handler) { - this.handler = handler; + public void setTarget(Target target) { + Assert.notNull(target, "target must not be null"); + this.target = target; } public void setMessageSelectors(List selectors) { @@ -145,34 +135,6 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA return (this.errorHandler != null); } - public void setReplyHandler(ReplyHandler replyHandler) { - Assert.notNull(replyHandler, "'replyHandler' must not be null"); - this.replyHandler = replyHandler; - } - - /** - * Set the timeout in milliseconds to be enforced when this endpoint sends a - * reply message. If the message is not sent successfully within the - * allotted time, then it will be sent within a MessageDeliveryException to - * the error handler instead. The default replyTimeout value - * is 1000 milliseconds. - */ - public void setReplyTimeout(long replyTimeout) { - this.replyTimeout = replyTimeout; - } - - public String getDefaultOutputChannelName() { - return this.defaultOutputChannelName; - } - - /** - * Set the name of the channel to which this endpoint should send reply - * messages by default. - */ - public void setDefaultOutputChannelName(String defaultOutputChannelName) { - this.defaultOutputChannelName = defaultOutputChannelName; - } - /** * Set the channel registry to use for looking up channels by name. */ @@ -180,23 +142,25 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA this.channelRegistry = channelRegistry; } + protected ChannelRegistry getChannelRegistry() { + return this.channelRegistry; + } + public void afterPropertiesSet() { - if (this.handler instanceof ChannelRegistryAware) { - ((ChannelRegistryAware) this.handler).setChannelRegistry(this.channelRegistry); + if (this.target instanceof ChannelRegistryAware) { + ((ChannelRegistryAware) this.target).setChannelRegistry(this.channelRegistry); } - if (this.concurrencyPolicy != null && !(this.handler instanceof ConcurrentHandler)) { + if (this.concurrencyPolicy != null && !(this.target instanceof ConcurrentTarget)) { int capacity = this.concurrencyPolicy.getQueueCapacity(); BlockingQueue queue = (capacity < 1) ? new SynchronousQueue() : new ArrayBlockingQueue(capacity); - ExecutorService executor = new ThreadPoolExecutor( - this.concurrencyPolicy.getCoreSize(), this.concurrencyPolicy.getMaxSize(), + ExecutorService executor = new ThreadPoolExecutor(this.concurrencyPolicy.getCoreSize(), this.concurrencyPolicy.getMaxSize(), this.concurrencyPolicy.getKeepAliveSeconds(), TimeUnit.SECONDS, queue); - this.handler = new ConcurrentHandler(this.handler, executor); + this.target = new ConcurrentTarget(this.target, executor); } - if (this.handler instanceof ConcurrentHandler) { + if (this.target instanceof ConcurrentTarget) { if (this.errorHandler != null) { - ((ConcurrentHandler) this.handler).setErrorHandler(this.errorHandler); + ((ConcurrentTarget) this.target).setErrorHandler(this.errorHandler); } - ((ConcurrentHandler) this.handler).setReplyHandler(this.replyHandler); } this.initialized = true; } @@ -209,7 +173,7 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA if (this.isRunning()) { return; } - if (!initialized) { + if (!this.initialized) { this.afterPropertiesSet(); } this.running = true; @@ -219,10 +183,20 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA if (!this.isRunning()) { return; } + if (this.target instanceof DisposableBean) { + try { + ((DisposableBean) this.target).destroy(); + } + catch (Exception e) { + if (logger.isWarnEnabled()) { + logger.warn("exception occurred when destroying target", e); + } + } + } this.running = false; } - public final Message handle(Message message) { + public final boolean send(Message message) { if (logger.isDebugEnabled()) { logger.debug("endpoint '" + this + "' handling message: " + message); } @@ -235,69 +209,26 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA } } try { - Message replyMessage = this.handler.handle(message); - if (replyMessage != null) { - if (replyMessage.getHeader().getCorrelationId() == null) { - replyMessage.getHeader().setCorrelationId(message.getId()); - } - this.replyHandler.handle(replyMessage, message.getHeader()); - } + return this.target.send(message); } catch (MessageHandlerRejectedExecutionException e) { throw e; } catch (Throwable t) { if (this.errorHandler == null) { + if (t instanceof MessageHandlingException) { + throw (MessageHandlingException) t; + } throw new MessageHandlingException(message, "error occurred in endpoint, and no 'errorHandler' available", t); } this.errorHandler.handle(t); + return false; } - return null; } public String toString() { return (this.name != null) ? this.name : super.toString(); } - private MessageChannel resolveReplyChannel(MessageHeader originalMessageHeader) { - Object returnAddress = originalMessageHeader.getReturnAddress(); - if (returnAddress instanceof MessageChannel) { - return (MessageChannel) returnAddress; - } - if (returnAddress instanceof String && this.channelRegistry != null) { - String channelName = (String) returnAddress; - if (StringUtils.hasText(channelName)) { - return this.channelRegistry.lookupChannel(channelName); - } - } - if (this.defaultOutputChannelName != null && this.channelRegistry != null) { - return this.channelRegistry.lookupChannel(this.defaultOutputChannelName); - } - return null; - } - - - private class EndpointReplyHandler implements ReplyHandler { - - public void handle(Message replyMessage, MessageHeader originalMessageHeader) { - if (replyMessage == null) { - return; - } - MessageChannel replyChannel = resolveReplyChannel(originalMessageHeader); - if (replyChannel == null) { - throw new MessageHandlingException(replyMessage, - "Unable to determine reply channel for message. Provide a 'returnAddress' in the message header " + - "or a 'defaultOutputChannelName' on the message endpoint."); - } - if (logger.isDebugEnabled()) { - logger.debug("endpoint '" + DefaultMessageEndpoint.this + "' replying to channel '" + replyChannel + "' with message: " + replyMessage); - } - if (!replyChannel.send(replyMessage, replyTimeout)) { - throw new MessageDeliveryException(replyMessage, - "unable to send reply message within alloted timeout of " + replyTimeout + " milliseconds"); - } - } - } - } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java index 23c0f17957..8b83645050 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandlerAdapter.java @@ -24,7 +24,6 @@ import org.springframework.core.Ordered; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessageHeader; -import org.springframework.integration.message.MessageMapper; import org.springframework.util.Assert; /** @@ -123,7 +122,7 @@ public abstract class AbstractMessageHandlerAdapter implements MessageHandler } protected Message createReplyMessage(Object payload, MessageHeader originalMessageHeader) { - return new GenericMessage(payload, originalMessageHeader); + return new GenericMessage(payload, originalMessageHeader); } /** diff --git a/spring-integration-core/src/main/java/org/springframework/integration/message/AbstractMessageMapper.java b/spring-integration-core/src/main/java/org/springframework/integration/message/AbstractMessageMapper.java deleted file mode 100644 index 0b6cd55eae..0000000000 --- a/spring-integration-core/src/main/java/org/springframework/integration/message/AbstractMessageMapper.java +++ /dev/null @@ -1,43 +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.message; - -import org.springframework.integration.util.RandomUuidGenerator; -import org.springframework.integration.util.IdGenerator; -import org.springframework.util.Assert; - -/** - * Base class that provides the default {@link IdGenerator} as well as a setter - * for providing a custom id generator implementation. - * - * @author Mark Fisher - */ -public abstract class AbstractMessageMapper implements MessageMapper { - - private IdGenerator idGenerator = new RandomUuidGenerator(); - - - public void setIdGenerator(IdGenerator idGenerator) { - Assert.notNull(idGenerator, "'idGenerator' must not be null"); - this.idGenerator = idGenerator; - } - - protected IdGenerator getIdGenerator() { - return this.idGenerator; - } - -} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/ArgumentListPreparer.java b/spring-integration-core/src/main/java/org/springframework/integration/message/MessageCreator.java similarity index 70% rename from spring-integration-core/src/main/java/org/springframework/integration/adapter/ArgumentListPreparer.java rename to spring-integration-core/src/main/java/org/springframework/integration/message/MessageCreator.java index 3dd80ecd4f..0b1a8fac23 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/adapter/ArgumentListPreparer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/message/MessageCreator.java @@ -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. @@ -14,15 +14,15 @@ * limitations under the License. */ -package org.springframework.integration.adapter; +package org.springframework.integration.message; /** - * A strategy for preparing an argument list from a single source object. + * Strategy interface for creating a {@link Message} from an Object. * * @author Mark Fisher */ -public interface ArgumentListPreparer { +public interface MessageCreator { - Object[] prepare(Object source); + Message

createMessage(O object); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/message/MessageMapper.java b/spring-integration-core/src/main/java/org/springframework/integration/message/MessageMapper.java index f788cf5a51..ece5f6bfbc 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/message/MessageMapper.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/message/MessageMapper.java @@ -17,20 +17,15 @@ package org.springframework.integration.message; /** - * Strategy interface for mapping between messages and objects. + * Strategy interface for mapping from a {@link Message} to an Object. * * @author Mark Fisher */ -public interface MessageMapper { +public interface MessageMapper { /** - * Map to a {@link Message} from the given object. + * Map from the given {@link Message} to an Object. */ - Message toMessage(O source); - - /** - * Map from the given {@link Message} to an object. - */ - O fromMessage(Message message); + O mapMessage(Message

message); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/message/SimplePayloadMessageMapper.java b/spring-integration-core/src/main/java/org/springframework/integration/message/SimplePayloadMessageMapper.java deleted file mode 100644 index 4bcd3fad79..0000000000 --- a/spring-integration-core/src/main/java/org/springframework/integration/message/SimplePayloadMessageMapper.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.message; - -/** - * A {@link MessageMapper} implementation that simply wraps and unwraps a - * payload object in a {@link Message}. - * - * @author Mark Fisher - */ -public class SimplePayloadMessageMapper extends AbstractMessageMapper { - - /** - * Return the payload of the given Message. - */ - public T fromMessage(Message message) { - return message.getPayload(); - } - - /** - * Return a {@link Message} with the given object as its payload. - */ - public Message toMessage(T source) { - return new GenericMessage(this.getIdGenerator().generateId(), source); - } - -} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/Target.java b/spring-integration-core/src/main/java/org/springframework/integration/message/Target.java similarity index 70% rename from spring-integration-core/src/main/java/org/springframework/integration/adapter/Target.java rename to spring-integration-core/src/main/java/org/springframework/integration/message/Target.java index 8b2c4dae92..029344f600 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/adapter/Target.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/message/Target.java @@ -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. @@ -14,16 +14,15 @@ * limitations under the License. */ -package org.springframework.integration.adapter; +package org.springframework.integration.message; /** - * Interface for any external target that may receive data from an outgoing - * channel adapter. + * Interface for any target to which {@link Message Messages} can be sent. * * @author Mark Fisher */ -public interface Target { +public interface Target { - boolean send(T t); + boolean send(Message message); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/DefaultTargetAdapterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/adapter/DefaultTargetAdapterTests.java deleted file mode 100644 index 17d06e754b..0000000000 --- a/spring-integration-core/src/test/java/org/springframework/integration/adapter/DefaultTargetAdapterTests.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.adapter; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.TimeUnit; - -import org.junit.Test; - -import org.springframework.integration.bus.MessageBus; -import org.springframework.integration.channel.SimpleChannel; -import org.springframework.integration.message.GenericMessage; -import org.springframework.integration.message.Message; -import org.springframework.integration.scheduling.Subscription; - -/** - * @author Mark Fisher - */ -public class DefaultTargetAdapterTests { - - @Test - public void testAdapterSendsToChannel() throws Exception { - SynchronousQueue queue = new SynchronousQueue(); - TestBean testBean = new TestBean(queue); - MethodInvokingTarget target = new MethodInvokingTarget(); - target.setObject(testBean); - target.setMethod("foo"); - target.afterPropertiesSet(); - DefaultTargetAdapter adapter = new DefaultTargetAdapter(target); - SimpleChannel channel = new SimpleChannel(); - Subscription subscription = new Subscription(channel); - Message message = new GenericMessage("123", "testing"); - channel.send(message); - assertNull(queue.poll()); - MessageBus bus = new MessageBus(); - bus.registerChannel("channel", channel); - bus.registerHandler("targetAdapter", adapter, subscription); - bus.start(); - String result = queue.poll(500, TimeUnit.MILLISECONDS); - assertNotNull(result); - assertEquals("testing", result); - bus.stop(); - } - - - public static class TestBean { - - private BlockingQueue queue; - - public TestBean(BlockingQueue queue) { - this.queue = queue; - } - - public void foo(String s) { - try { - this.queue.put(s); - } - catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - } - -} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/MethodInvokingTargetTests.java b/spring-integration-core/src/test/java/org/springframework/integration/adapter/MethodInvokingTargetTests.java index 4e3196f743..556bca288c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/adapter/MethodInvokingTargetTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/MethodInvokingTargetTests.java @@ -16,11 +16,24 @@ package org.springframework.integration.adapter; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.TimeUnit; + import org.junit.Test; +import org.springframework.integration.bus.MessageBus; +import org.springframework.integration.channel.SimpleChannel; +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 @@ -29,40 +42,82 @@ public class MethodInvokingTargetTests { @Test public void testValidMethod() { - MethodInvokingTarget target = new MethodInvokingTarget(); + MethodInvokingTarget target = new MethodInvokingTarget(); target.setObject(new TestSink()); target.setMethod("validMethod"); target.afterPropertiesSet(); - boolean result = target.send("test"); + boolean result = target.send(new GenericMessage("test")); assertTrue(result); } @Test(expected=MessagingException.class) public void testInvalidMethodWithNoArgs() { - MethodInvokingTarget target = new MethodInvokingTarget(); + MethodInvokingTarget target = new MethodInvokingTarget(); target.setObject(new TestSink()); target.setMethod("invalidMethodWithNoArgs"); target.afterPropertiesSet(); - target.send("test"); + target.send(new StringMessage("test")); } - @Test - public void testValidMethodWithIgnoredReturnValue() { - MethodInvokingTarget target = new MethodInvokingTarget(); + @Test(expected=MessagingException.class) + public void testMethodWithReturnValue() { + MethodInvokingTarget target = new MethodInvokingTarget(); target.setObject(new TestSink()); - target.setMethod("validMethodWithIgnoredReturnValue"); + target.setMethod("methodWithReturnValue"); target.afterPropertiesSet(); - boolean result = target.send("test"); + boolean result = target.send(new StringMessage("test")); assertTrue(result); } @Test(expected=MessagingException.class) public void testNoMatchingMethodName() { - MethodInvokingTarget target = new MethodInvokingTarget(); + MethodInvokingTarget target = new MethodInvokingTarget(); target.setObject(new TestSink()); target.setMethod("noSuchMethod"); target.afterPropertiesSet(); - target.send("test"); + target.send(new StringMessage("test")); + } + + @Test + public void testSubscription() throws Exception { + SynchronousQueue queue = new SynchronousQueue(); + TestBean testBean = new TestBean(queue); + MethodInvokingTarget target = new MethodInvokingTarget(); + target.setObject(testBean); + target.setMethod("foo"); + target.afterPropertiesSet(); + SimpleChannel channel = new SimpleChannel(); + Subscription subscription = new Subscription(channel); + Message message = new GenericMessage("123", "testing"); + channel.send(message); + assertNull(queue.poll()); + MessageBus bus = new MessageBus(); + bus.registerChannel("channel", channel); + bus.registerHandler("targetAdapter", target, subscription); + bus.start(); + String result = queue.poll(500, TimeUnit.MILLISECONDS); + assertNotNull(result); + assertEquals("testing", result); + bus.stop(); + } + + + public static class TestBean { + + private BlockingQueue queue; + + public TestBean(BlockingQueue queue) { + this.queue = queue; + } + + public void foo(String s) { + try { + this.queue.put(s); + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/TestSink.java b/spring-integration-core/src/test/java/org/springframework/integration/adapter/TestSink.java index c45b6dd6f1..313ee6e070 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/adapter/TestSink.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/TestSink.java @@ -38,8 +38,8 @@ public class TestSink { public void invalidMethodWithNoArgs() { } - public String validMethodWithIgnoredReturnValue(String s) { - return "ignored"; + public String methodWithReturnValue(String s) { + return "value"; } public void store(String s) { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/adapterTests.xml b/spring-integration-core/src/test/java/org/springframework/integration/adapter/adapterTests.xml index 3f1386e04d..b348789c29 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/adapter/adapterTests.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/adapterTests.xml @@ -27,17 +27,13 @@ - - - - - - - + + + - - + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/bus/SynchronousChannelSubscriptionTests.java b/spring-integration-core/src/test/java/org/springframework/integration/bus/SynchronousChannelSubscriptionTests.java index ce060603d3..22c14e1904 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/bus/SynchronousChannelSubscriptionTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/bus/SynchronousChannelSubscriptionTests.java @@ -27,7 +27,7 @@ import org.springframework.integration.channel.MessageChannel; import org.springframework.integration.channel.SimpleChannel; import org.springframework.integration.config.MessageEndpointAnnotationPostProcessor; import org.springframework.integration.dispatcher.SynchronousChannel; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessagingException; @@ -55,7 +55,7 @@ public class SynchronousChannelSubscriptionTests { @Test public void testSendAndReceiveForRegisteredEndpoint() { - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new TestHandler()); + HandlerEndpoint endpoint = new HandlerEndpoint(new TestHandler()); endpoint.setSubscription(new Subscription("sourceChannel")); endpoint.setDefaultOutputChannelName("targetChannel"); bus.registerEndpoint("testEndpoint", endpoint); @@ -83,7 +83,7 @@ public class SynchronousChannelSubscriptionTests { public void testExceptionThrownFromRegisteredEndpoint() { SimpleChannel errorChannel = new SimpleChannel(); bus.setErrorChannel(errorChannel); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new MessageHandler() { + HandlerEndpoint endpoint = new HandlerEndpoint(new MessageHandler() { public Message handle(Message message) { throw new RuntimeException("intentional test failure"); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/bus/messageBusTests.xml b/spring-integration-core/src/test/java/org/springframework/integration/bus/messageBusTests.xml index 0c73be72b2..4e3bf0900b 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/bus/messageBusTests.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/bus/messageBusTests.xml @@ -10,7 +10,7 @@ - + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/AggregatorAnnotationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/AggregatorAnnotationTests.java index 714a89fd66..c6e26b171a 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/AggregatorAnnotationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/AggregatorAnnotationTests.java @@ -25,7 +25,7 @@ import org.springframework.beans.DirectFieldAccessor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.bus.MessageBus; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.handler.MessageHandlerChain; import org.springframework.integration.router.AggregatingMessageHandler; import org.springframework.integration.router.SequenceSizeCompletionStrategy; @@ -83,7 +83,7 @@ public class AggregatorAnnotationTests { private DirectFieldAccessor getDirectFieldAccessorForAggregatingHandler(ApplicationContext context, final String endpointName) { MessageBus messageBus = getMessageBus(context); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) messageBus.lookupEndpoint(endpointName + "-endpoint"); + HandlerEndpoint endpoint = (HandlerEndpoint) messageBus.lookupEndpoint(endpointName + "-endpoint"); MessageHandlerChain messageHandlerChain = (MessageHandlerChain) endpoint.getHandler(); AggregatingMessageHandler aggregatingMessageHandler = (AggregatingMessageHandler) ((List) new DirectFieldAccessor( messageHandlerChain).getPropertyValue("handlers")).get(0); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelParserTests.java index cc3ec0e0b7..2f3e2e03f7 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelParserTests.java @@ -32,12 +32,12 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.channel.DispatcherPolicy; import org.springframework.integration.channel.MessageChannel; import org.springframework.integration.dispatcher.DefaultMessageDispatcher; -import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessageDeliveryException; import org.springframework.integration.message.MessagePriority; import org.springframework.integration.message.StringMessage; +import org.springframework.integration.message.Target; import org.springframework.integration.scheduling.SimpleMessagingTaskScheduler; /** @@ -72,10 +72,10 @@ public class ChannelParserTests { DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); AtomicInteger counter = new AtomicInteger(); CountDownLatch latch = new CountDownLatch(1); - TestHandler handler1 = new TestHandler(counter, latch); - TestHandler handler2 = new TestHandler(counter, latch); - dispatcher.addHandler(handler1); - dispatcher.addHandler(handler2); + TestTarget target1 = new TestTarget(counter, latch); + TestTarget target2 = new TestTarget(counter, latch); + dispatcher.addTarget(target1); + dispatcher.addTarget(target2); dispatcher.start(); latch.await(500, TimeUnit.MILLISECONDS); assertEquals(0, latch.getCount()); @@ -92,10 +92,10 @@ public class ChannelParserTests { DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); AtomicInteger counter = new AtomicInteger(); CountDownLatch latch = new CountDownLatch(1); - TestHandler handler1 = new TestHandler(counter, latch); - TestHandler handler2 = new TestHandler(counter, latch); - dispatcher.addHandler(handler1); - dispatcher.addHandler(handler2); + TestTarget target1 = new TestTarget(counter, latch); + TestTarget target2 = new TestTarget(counter, latch); + dispatcher.addTarget(target1); + dispatcher.addTarget(target2); dispatcher.start(); latch.await(500, TimeUnit.MILLISECONDS); assertEquals(0, latch.getCount()); @@ -112,10 +112,10 @@ public class ChannelParserTests { DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); AtomicInteger counter = new AtomicInteger(); CountDownLatch latch = new CountDownLatch(2); - TestHandler handler1 = new TestHandler(counter, latch); - TestHandler handler2 = new TestHandler(counter, latch); - dispatcher.addHandler(handler1); - dispatcher.addHandler(handler2); + TestTarget target1 = new TestTarget(counter, latch); + TestTarget target2 = new TestTarget(counter, latch); + dispatcher.addTarget(target1); + dispatcher.addTarget(target2); dispatcher.start(); latch.await(500, TimeUnit.MILLISECONDS); assertEquals(0, latch.getCount()); @@ -270,21 +270,21 @@ public class ChannelParserTests { } - private static class TestHandler implements MessageHandler { + private static class TestTarget implements Target { private AtomicInteger counter; private CountDownLatch latch; - TestHandler(AtomicInteger counter, CountDownLatch latch) { + TestTarget(AtomicInteger counter, CountDownLatch latch) { this.counter = counter; this.latch = latch; } - public Message handle(Message message) { + public boolean send(Message message) { this.counter.incrementAndGet(); this.latch.countDown(); - return null; + return true; } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/CompletionStrategyAnnotationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/CompletionStrategyAnnotationTests.java index 04203304b2..0a2b0aa6b5 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/CompletionStrategyAnnotationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/CompletionStrategyAnnotationTests.java @@ -26,7 +26,7 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.bus.MessageBus; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.handler.MessageHandlerChain; import org.springframework.integration.router.AggregatingMessageHandler; import org.springframework.integration.router.CompletionStrategyAdapter; @@ -58,7 +58,7 @@ public class CompletionStrategyAnnotationTests { private DirectFieldAccessor getDirectFieldAccessorForAggregatingHandler(ApplicationContext context, final String endpointName) { MessageBus messageBus = getMessageBus(context); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) messageBus + HandlerEndpoint endpoint = (HandlerEndpoint) messageBus .lookupEndpoint(endpointName + "-endpoint"); MessageHandlerChain messageHandlerChain = (MessageHandlerChain) endpoint.getHandler(); AggregatingMessageHandler aggregatingMessageHandler = (AggregatingMessageHandler) ((List) new DirectFieldAccessor( diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/EndpointParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/EndpointParserTests.java index f5f9f6b28d..18bdbfe477 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/EndpointParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/EndpointParserTests.java @@ -29,14 +29,13 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.channel.MessageChannel; import org.springframework.integration.channel.SimpleChannel; import org.springframework.integration.endpoint.ConcurrencyPolicy; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; -import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessageHandlingException; import org.springframework.integration.message.StringMessage; +import org.springframework.integration.message.Target; import org.springframework.integration.message.selector.MessageSelectorRejectedException; -import org.springframework.integration.util.ErrorHandler; /** * @author Mark Fisher @@ -98,7 +97,7 @@ public class EndpointParserTests { public void testDefaultConcurrency() throws InterruptedException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "endpointConcurrencyTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("defaultConcurrencyEndpoint"); + HandlerEndpoint endpoint = (HandlerEndpoint) context.getBean("defaultConcurrencyEndpoint"); ConcurrencyPolicy concurrencyPolicy = endpoint.getConcurrencyPolicy(); assertEquals(ConcurrencyPolicy.DEFAULT_CORE_SIZE, concurrencyPolicy.getCoreSize()); assertEquals(ConcurrencyPolicy.DEFAULT_MAX_SIZE, concurrencyPolicy.getMaxSize()); @@ -110,7 +109,7 @@ public class EndpointParserTests { public void testConfiguredConcurrency() throws InterruptedException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "endpointConcurrencyTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("configuredConcurrencyEndpoint"); + HandlerEndpoint endpoint = (HandlerEndpoint) context.getBean("configuredConcurrencyEndpoint"); ConcurrencyPolicy concurrencyPolicy = endpoint.getConcurrencyPolicy(); assertEquals(7, concurrencyPolicy.getCoreSize()); assertEquals(77, concurrencyPolicy.getMaxSize()); @@ -122,12 +121,12 @@ public class EndpointParserTests { public void testEndpointWithSelectorAccepts() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "endpointWithSelectors.xml", this.getClass()); - MessageHandler endpoint = (MessageHandler) context.getBean("endpoint"); + Target endpoint = (Target) context.getBean("endpoint"); ((Lifecycle) endpoint).start(); Message message = new StringMessage("test"); MessageChannel replyChannel = new SimpleChannel(); message.getHeader().setReturnAddress(replyChannel); - endpoint.handle(message); + endpoint.send(message); Message reply = replyChannel.receive(500); assertNotNull(reply); assertEquals("foo", reply.getPayload()); @@ -137,20 +136,20 @@ public class EndpointParserTests { public void testEndpointWithSelectorRejects() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "endpointWithSelectors.xml", this.getClass()); - MessageHandler endpoint = (MessageHandler) context.getBean("endpoint"); + Target endpoint = (Target) context.getBean("endpoint"); ((Lifecycle) endpoint).start(); - endpoint.handle(new GenericMessage(123)); + endpoint.send(new GenericMessage(123)); } @Test public void testCustomErrorHandler() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "endpointWithErrorHandler.xml", this.getClass()); - MessageHandler endpoint = (MessageHandler) context.getBean("endpoint"); + Target endpoint = (Target) context.getBean("endpoint"); TestErrorHandler errorHandler = (TestErrorHandler) context.getBean("errorHandler"); assertNull(errorHandler.getLastError()); Message message = new StringMessage("test"); - endpoint.handle(message); + endpoint.send(message); Throwable error = errorHandler.getLastError(); assertEquals(MessageHandlingException.class, error.getClass()); MessageHandlingException exception = (MessageHandlingException) error; @@ -161,11 +160,11 @@ public class EndpointParserTests { public void testCustomReplyHandler() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "endpointWithReplyHandler.xml", this.getClass()); - MessageHandler endpoint = (MessageHandler) context.getBean("endpoint"); + Target endpoint = (Target) context.getBean("endpoint"); TestReplyHandler replyHandler = (TestReplyHandler) context.getBean("replyHandler"); assertNull(replyHandler.getLastMessage()); Message message = new StringMessage("test"); - endpoint.handle(message); + endpoint.send(message); Message reply = replyHandler.getLastMessage(); assertNotNull(reply); assertEquals("foo", reply.getPayload()); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/DefaultMessageDispatcherTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/DefaultMessageDispatcherTests.java index 7560a322c8..8a67d4fabc 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/DefaultMessageDispatcherTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/DefaultMessageDispatcherTests.java @@ -21,10 +21,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -32,11 +29,9 @@ import org.junit.Test; import org.springframework.integration.channel.DispatcherPolicy; import org.springframework.integration.channel.SimpleChannel; -import org.springframework.integration.dispatcher.DefaultMessageDispatcher; import org.springframework.integration.endpoint.ConcurrencyPolicy; -import org.springframework.integration.endpoint.ConcurrentHandler; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; -import org.springframework.integration.handler.InterceptingMessageHandler; +import org.springframework.integration.endpoint.HandlerEndpoint; +import org.springframework.integration.endpoint.MessageEndpoint; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.handler.MessageHandlerRejectedExecutionException; import org.springframework.integration.handler.TestHandlers; @@ -44,6 +39,7 @@ import org.springframework.integration.message.ErrorMessage; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessageDeliveryException; import org.springframework.integration.message.StringMessage; +import org.springframework.integration.message.Target; import org.springframework.integration.message.selector.PayloadTypeSelector; import org.springframework.integration.scheduling.MessagePublishingErrorHandler; import org.springframework.integration.scheduling.SimpleMessagingTaskScheduler; @@ -66,8 +62,8 @@ public class DefaultMessageDispatcherTests { SimpleChannel channel = new SimpleChannel(); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor())); - dispatcher.addHandler(new ConcurrentHandler(handler2, createExecutor())); + dispatcher.addTarget(createEndpoint(handler1, true)); + dispatcher.addTarget(createEndpoint(handler2, true)); dispatcher.start(); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); @@ -84,8 +80,8 @@ public class DefaultMessageDispatcherTests { SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true)); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor())); - dispatcher.addHandler(new ConcurrentHandler(handler2, createExecutor())); + dispatcher.addTarget(createEndpoint(handler1, true)); + dispatcher.addTarget(createEndpoint(handler2, true)); dispatcher.start(); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); @@ -102,14 +98,14 @@ public class DefaultMessageDispatcherTests { MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch); MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch); SimpleChannel channel = new SimpleChannel(); - channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - ConcurrentHandler inactiveHandler = new ConcurrentHandler(handler1, createExecutor()); - inactiveHandler.destroy(); - dispatcher.addHandler(inactiveHandler); - dispatcher.addHandler(new ConcurrentHandler(handler2, createExecutor())); - dispatcher.addHandler(new ConcurrentHandler(handler3, createExecutor())); + MessageEndpoint inactiveEndpoint = createEndpoint(handler1, true); + dispatcher.addTarget(inactiveEndpoint); + dispatcher.addTarget(createEndpoint(handler2, true)); + dispatcher.addTarget(createEndpoint(handler3, true)); dispatcher.start(); + inactiveEndpoint.stop(); + channel.send(new StringMessage(1, "test")); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); assertEquals("inactive handler should not have received message", 0, counter1.get()); @@ -126,14 +122,14 @@ public class DefaultMessageDispatcherTests { MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch); MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch); SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true)); - channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - ConcurrentHandler inactiveHandler = new ConcurrentHandler(handler2, createExecutor()); - inactiveHandler.destroy(); - dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor())); - dispatcher.addHandler(inactiveHandler); - dispatcher.addHandler(new ConcurrentHandler(handler3, createExecutor())); + MessageEndpoint inactiveEndpoint = createEndpoint(handler2, true); + dispatcher.addTarget(createEndpoint(handler1, true)); + dispatcher.addTarget(inactiveEndpoint); + dispatcher.addTarget(createEndpoint(handler3, true)); dispatcher.start(); + inactiveEndpoint.stop(); + channel.send(new StringMessage(1, "test")); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); assertEquals("inactive handler should not have received message", 0, counter2.get()); @@ -151,25 +147,22 @@ public class DefaultMessageDispatcherTests { @Test public void testBroadcastingDispatcherReachesRejectionLimitAndShouldFail() throws InterruptedException { final AtomicInteger counter1 = new AtomicInteger(); - final AtomicInteger counter2 = new AtomicInteger(); final AtomicInteger counter3 = new AtomicInteger(); final CountDownLatch latch = new CountDownLatch(2); MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch); - MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch); MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch); SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true)); channel.getDispatcherPolicy().setRejectionLimit(2); channel.getDispatcherPolicy().setRetryInterval(3); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor())); - dispatcher.addHandler(new ConcurrentHandler(handler2, createExecutor()) { - @Override - public Message handle(Message message) { + dispatcher.addTarget(createEndpoint(handler1, true)); + dispatcher.addTarget(new Target() { + public boolean send(Message message) { throw new MessageHandlerRejectedExecutionException(message); } }); - dispatcher.addHandler(new ConcurrentHandler(handler3, createExecutor())); + dispatcher.addTarget(createEndpoint(handler3, true)); SimpleChannel errorChannel = new SimpleChannel(); scheduler.setErrorHandler(new MessagePublishingErrorHandler(errorChannel)); dispatcher.start(); @@ -194,14 +187,14 @@ public class DefaultMessageDispatcherTests { channel.getDispatcherPolicy().setShouldFailOnRejectionLimit(false); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(handler1); - dispatcher.addHandler(new MessageHandler() { + dispatcher.addTarget(createEndpoint(handler1, false)); + dispatcher.addTarget(createEndpoint(new MessageHandler() { public Message handle(Message message) { latch.countDown(); throw new MessageHandlerRejectedExecutionException(message); } - }); - dispatcher.addHandler(handler2); + }, false)); + dispatcher.addTarget(createEndpoint(handler2, false)); dispatcher.start(); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); @@ -218,8 +211,8 @@ public class DefaultMessageDispatcherTests { DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); channel.getDispatcherPolicy().setRejectionLimit(2); channel.getDispatcherPolicy().setRetryInterval(3); - dispatcher.addHandler(handler1); - dispatcher.addHandler(handler2); + dispatcher.addTarget(createEndpoint(handler1, false)); + dispatcher.addTarget(createEndpoint(handler2, false)); SimpleChannel errorChannel = new SimpleChannel(); scheduler.setErrorHandler(new MessagePublishingErrorHandler(errorChannel)); dispatcher.start(); @@ -238,30 +231,26 @@ public class DefaultMessageDispatcherTests { final AtomicInteger rejectedCounter1 = new AtomicInteger(); final AtomicInteger rejectedCounter2 = new AtomicInteger(); final CountDownLatch latch = new CountDownLatch(4); - MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch); - MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch); SimpleChannel channel = new SimpleChannel(); channel.getDispatcherPolicy().setRejectionLimit(2); channel.getDispatcherPolicy().setRetryInterval(3); channel.getDispatcherPolicy().setShouldFailOnRejectionLimit(false); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor()) { - @Override + dispatcher.addTarget(createEndpoint(new MessageHandler() { public Message handle(Message message) { rejectedCounter1.incrementAndGet(); latch.countDown(); throw new MessageHandlerRejectedExecutionException(message); } - }); - dispatcher.addHandler(new ConcurrentHandler(handler2, createExecutor()) { - @Override + }, false)); + dispatcher.addTarget(createEndpoint(new MessageHandler() { public Message handle(Message message) { rejectedCounter2.incrementAndGet(); latch.countDown(); throw new MessageHandlerRejectedExecutionException(message); } - }); + }, false)); dispatcher.start(); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); @@ -280,9 +269,7 @@ public class DefaultMessageDispatcherTests { final AtomicInteger rejectedCounter2 = new AtomicInteger(); final AtomicInteger rejectedCounter3 = new AtomicInteger(); final CountDownLatch latch = new CountDownLatch(5); - MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch); - MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch); - MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch); + final MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch); DispatcherPolicy dispatcherPolicy = new DispatcherPolicy(); dispatcherPolicy.setRejectionLimit(2); dispatcherPolicy.setRetryInterval(3); @@ -290,33 +277,30 @@ public class DefaultMessageDispatcherTests { SimpleChannel channel = new SimpleChannel(25, dispatcherPolicy); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor()) { - @Override + dispatcher.addTarget(createEndpoint(new MessageHandler() { public Message handle(Message message) { rejectedCounter1.incrementAndGet(); latch.countDown(); throw new MessageHandlerRejectedExecutionException(message); } - }); - dispatcher.addHandler(new ConcurrentHandler(handler2, createExecutor()) { - @Override + }, false)); + dispatcher.addTarget(createEndpoint(new MessageHandler() { public Message handle(Message message) { if (rejectedCounter2.get() == 1) { - return super.handle(message); + return handler2.handle(message); } rejectedCounter2.incrementAndGet(); latch.countDown(); throw new MessageHandlerRejectedExecutionException(message); } - }); - dispatcher.addHandler(new ConcurrentHandler(handler3, createExecutor()) { - @Override + }, false)); + dispatcher.addTarget(createEndpoint(new MessageHandler() { public Message handle(Message message) { rejectedCounter3.incrementAndGet(); latch.countDown(); throw new MessageHandlerRejectedExecutionException(message); } - }); + }, false)); dispatcher.start(); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); @@ -335,8 +319,8 @@ public class DefaultMessageDispatcherTests { final AtomicInteger rejectedCounter1 = new AtomicInteger(); final AtomicInteger rejectedCounter2 = new AtomicInteger(); final CountDownLatch latch = new CountDownLatch(8); - MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch); - MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch); + final MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch); + final MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch); DispatcherPolicy dispatcherPolicy = new DispatcherPolicy(true); dispatcherPolicy.setRejectionLimit(5); dispatcherPolicy.setRetryInterval(3); @@ -344,28 +328,26 @@ public class DefaultMessageDispatcherTests { SimpleChannel channel = new SimpleChannel(25, dispatcherPolicy); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor()) { - @Override + dispatcher.addTarget(createEndpoint(new MessageHandler() { public Message handle(Message message) { if (rejectedCounter1.get() == 2) { - return super.handle(message); + return handler1.handle(message); } rejectedCounter1.incrementAndGet(); latch.countDown(); throw new MessageHandlerRejectedExecutionException(message); } - }); - dispatcher.addHandler(new ConcurrentHandler(handler2, createExecutor()) { - @Override + }, false)); + dispatcher.addTarget(createEndpoint(new MessageHandler() { public Message handle(Message message) { if (rejectedCounter2.get() == 4) { - return super.handle(message); + return handler2.handle(message); } rejectedCounter2.incrementAndGet(); latch.countDown(); throw new MessageHandlerRejectedExecutionException(message); } - }); + }, false)); dispatcher.start(); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); @@ -385,12 +367,12 @@ public class DefaultMessageDispatcherTests { SimpleChannel channel = new SimpleChannel(); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - DefaultMessageEndpoint endpoint1 = new DefaultMessageEndpoint(handler1); - DefaultMessageEndpoint endpoint2 = new DefaultMessageEndpoint(handler2); + HandlerEndpoint endpoint1 = new HandlerEndpoint(handler1); + HandlerEndpoint endpoint2 = new HandlerEndpoint(handler2); endpoint1.addMessageSelector(new PayloadTypeSelector(Integer.class)); endpoint2.addMessageSelector(new PayloadTypeSelector(String.class)); - dispatcher.addHandler(endpoint1); - dispatcher.addHandler(endpoint2); + dispatcher.addTarget(endpoint1); + dispatcher.addTarget(endpoint2); dispatcher.start(); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); @@ -411,28 +393,30 @@ public class DefaultMessageDispatcherTests { SimpleChannel channel = new SimpleChannel(); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - DefaultMessageEndpoint endpoint1 = new DefaultMessageEndpoint(handler1); - DefaultMessageEndpoint endpoint2 = new DefaultMessageEndpoint(handler2); + final HandlerEndpoint endpoint1 = new HandlerEndpoint(handler1); + final HandlerEndpoint endpoint2 = new HandlerEndpoint(handler2); endpoint1.addMessageSelector(new PayloadTypeSelector(Integer.class)); endpoint2.addMessageSelector(new PayloadTypeSelector(Integer.class)); - MessageHandler interceptor1 = new InterceptingMessageHandler(endpoint1) { - @Override - public Message handle(Message message, MessageHandler target) { + endpoint1.start(); + endpoint2.start(); + MessageHandler interceptor1 = new MessageHandler() { + public Message handle(Message message) { attemptedCounter1.incrementAndGet(); attemptedLatch.countDown(); - return target.handle(message); + endpoint1.send(message); + return null; } }; - MessageHandler interceptor2 = new InterceptingMessageHandler(endpoint2) { - @Override - public Message handle(Message message, MessageHandler target) { + MessageHandler interceptor2 = new MessageHandler() { + public Message handle(Message message) { attemptedCounter2.incrementAndGet(); attemptedLatch.countDown(); - return target.handle(message); + endpoint2.send(message); + return null; } }; - dispatcher.addHandler(interceptor1); - dispatcher.addHandler(interceptor2); + dispatcher.addTarget(createEndpoint(interceptor1, false)); + dispatcher.addTarget(createEndpoint(interceptor2, false)); dispatcher.start(); attemptedLatch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, attemptedLatch.getCount()); @@ -454,14 +438,14 @@ public class DefaultMessageDispatcherTests { SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true)); channel.send(new StringMessage(1, "test")); DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler); - DefaultMessageEndpoint endpoint1 = new DefaultMessageEndpoint(handler1); + HandlerEndpoint endpoint1 = new HandlerEndpoint(handler1); endpoint1.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1)); - DefaultMessageEndpoint endpoint2 = new DefaultMessageEndpoint(handler2); + HandlerEndpoint endpoint2 = new HandlerEndpoint(handler2); endpoint2.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1)); endpoint1.addMessageSelector(new PayloadTypeSelector(Integer.class)); endpoint2.addMessageSelector(new PayloadTypeSelector(String.class)); - dispatcher.addHandler(endpoint1); - dispatcher.addHandler(endpoint2); + dispatcher.addTarget(endpoint1); + dispatcher.addTarget(endpoint2); dispatcher.start(); latch.await(2000, TimeUnit.MILLISECONDS); assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount()); @@ -470,8 +454,13 @@ public class DefaultMessageDispatcherTests { } - private static ExecutorService createExecutor() { - return new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new SynchronousQueue()); + private static MessageEndpoint createEndpoint(MessageHandler handler, boolean asynchronous) { + HandlerEndpoint endpoint = new HandlerEndpoint(handler); + if (asynchronous) { + endpoint.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1)); + } + endpoint.afterPropertiesSet(); + return endpoint; } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/DefaultMessageDistributorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/DefaultMessageDistributorTests.java index fd88f11b02..8caa9d97ef 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/DefaultMessageDistributorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/DefaultMessageDistributorTests.java @@ -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. @@ -25,8 +25,11 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.springframework.integration.channel.DispatcherPolicy; +import org.springframework.integration.endpoint.HandlerEndpoint; +import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.handler.TestHandlers; import org.springframework.integration.message.StringMessage; +import org.springframework.integration.message.Target; /** * @author Mark Fisher @@ -37,7 +40,7 @@ public class DefaultMessageDistributorTests { public void testSingleMessage() throws InterruptedException { MessageDistributor distributor = new DefaultMessageDistributor(new DispatcherPolicy()); final CountDownLatch latch = new CountDownLatch(1); - distributor.addHandler(TestHandlers.countDownHandler(latch)); + distributor.addTarget(createEndpoint(TestHandlers.countDownHandler(latch))); distributor.distribute(new StringMessage("test")); latch.await(500, TimeUnit.MILLISECONDS); assertEquals(0, latch.getCount()); @@ -49,8 +52,8 @@ public class DefaultMessageDistributorTests { final CountDownLatch latch = new CountDownLatch(1); final AtomicInteger counter1 = new AtomicInteger(); final AtomicInteger counter2 = new AtomicInteger(); - distributor.addHandler(TestHandlers.countingCountDownHandler(counter1, latch)); - distributor.addHandler(TestHandlers.countingCountDownHandler(counter2, latch)); + distributor.addTarget(createEndpoint(TestHandlers.countingCountDownHandler(counter1, latch))); + distributor.addTarget(createEndpoint(TestHandlers.countingCountDownHandler(counter2, latch))); distributor.distribute(new StringMessage("test")); latch.await(500, TimeUnit.MILLISECONDS); assertEquals(0, latch.getCount()); @@ -63,8 +66,8 @@ public class DefaultMessageDistributorTests { final CountDownLatch latch = new CountDownLatch(2); final AtomicInteger counter1 = new AtomicInteger(); final AtomicInteger counter2 = new AtomicInteger(); - distributor.addHandler(TestHandlers.countingCountDownHandler(counter1, latch)); - distributor.addHandler(TestHandlers.countingCountDownHandler(counter2, latch)); + distributor.addTarget(createEndpoint(TestHandlers.countingCountDownHandler(counter1, latch))); + distributor.addTarget(createEndpoint(TestHandlers.countingCountDownHandler(counter2, latch))); distributor.distribute(new StringMessage("test")); latch.await(500, TimeUnit.MILLISECONDS); assertEquals(0, latch.getCount()); @@ -72,4 +75,11 @@ public class DefaultMessageDistributorTests { assertEquals(1, counter2.get()); } + + private static Target createEndpoint(MessageHandler handler) { + HandlerEndpoint endpoint = new HandlerEndpoint(handler); + endpoint.start(); + return endpoint; + } + } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/SynchronousChannelTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/SynchronousChannelTests.java index 25d119b0f7..989e962330 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/SynchronousChannelTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dispatcher/SynchronousChannelTests.java @@ -28,10 +28,10 @@ import java.util.concurrent.TimeUnit; import org.junit.Test; -import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.Message; import org.springframework.integration.message.PollableSource; import org.springframework.integration.message.StringMessage; +import org.springframework.integration.message.Target; /** * @author Mark Fisher @@ -44,7 +44,7 @@ public class SynchronousChannelTests { @Test public void testSend() { SynchronousChannel channel = new SynchronousChannel(); - channel.addHandler(new ThreadNameSettingTestHandler()); + channel.addTarget(new ThreadNameSettingTestTarget()); StringMessage message = new StringMessage("test"); assertTrue(channel.send(message)); String handlerThreadName = message.getHeader().getProperty(HANDLER_THREAD); @@ -82,7 +82,7 @@ public class SynchronousChannelTests { public void testSendInSeparateThread() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); final SynchronousChannel channel = new SynchronousChannel(); - channel.addHandler(new ThreadNameSettingTestHandler(latch)); + channel.addTarget(new ThreadNameSettingTestTarget(latch)); final StringMessage message = new StringMessage("test"); new Thread(new Runnable() { public void run() { @@ -152,25 +152,25 @@ public class SynchronousChannelTests { } - private static class ThreadNameSettingTestHandler implements MessageHandler { + private static class ThreadNameSettingTestTarget implements Target { private final CountDownLatch latch; - ThreadNameSettingTestHandler() { + ThreadNameSettingTestTarget() { this(null); } - ThreadNameSettingTestHandler(CountDownLatch latch) { + ThreadNameSettingTestTarget(CountDownLatch latch) { this.latch = latch; } - public Message handle(Message message) { + public boolean send(Message message) { message.getHeader().setProperty(HANDLER_THREAD, Thread.currentThread().getName()); if (this.latch != null) { this.latch.countDown(); } - return null; + return true; } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/DefaultMessageEndpointTests.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/HandlerEndpointTests.java similarity index 83% rename from spring-integration-core/src/test/java/org/springframework/integration/endpoint/DefaultMessageEndpointTests.java rename to spring-integration-core/src/test/java/org/springframework/integration/endpoint/HandlerEndpointTests.java index 7bec2fe98a..58df061cf7 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/DefaultMessageEndpointTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/HandlerEndpointTests.java @@ -23,9 +23,6 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -48,7 +45,7 @@ import org.springframework.integration.util.ErrorHandler; /** * @author Mark Fisher */ -public class DefaultMessageEndpointTests { +public class HandlerEndpointTests { @Test public void testDefaultReplyChannel() throws Exception { @@ -60,11 +57,11 @@ public class DefaultMessageEndpointTests { return new StringMessage("123", "hello " + message.getPayload()); } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handler); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); endpoint.setChannelRegistry(channelRegistry); endpoint.setDefaultOutputChannelName("replyChannel"); endpoint.start(); - endpoint.handle(new StringMessage(1, "test")); + endpoint.send(new StringMessage(1, "test")); endpoint.stop(); Message reply = replyChannel.receive(50); assertNotNull(reply); @@ -79,11 +76,11 @@ public class DefaultMessageEndpointTests { return new StringMessage("123", "hello " + message.getPayload()); } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handler); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); endpoint.start(); StringMessage testMessage = new StringMessage(1, "test"); testMessage.getHeader().setReturnAddress(replyChannel); - endpoint.handle(testMessage); + endpoint.send(testMessage); endpoint.stop(); Message reply = replyChannel.receive(50); assertNotNull(reply); @@ -100,12 +97,12 @@ public class DefaultMessageEndpointTests { return new StringMessage("123", "hello " + message.getPayload()); } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handler); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); endpoint.setChannelRegistry(channelRegistry); endpoint.start(); StringMessage testMessage = new StringMessage(1, "test"); testMessage.getHeader().setReturnAddress("replyChannel"); - endpoint.handle(testMessage); + endpoint.send(testMessage); endpoint.stop(); Message reply = replyChannel.receive(50); assertNotNull(reply); @@ -123,19 +120,19 @@ public class DefaultMessageEndpointTests { return new StringMessage("123", "hello " + message.getPayload()); } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handler); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); endpoint.setChannelRegistry(channelRegistry); endpoint.start(); StringMessage testMessage = new StringMessage("test"); testMessage.getHeader().setReturnAddress(replyChannel1); - endpoint.handle(testMessage); + endpoint.send(testMessage); Message reply1 = replyChannel1.receive(50); assertNotNull(reply1); assertEquals("hello test", reply1.getPayload()); Message reply2 = replyChannel2.receive(0); assertNull(reply2); testMessage.getHeader().setReturnAddress("replyChannel2"); - endpoint.handle(testMessage); + endpoint.send(testMessage); reply1 = replyChannel1.receive(0); assertNull(reply1); reply2 = replyChannel2.receive(0); @@ -147,7 +144,7 @@ public class DefaultMessageEndpointTests { @Test public void testCustomErrorHandler() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(2); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(TestHandlers.rejectingCountDownHandler(latch)); + HandlerEndpoint endpoint = new HandlerEndpoint(TestHandlers.rejectingCountDownHandler(latch)); endpoint.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1)); endpoint.setErrorHandler(new ErrorHandler() { public void handle(Throwable t) { @@ -155,7 +152,7 @@ public class DefaultMessageEndpointTests { } }); endpoint.start(); - endpoint.handle(new StringMessage("test")); + endpoint.send(new StringMessage("test")); latch.await(500, TimeUnit.MILLISECONDS); assertEquals("both handler and errorHandler should have been invoked", 0, latch.getCount()); } @@ -172,13 +169,14 @@ public class DefaultMessageEndpointTests { return new StringMessage("123", "hello " + message.getPayload()); } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new ConcurrentHandler(handler, createExecutor())); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); + endpoint.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1)); endpoint.setChannelRegistry(channelRegistry); endpoint.setDefaultOutputChannelName("replyChannel"); endpoint.start(); - endpoint.handle(new StringMessage(1, "test")); - endpoint.stop(); + endpoint.send(new StringMessage(1, "test")); latch.await(500, TimeUnit.MILLISECONDS); + endpoint.stop(); assertEquals("handler should have been invoked within allotted time", 0, latch.getCount()); Message reply = replyChannel.receive(100); assertNotNull(reply); @@ -197,11 +195,11 @@ public class DefaultMessageEndpointTests { return null; } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handler); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); endpoint.setChannelRegistry(channelRegistry); endpoint.setDefaultOutputChannelName("replyChannel"); endpoint.start(); - endpoint.handle(new StringMessage(1, "test")); + endpoint.send(new StringMessage(1, "test")); endpoint.stop(); latch.await(500, TimeUnit.MILLISECONDS); assertEquals("handler should have been invoked within allotted time", 0, latch.getCount()); @@ -221,13 +219,14 @@ public class DefaultMessageEndpointTests { return null; } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new ConcurrentHandler(handler, createExecutor())); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); + endpoint.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1)); endpoint.setChannelRegistry(channelRegistry); endpoint.setDefaultOutputChannelName("replyChannel"); endpoint.start(); - endpoint.handle(new StringMessage(1, "test")); - endpoint.stop(); + endpoint.send(new StringMessage(1, "test")); latch.await(500, TimeUnit.MILLISECONDS); + endpoint.stop(); assertEquals("handler should have been invoked within allotted time", 0, latch.getCount()); Message reply = replyChannel.receive(0); assertNull(reply); @@ -245,16 +244,17 @@ public class DefaultMessageEndpointTests { return new StringMessage("123", "hello " + message.getPayload()); } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new ConcurrentHandler(handler, createExecutor())); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); + endpoint.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1)); endpoint.setChannelRegistry(channelRegistry); endpoint.start(); StringMessage message = new StringMessage(1, "test"); message.getHeader().setReturnAddress("replyChannel"); - endpoint.handle(message); - endpoint.stop(); + endpoint.send(message); latch.await(500, TimeUnit.MILLISECONDS); assertEquals("handler should have been invoked within allotted time", 0, latch.getCount()); Message reply = replyChannel.receive(100); + endpoint.stop(); assertNotNull(reply); assertEquals("hello test", reply.getPayload()); } @@ -271,14 +271,14 @@ public class DefaultMessageEndpointTests { return new StringMessage("123", "hello " + message.getPayload()); } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handler); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); endpoint.setChannelRegistry(channelRegistry); endpoint.setConcurrencyPolicy(new ConcurrencyPolicy(3, 14)); endpoint.setDefaultOutputChannelName("replyChannel"); endpoint.start(); - endpoint.handle(new StringMessage(1, "test")); - endpoint.stop(); + endpoint.send(new StringMessage(1, "test")); latch.await(500, TimeUnit.MILLISECONDS); + endpoint.stop(); assertEquals("handler should have been invoked within allotted time", 0, latch.getCount()); Message reply = replyChannel.receive(100); assertNotNull(reply); @@ -297,15 +297,15 @@ public class DefaultMessageEndpointTests { return new StringMessage("123", "hello " + message.getPayload()); } }; - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(handler); + HandlerEndpoint endpoint = new HandlerEndpoint(handler); endpoint.setChannelRegistry(channelRegistry); endpoint.setConcurrencyPolicy(new ConcurrencyPolicy(3, 14)); endpoint.start(); StringMessage message = new StringMessage(1, "test"); message.getHeader().setReturnAddress("replyChannel"); - endpoint.handle(message); - endpoint.stop(); + endpoint.send(message); latch.await(500, TimeUnit.MILLISECONDS); + endpoint.stop(); assertEquals("handler should have been invoked within allotted time", 0, latch.getCount()); Message reply = replyChannel.receive(100); assertNotNull(reply); @@ -314,20 +314,20 @@ public class DefaultMessageEndpointTests { @Test(expected=MessageHandlerNotRunningException.class) public void testEndpointDoesNotHandleMessagesWhenNotYetStarted() { - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(TestHandlers.nullHandler()); - endpoint.handle(new StringMessage("test")); + HandlerEndpoint endpoint = new HandlerEndpoint(TestHandlers.nullHandler()); + endpoint.send(new StringMessage("test")); } @Test public void testEndpointDoesNotHandleMessagesAfterBeingStopped() { AtomicInteger counter = new AtomicInteger(); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(TestHandlers.countingHandler(counter)); + HandlerEndpoint endpoint = new HandlerEndpoint(TestHandlers.countingHandler(counter)); boolean exceptionThrown = false; try { endpoint.start(); - endpoint.handle(new StringMessage("test1")); + endpoint.send(new StringMessage("test1")); endpoint.stop(); - endpoint.handle(new StringMessage("test2")); + endpoint.send(new StringMessage("test2")); } catch (MessageHandlerNotRunningException e) { exceptionThrown = true; @@ -338,27 +338,27 @@ public class DefaultMessageEndpointTests { @Test(expected=MessageSelectorRejectedException.class) public void testEndpointWithSelectorRejecting() { - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(TestHandlers.nullHandler()); + HandlerEndpoint endpoint = new HandlerEndpoint(TestHandlers.nullHandler()); endpoint.addMessageSelector(new MessageSelector() { public boolean accept(Message message) { return false; } }); endpoint.start(); - endpoint.handle(new StringMessage("test")); + endpoint.send(new StringMessage("test")); } @Test public void testEndpointWithSelectorAccepting() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(TestHandlers.countDownHandler(latch)); + HandlerEndpoint endpoint = new HandlerEndpoint(TestHandlers.countDownHandler(latch)); endpoint.addMessageSelector(new MessageSelector() { public boolean accept(Message message) { return true; } }); endpoint.start(); - endpoint.handle(new StringMessage("test")); + endpoint.send(new StringMessage("test")); latch.await(100, TimeUnit.MILLISECONDS); assertEquals("handler should have been invoked", 0, latch.getCount()); endpoint.stop(); @@ -367,7 +367,7 @@ public class DefaultMessageEndpointTests { @Test public void testEndpointWithMultipleSelectorsAndFirstRejects() { final AtomicInteger counter = new AtomicInteger(); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(TestHandlers.countingHandler(counter)); + HandlerEndpoint endpoint = new HandlerEndpoint(TestHandlers.countingHandler(counter)); boolean exceptionThrown = false; endpoint.addMessageSelector(new MessageSelector() { public boolean accept(Message message) { @@ -383,7 +383,7 @@ public class DefaultMessageEndpointTests { }); endpoint.start(); try { - endpoint.handle(new StringMessage("test")); + endpoint.send(new StringMessage("test")); } catch (MessageSelectorRejectedException e) { exceptionThrown = true; @@ -396,7 +396,7 @@ public class DefaultMessageEndpointTests { @Test public void testEndpointWithMultipleSelectorsAndFirstAccepts() { final AtomicInteger counter = new AtomicInteger(); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(TestHandlers.countingHandler(counter)); + HandlerEndpoint endpoint = new HandlerEndpoint(TestHandlers.countingHandler(counter)); boolean exceptionThrown = false; endpoint.addMessageSelector(new MessageSelector() { public boolean accept(Message message) { @@ -412,7 +412,7 @@ public class DefaultMessageEndpointTests { }); endpoint.start(); try { - endpoint.handle(new StringMessage("test")); + endpoint.send(new StringMessage("test")); } catch (MessageSelectorRejectedException e) { exceptionThrown = true; @@ -425,7 +425,7 @@ public class DefaultMessageEndpointTests { @Test public void testEndpointWithMultipleSelectorsAndBothAccept() { final AtomicInteger counter = new AtomicInteger(); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(TestHandlers.countingHandler(counter)); + HandlerEndpoint endpoint = new HandlerEndpoint(TestHandlers.countingHandler(counter)); endpoint.addMessageSelector(new MessageSelector() { public boolean accept(Message message) { counter.incrementAndGet(); @@ -439,7 +439,7 @@ public class DefaultMessageEndpointTests { } }); endpoint.start(); - endpoint.handle(new StringMessage("test")); + endpoint.send(new StringMessage("test")); assertEquals("both selectors and handler should have been invoked", 3, counter.get()); endpoint.stop(); } @@ -449,7 +449,7 @@ public class DefaultMessageEndpointTests { SimpleChannel output = new SimpleChannel(1); ChannelRegistry channelRegistry = new DefaultChannelRegistry(); channelRegistry.registerChannel("output", output); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new MessageHandler() { + HandlerEndpoint endpoint = new HandlerEndpoint(new MessageHandler() { public Message handle(Message message) { return message; } @@ -460,9 +460,9 @@ public class DefaultMessageEndpointTests { endpoint.setErrorHandler(errorHandler); endpoint.setReplyTimeout(0); endpoint.start(); - endpoint.handle(new StringMessage("test1")); + endpoint.send(new StringMessage("test1")); assertNull(errorHandler.getLastError()); - endpoint.handle(new StringMessage("test2")); + endpoint.send(new StringMessage("test2")); Throwable error = errorHandler.getLastError(); assertNotNull(error); assertEquals(MessageDeliveryException.class, error.getClass()); @@ -472,7 +472,7 @@ public class DefaultMessageEndpointTests { @Test public void testReturnAddressChannelTimeoutSendsToErrorHandler() { SimpleChannel replyChannel = new SimpleChannel(1); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new MessageHandler() { + HandlerEndpoint endpoint = new HandlerEndpoint(new MessageHandler() { public Message handle(Message message) { return message; } @@ -483,11 +483,11 @@ public class DefaultMessageEndpointTests { endpoint.start(); Message message1 = new StringMessage("test1"); message1.getHeader().setReturnAddress(replyChannel); - endpoint.handle(message1); + endpoint.send(message1); assertNull(errorHandler.getLastError()); Message message2 = new StringMessage("test2"); message2.getHeader().setReturnAddress(replyChannel); - endpoint.handle(message2); + endpoint.send(message2); Throwable error = errorHandler.getLastError(); assertNotNull(error); assertEquals(MessageDeliveryException.class, error.getClass()); @@ -499,7 +499,7 @@ public class DefaultMessageEndpointTests { SimpleChannel replyChannel = new SimpleChannel(1); ChannelRegistry channelRegistry = new DefaultChannelRegistry(); channelRegistry.registerChannel("replyChannel", replyChannel); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new MessageHandler() { + HandlerEndpoint endpoint = new HandlerEndpoint(new MessageHandler() { public Message handle(Message message) { return message; } @@ -511,11 +511,11 @@ public class DefaultMessageEndpointTests { endpoint.start(); Message message1 = new StringMessage("test1"); message1.getHeader().setReturnAddress("replyChannel"); - endpoint.handle(message1); + endpoint.send(message1); assertNull(errorHandler.getLastError()); Message message2 = new StringMessage("test2"); message2.getHeader().setReturnAddress("replyChannel"); - endpoint.handle(message2); + endpoint.send(message2); Throwable error = errorHandler.getLastError(); assertNotNull(error); assertEquals(MessageDeliveryException.class, error.getClass()); @@ -525,7 +525,7 @@ public class DefaultMessageEndpointTests { @Test public void testCorrelationId() { SimpleChannel replyChannel = new SimpleChannel(1); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new MessageHandler() { + HandlerEndpoint endpoint = new HandlerEndpoint(new MessageHandler() { public Message handle(Message message) { return message; } @@ -533,7 +533,7 @@ public class DefaultMessageEndpointTests { endpoint.start(); Message message = new StringMessage("test"); message.getHeader().setReturnAddress(replyChannel); - endpoint.handle(message); + endpoint.send(message); Message reply = replyChannel.receive(500); assertEquals(message.getId(), reply.getHeader().getCorrelationId()); } @@ -541,7 +541,7 @@ public class DefaultMessageEndpointTests { @Test public void testCorrelationIdSetByHandlerTakesPrecedence() { SimpleChannel replyChannel = new SimpleChannel(1); - DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint(new MessageHandler() { + HandlerEndpoint endpoint = new HandlerEndpoint(new MessageHandler() { public Message handle(Message message) { message.getHeader().setCorrelationId("ABC-123"); return message; @@ -550,7 +550,7 @@ public class DefaultMessageEndpointTests { endpoint.start(); Message message = new StringMessage("test"); message.getHeader().setReturnAddress(replyChannel); - endpoint.handle(message); + endpoint.send(message); Message reply = replyChannel.receive(500); Object correlationId = reply.getHeader().getCorrelationId(); assertFalse(message.getId().equals(correlationId)); @@ -558,11 +558,6 @@ public class DefaultMessageEndpointTests { } - private static ExecutorService createExecutor() { - return new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new SynchronousQueue()); - } - - private static class TestErrorHandler implements ErrorHandler { private volatile Throwable lastError; diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/MessageEndpointAnnotationPostProcessorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/MessageEndpointAnnotationPostProcessorTests.java index 1a5ab81d2f..7c1e3237ae 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/MessageEndpointAnnotationPostProcessorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/annotation/MessageEndpointAnnotationPostProcessorTests.java @@ -42,7 +42,7 @@ import org.springframework.integration.channel.MessageChannel; import org.springframework.integration.channel.SimpleChannel; import org.springframework.integration.config.MessageEndpointAnnotationPostProcessor; import org.springframework.integration.endpoint.ConcurrencyPolicy; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.message.Message; import org.springframework.integration.message.StringMessage; @@ -130,7 +130,7 @@ public class MessageEndpointAnnotationPostProcessorTests { postProcessor.afterPropertiesSet(); ConcurrencyAnnotationTestBean testBean = new ConcurrencyAnnotationTestBean(); postProcessor.postProcessAfterInitialization(testBean, "testBean"); - DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) messageBus.lookupEndpoint("testBean-endpoint"); + HandlerEndpoint endpoint = (HandlerEndpoint) messageBus.lookupEndpoint("testBean-endpoint"); ConcurrencyPolicy concurrencyPolicy = endpoint.getConcurrencyPolicy(); assertEquals(17, concurrencyPolicy.getCoreSize()); assertEquals(42, concurrencyPolicy.getMaxSize()); diff --git a/spring-integration-ws/src/main/java/org/springframework/integration/ws/config/WebServiceTargetAdapterParser.java b/spring-integration-ws/src/main/java/org/springframework/integration/ws/config/WebServiceTargetAdapterParser.java index 1f67fa1fef..154f95438c 100644 --- a/spring-integration-ws/src/main/java/org/springframework/integration/ws/config/WebServiceTargetAdapterParser.java +++ b/spring-integration-ws/src/main/java/org/springframework/integration/ws/config/WebServiceTargetAdapterParser.java @@ -25,7 +25,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.ConfigurationException; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.scheduling.Subscription; import org.springframework.integration.ws.adapter.MarshallingWebServiceTargetAdapter; import org.springframework.integration.ws.adapter.SimpleWebServiceTargetAdapter; @@ -39,7 +39,7 @@ import org.springframework.util.StringUtils; public class WebServiceTargetAdapterParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { - return DefaultMessageEndpoint.class; + return HandlerEndpoint.class; } protected boolean shouldGenerateId() { diff --git a/spring-integration-ws/src/test/java/org/springframework/integration/ws/config/WebServiceTargetAdapterParserTests.java b/spring-integration-ws/src/test/java/org/springframework/integration/ws/config/WebServiceTargetAdapterParserTests.java index 27da9b33af..a0f0258671 100644 --- a/spring-integration-ws/src/test/java/org/springframework/integration/ws/config/WebServiceTargetAdapterParserTests.java +++ b/spring-integration-ws/src/test/java/org/springframework/integration/ws/config/WebServiceTargetAdapterParserTests.java @@ -23,7 +23,7 @@ import org.junit.Test; import org.springframework.beans.DirectFieldAccessor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.integration.endpoint.DefaultMessageEndpoint; +import org.springframework.integration.endpoint.HandlerEndpoint; import org.springframework.integration.ws.adapter.MarshallingWebServiceTargetAdapter; import org.springframework.integration.ws.adapter.SimpleWebServiceTargetAdapter; import org.springframework.oxm.Marshaller; @@ -39,8 +39,8 @@ public class WebServiceTargetAdapterParserTests { public void testSimpleWebServiceTargetAdapterWithDefaultSourceExtractor() { ApplicationContext context = new ClassPathXmlApplicationContext( "simpleWebServiceTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = - (DefaultMessageEndpoint) context.getBean("adapterWithDefaultSourceExtractor"); + HandlerEndpoint endpoint = + (HandlerEndpoint) context.getBean("adapterWithDefaultSourceExtractor"); assertEquals(SimpleWebServiceTargetAdapter.class, endpoint.getHandler().getClass()); DirectFieldAccessor accessor = new DirectFieldAccessor(endpoint.getHandler()); assertEquals("DefaultSourceExtractor", accessor.getPropertyValue("sourceExtractor").getClass().getSimpleName()); @@ -50,8 +50,8 @@ public class WebServiceTargetAdapterParserTests { public void testSimpleWebServiceTargetAdapterWithCustomSourceExtractor() { ApplicationContext context = new ClassPathXmlApplicationContext( "simpleWebServiceTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = - (DefaultMessageEndpoint) context.getBean("adapterWithCustomSourceExtractor"); + HandlerEndpoint endpoint = + (HandlerEndpoint) context.getBean("adapterWithCustomSourceExtractor"); assertEquals(SimpleWebServiceTargetAdapter.class, endpoint.getHandler().getClass()); DirectFieldAccessor accessor = new DirectFieldAccessor(endpoint.getHandler()); SourceExtractor sourceExtractor = (SourceExtractor) context.getBean("sourceExtractor"); @@ -62,8 +62,8 @@ public class WebServiceTargetAdapterParserTests { public void testWebServiceTargetAdapterWithAllInOneMarshaller() { ApplicationContext context = new ClassPathXmlApplicationContext( "marshallingWebServiceTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = - (DefaultMessageEndpoint) context.getBean("adapterWithAllInOneMarshaller"); + HandlerEndpoint endpoint = + (HandlerEndpoint) context.getBean("adapterWithAllInOneMarshaller"); assertEquals(MarshallingWebServiceTargetAdapter.class, endpoint.getHandler().getClass()); DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(endpoint.getHandler()); DirectFieldAccessor templateAccessor = new DirectFieldAccessor( @@ -77,8 +77,8 @@ public class WebServiceTargetAdapterParserTests { public void testWebServiceTargetAdapterWithSeparateMarshallerAndUnmarshaller() { ApplicationContext context = new ClassPathXmlApplicationContext( "marshallingWebServiceTargetAdapterParserTests.xml", this.getClass()); - DefaultMessageEndpoint endpoint = - (DefaultMessageEndpoint) context.getBean("adapterWithSeparateMarshallerAndUnmarshaller"); + HandlerEndpoint endpoint = + (HandlerEndpoint) context.getBean("adapterWithSeparateMarshallerAndUnmarshaller"); assertEquals(MarshallingWebServiceTargetAdapter.class, endpoint.getHandler().getClass()); DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(endpoint.getHandler()); DirectFieldAccessor templateAccessor = new DirectFieldAccessor(