From 180d57c418bd65ea17a9d021208d45cfe067eaee Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Sun, 5 Oct 2008 21:26:01 +0000 Subject: [PATCH] Added PollingConsumerEndpoint. --- .../endpoint/AbstractPollingEndpoint.java | 3 + .../endpoint/PollingConsumerEndpoint.java | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 org.springframework.integration/src/main/java/org/springframework/integration/endpoint/PollingConsumerEndpoint.java diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/AbstractPollingEndpoint.java b/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/AbstractPollingEndpoint.java index 98c88e52cf..bb9cb711fd 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/AbstractPollingEndpoint.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/AbstractPollingEndpoint.java @@ -137,6 +137,9 @@ public abstract class AbstractPollingEndpoint implements MessageEndpoint, TaskSc public void start() { synchronized (this.lifecycleMonitor) { + if (!this.initialized) { + this.afterPropertiesSet(); + } Assert.state(this.taskScheduler != null, "unable to start polling, no taskScheduler available"); this.runningTask = this.taskScheduler.schedule(new Poller(), this.trigger); diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/PollingConsumerEndpoint.java b/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/PollingConsumerEndpoint.java new file mode 100644 index 0000000000..7c1e17a689 --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/endpoint/PollingConsumerEndpoint.java @@ -0,0 +1,60 @@ +/* + * 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.PollableChannel; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageConsumer; +import org.springframework.util.Assert; + +/** + * @author Mark Fisher + */ +public class PollingConsumerEndpoint extends AbstractPollingEndpoint { + + private final MessageConsumer consumer; + + private final PollableChannel inputChannel; + + private volatile long receiveTimeout = 1000; + + + public PollingConsumerEndpoint(MessageConsumer consumer, PollableChannel inputChannel) { + Assert.notNull(consumer, "consumer must not be null"); + Assert.notNull(inputChannel, "inputChannel must not be null"); + this.consumer = consumer; + this.inputChannel = inputChannel; + } + + + public void setReceiveTimeout(long receiveTimeout) { + this.receiveTimeout = receiveTimeout; + } + + @Override + protected boolean doPoll() { + Message message = (this.receiveTimeout >= 0) + ? this.inputChannel.receive(this.receiveTimeout) + : this.inputChannel.receive(); + if (message == null) { + return false; + } + this.consumer.onMessage(message); + return true; + } + +}