From 4a36bf08cb1e89f361eecd05cc194bf2f96b6125 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 3 Dec 2007 00:43:35 +0000 Subject: [PATCH] Added message source adapters for polling sources. --- .../AbstractPollingMessageSource.java | 66 ++++++++++++++++++ .../AbstractPrefetchingMessageSource.java | 68 +++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 spring-eai-core/src/main/java/org/springframework/integration/endpoint/AbstractPollingMessageSource.java create mode 100644 spring-eai-core/src/main/java/org/springframework/integration/endpoint/AbstractPrefetchingMessageSource.java diff --git a/spring-eai-core/src/main/java/org/springframework/integration/endpoint/AbstractPollingMessageSource.java b/spring-eai-core/src/main/java/org/springframework/integration/endpoint/AbstractPollingMessageSource.java new file mode 100644 index 0000000000..66c19ada67 --- /dev/null +++ b/spring-eai-core/src/main/java/org/springframework/integration/endpoint/AbstractPollingMessageSource.java @@ -0,0 +1,66 @@ +/* + * 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.endpoint; + +import org.springframework.integration.MessageSource; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageMapper; +import org.springframework.integration.message.SimplePayloadMessageMapper; +import org.springframework.util.Assert; + +/** + * A {@link MessageSource} adapter for any source that can be polled for + * objects. + * + * @author Mark Fisher + */ +public abstract class AbstractPollingMessageSource implements MessageSource { + + private MessageMapper mapper = new SimplePayloadMessageMapper(); + + + public void setMapper(MessageMapper mapper) { + Assert.notNull(mapper, "mapper must not be null"); + this.mapper = mapper; + } + + public Message receive() { + return this.receive(-1); + } + + public Message receive(long timeout) { + long start = System.currentTimeMillis(); + while (timeout <= 0 || System.currentTimeMillis() - start < timeout) { + Object o = this.pollForObject(); + if (o != null) { + return this.mapper.toMessage(o); + } + if (timeout == 0) { + return null; + } + } + return null; + } + + + /** + * Method for subclasses to implement. Returns an object to be mapped to a + * {@link Message} by the message mapper. + */ + protected abstract Object pollForObject(); + +} diff --git a/spring-eai-core/src/main/java/org/springframework/integration/endpoint/AbstractPrefetchingMessageSource.java b/spring-eai-core/src/main/java/org/springframework/integration/endpoint/AbstractPrefetchingMessageSource.java new file mode 100644 index 0000000000..f8b157358b --- /dev/null +++ b/spring-eai-core/src/main/java/org/springframework/integration/endpoint/AbstractPrefetchingMessageSource.java @@ -0,0 +1,68 @@ +/* + * 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.endpoint; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +import org.springframework.integration.MessageSource; +import org.springframework.integration.message.Message; + +/** + * A {@link MessageSource} adapter for any source that can be polled for + * objects. This version allows for pre-fetching multiple results so that + * subsequent calls to {@link #pollForObject()} may be more efficient. + * + * @author Mark Fisher + */ +public abstract class AbstractPrefetchingMessageSource extends AbstractPollingMessageSource { + + private BlockingQueue queue = new LinkedBlockingQueue(); + + + public Object pollForObject() { + Object o = queue.poll(); + if (o == null) { + this.prefetch(); + o = queue.poll(); + } + return o; + } + + private void prefetch() { + Object[] results = this.pollForObjects(); + if (results != null) { + try { + for (Object o : results) { + queue.put(o); + } + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + } + } + + + /** + * Method for subclasses to implement. Returns objects to be mapped to + * {@link Message Messages} by the message mapper. + */ + protected abstract Object[] pollForObjects(); + +}