INT-2082 added support for evaluating payload expression in ContinuousQueryMessageProducer

This commit is contained in:
David Turanski
2011-08-25 13:06:00 -04:00
parent a97ddfa71e
commit 2cade4220e
6 changed files with 108 additions and 51 deletions

View File

@@ -22,10 +22,6 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.util.Assert;
@@ -47,7 +43,7 @@ import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
* @since 2.1
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class CacheListeningMessageProducer extends MessageProducerSupport {
public class CacheListeningMessageProducer extends SpelMessageProducerSupport {
private final Log logger = LogFactory.getLog(this.getClass());
@@ -58,9 +54,6 @@ public class CacheListeningMessageProducer extends MessageProducerSupport {
private volatile Set<EventType> supportedEventTypes =
new HashSet<EventType>(Arrays.asList(EventType.CREATED, EventType.UPDATED));
private volatile Expression payloadExpression;
private final SpelExpressionParser parser = new SpelExpressionParser();
public CacheListeningMessageProducer(Region<?, ?> region) {
Assert.notNull(region, "region must not be null");
@@ -74,14 +67,6 @@ public class CacheListeningMessageProducer extends MessageProducerSupport {
this.supportedEventTypes = new HashSet<EventType>(Arrays.asList(eventTypes));
}
public void setPayloadExpression(String payloadExpression) {
if (payloadExpression == null) {
this.payloadExpression = null;
}
else {
this.payloadExpression = this.parser.parseExpression(payloadExpression);
}
}
@Override
protected void doStart() {
@@ -105,8 +90,7 @@ public class CacheListeningMessageProducer extends MessageProducerSupport {
}
}
private class MessageProducingCacheListener extends CacheListenerAdapter {
@Override
@@ -137,14 +121,9 @@ public class CacheListeningMessageProducer extends MessageProducerSupport {
}
}
private void processEvent(EntryEvent event) {
if (payloadExpression != null) {
Object evaluationResult = payloadExpression.getValue(event);
this.publish(evaluationResult);
}
else {
this.publish(event);
}
private void processEvent(EntryEvent event) {
this.publish(evaluationResult(event));
}
private void publish(Object payload) {

View File

@@ -16,15 +16,15 @@
package org.springframework.integration.gemfire.inbound;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.gemfire.listener.CqQueryDefinition;
import org.springframework.data.gemfire.listener.QueryListener;
import org.springframework.data.gemfire.listener.QueryListenerContainer;
import org.springframework.integration.Message;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.util.Assert;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.gemstone.gemfire.cache.query.CqEvent;
/**
@@ -38,7 +38,7 @@ import com.gemstone.gemfire.cache.query.CqEvent;
* @since 2.1
*
*/
public class ContinuousQueryMessageProducer extends MessageProducerSupport implements QueryListener {
public class ContinuousQueryMessageProducer extends SpelMessageProducerSupport implements QueryListener {
private static Log logger = LogFactory.getLog(ContinuousQueryMessageProducer.class);
private final String query;
@@ -95,7 +95,7 @@ public class ContinuousQueryMessageProducer extends MessageProducerSupport imple
if (logger.isDebugEnabled()){
logger.debug(String.format("processing cq event key [%s] event [%s]",event.getBaseOperation().toString(),event.getKey()));
}
Message<CqEvent> cqEventMessage = MessageBuilder.withPayload(event).build();
Message<?> cqEventMessage = MessageBuilder.withPayload(evaluationResult(event)).build();
sendMessage(cqEventMessage);
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2011 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.gemfire.inbound;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.endpoint.MessageProducerSupport;
/**
* @author David Turanski
* @since 2.1
*
*/
public class SpelMessageProducerSupport extends MessageProducerSupport {
private volatile Expression payloadExpression;
private final SpelExpressionParser parser = new SpelExpressionParser();
@Override
protected void onInit(){
super.onInit();
}
public void setPayloadExpression(String payloadExpression) {
if (payloadExpression == null) {
this.payloadExpression = null;
}
else {
this.payloadExpression = this.parser.parseExpression(payloadExpression);
}
}
protected Object evaluationResult(Object payload){
Object evaluationResult = payload;
if (payloadExpression != null) {
evaluationResult = payloadExpression.getValue(payload);
}
return evaluationResult;
}
}