Removed AbstractPoller and ChannelPoller. Refactored test cases to use PollingConsumerEndpoint.

This commit is contained in:
Mark Fisher
2008-10-20 15:27:03 +00:00
parent f8b2724f7a
commit 1dc2c175a2
6 changed files with 458 additions and 468 deletions

View File

@@ -1,157 +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.endpoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.scheduling.Trigger;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;
/**
* Base class for pollers.
*
* @author Mark Fisher
*/
public abstract class AbstractPoller implements Runnable, InitializingBean {
public static final int MAX_MESSAGES_UNBOUNDED = -1;
private final Trigger trigger;
private volatile long maxMessagesPerPoll = MAX_MESSAGES_UNBOUNDED;
private volatile TaskExecutor taskExecutor;
private volatile PlatformTransactionManager transactionManager;
private volatile TransactionDefinition transactionDefinition;
private volatile TransactionTemplate transactionTemplate;
private volatile boolean initialized;
private final Object initializationMonitor = new Object();
public AbstractPoller(Trigger trigger) {
Assert.notNull(trigger, "trigger must not be null");
this.trigger = trigger;
}
public Trigger getTrigger() {
return this.trigger;
}
/**
* Set the maximum number of messages to receive for each poll.
* A non-positive value indicates that polling should repeat as long
* as non-null messages are being received and successfully sent.
*
* <p>The default is unbounded.
*
* @see #MAX_MESSAGES_UNBOUNDED
*/
public void setMaxMessagesPerPoll(int maxMessagesPerPoll) {
this.maxMessagesPerPoll = maxMessagesPerPoll;
}
public void setTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
/**
* Specify a transaction manager to use for all exchange operations.
* If none is provided, then the operations will occur without any
* transactional behavior (i.e. there is no default transaction manager).
*/
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void setTransactionDefinition(TransactionDefinition transactionDefinition) {
this.transactionDefinition = transactionDefinition;
}
private TransactionTemplate getTransactionTemplate() {
if (!this.initialized) {
this.afterPropertiesSet();
}
return this.transactionTemplate;
}
public void afterPropertiesSet() {
synchronized (this.initializationMonitor) {
if (this.initialized) {
return;
}
if (this.transactionManager != null) {
if (this.transactionDefinition == null) {
this.transactionDefinition = new DefaultTransactionDefinition();
}
this.transactionTemplate = new TransactionTemplate(
this.transactionManager, this.transactionDefinition);
}
this.initialized = true;
}
}
public void run() {
if (this.taskExecutor != null) {
this.taskExecutor.execute(new Runnable() {
public void run() {
poll();
}
});
}
else {
poll();
}
}
private void poll() {
int count = 0;
while (this.maxMessagesPerPoll < 0 || count < this.maxMessagesPerPoll) {
if (!this.innerPoll()) {
break;
}
count++;
}
}
private boolean innerPoll() {
TransactionTemplate txTemplate = this.getTransactionTemplate();
if (txTemplate != null) {
return (Boolean) txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return doPoll();
}
});
}
return doPoll();
}
protected abstract boolean doPoll();
}

View File

@@ -1,69 +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.endpoint;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.MessageConsumer;
import org.springframework.integration.scheduling.Trigger;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
*/
public class ChannelPoller extends AbstractPoller {
private final PollableChannel channel;
private volatile MessageConsumer consumer;
private volatile long receiveTimeout = 1000;
public ChannelPoller(PollableChannel channel, Trigger trigger) {
super(trigger);
Assert.notNull(channel, "channel must not be null");
this.channel = channel;
}
/**
* Specify the timeout to use when receiving from the channel (in milliseconds).
* A negative value indicates that receive calls should block indefinitely.
* The default value is 1000 (1 second).
*/
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
public void setConsumer(MessageConsumer consumer) {
this.consumer = consumer;
}
@Override
protected boolean doPoll() {
Assert.notNull(this.consumer, "consumer must not be null");
Message<?> message = (this.receiveTimeout >= 0)
? this.channel.receive(this.receiveTimeout)
: this.channel.receive();
if (message == null) {
return false;
}
this.consumer.onMessage(message);
return true;
}
}