use of org.springframework.integration.core.MessageSelector in project spring-integration by spring-projects.
the class RecipientListRouter method removeRecipient.
@Override
@ManagedOperation
public int removeRecipient(String channelName, String selectorExpression) {
int counter = 0;
MessageChannel targetChannel = getChannelResolver().resolveDestination(channelName);
for (Iterator<Recipient> it = this.recipients.iterator(); it.hasNext(); ) {
Recipient next = it.next();
MessageSelector selector = next.getSelector();
MessageChannel channel = next.getChannel();
if (selector instanceof ExpressionEvaluatingSelector && channel == targetChannel && ((ExpressionEvaluatingSelector) selector).getExpressionString().equals(selectorExpression)) {
it.remove();
counter++;
}
}
return counter;
}
use of org.springframework.integration.core.MessageSelector in project spring-integration by spring-projects.
the class MessageSelectingInterceptorTests method testMultipleSelectorsAccept.
@Test
public void testMultipleSelectorsAccept() {
final AtomicInteger counter = new AtomicInteger();
MessageSelector selector1 = new TestMessageSelector(true, counter);
MessageSelector selector2 = new TestMessageSelector(true, counter);
MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2);
QueueChannel channel = new QueueChannel();
channel.addInterceptor(interceptor);
assertTrue(channel.send(new GenericMessage<String>("test1")));
assertEquals(2, counter.get());
}
use of org.springframework.integration.core.MessageSelector in project spring-integration by spring-projects.
the class MessageSelectingInterceptorTests method testMultipleSelectorsReject.
@Test
public void testMultipleSelectorsReject() {
boolean exceptionThrown = false;
final AtomicInteger counter = new AtomicInteger();
MessageSelector selector1 = new TestMessageSelector(true, counter);
MessageSelector selector2 = new TestMessageSelector(false, counter);
MessageSelector selector3 = new TestMessageSelector(false, counter);
MessageSelector selector4 = new TestMessageSelector(true, counter);
MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2, selector3, selector4);
QueueChannel channel = new QueueChannel();
channel.addInterceptor(interceptor);
try {
channel.send(new GenericMessage<String>("test1"));
} catch (MessageDeliveryException e) {
exceptionThrown = true;
}
assertTrue(exceptionThrown);
assertEquals(2, counter.get());
}
use of org.springframework.integration.core.MessageSelector in project spring-integration by spring-projects.
the class FilterAnnotationPostProcessor method createHandler.
@Override
protected MessageHandler createHandler(Object bean, Method method, List<Annotation> annotations) {
MessageSelector selector;
if (AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
Object target = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
if (target instanceof MessageSelector) {
selector = (MessageSelector) target;
} else if (this.extractTypeIfPossible(target, MessageFilter.class) != null) {
checkMessageHandlerAttributes(resolveTargetBeanName(method), annotations);
return (MessageHandler) target;
} else {
selector = new MethodInvokingSelector(target);
}
} else {
Assert.isTrue(boolean.class.equals(method.getReturnType()) || Boolean.class.equals(method.getReturnType()), "The Filter annotation may only be applied to methods with a boolean return type.");
selector = new MethodInvokingSelector(bean, method);
}
MessageFilter filter = new MessageFilter(selector);
String discardWithinAdvice = MessagingAnnotationUtils.resolveAttribute(annotations, "discardWithinAdvice", String.class);
if (StringUtils.hasText(discardWithinAdvice)) {
discardWithinAdvice = this.beanFactory.resolveEmbeddedValue(discardWithinAdvice);
if (StringUtils.hasText(discardWithinAdvice)) {
filter.setDiscardWithinAdvice(Boolean.parseBoolean(discardWithinAdvice));
}
}
String throwExceptionOnRejection = MessagingAnnotationUtils.resolveAttribute(annotations, "throwExceptionOnRejection", String.class);
if (StringUtils.hasText(throwExceptionOnRejection)) {
String throwExceptionOnRejectionValue = this.beanFactory.resolveEmbeddedValue(throwExceptionOnRejection);
if (StringUtils.hasText(throwExceptionOnRejectionValue)) {
filter.setThrowExceptionOnRejection(Boolean.parseBoolean(throwExceptionOnRejectionValue));
}
}
String discardChannelName = MessagingAnnotationUtils.resolveAttribute(annotations, "discardChannel", String.class);
if (StringUtils.hasText(discardChannelName)) {
filter.setDiscardChannelName(discardChannelName);
}
this.setOutputChannelIfPresent(annotations, filter);
return filter;
}
use of org.springframework.integration.core.MessageSelector in project spring-integration by spring-projects.
the class IntegrationFlowDefinition method filter.
/**
* Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
* for the provided {@link GenericSelector}.
* In addition accept options for the integration endpoint using {@link FilterEndpointSpec}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .filter(Date.class, p -> p.after(new Date()), e -> e.autoStartup(false))
* }
* </pre>
* @param payloadType the {@link Class} for desired {@code payload} type.
* @param genericSelector the {@link GenericSelector} to use.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @param <P> the source payload type.
* @return the current {@link IntegrationFlowDefinition}.
* @see LambdaMessageProcessor
* @see FilterEndpointSpec
*/
public <P> B filter(Class<P> payloadType, GenericSelector<P> genericSelector, Consumer<FilterEndpointSpec> endpointConfigurer) {
Assert.notNull(genericSelector, "'genericSelector' must not be null");
MessageSelector selector = genericSelector instanceof MessageSelector ? (MessageSelector) genericSelector : (isLambda(genericSelector) ? new MethodInvokingSelector(new LambdaMessageProcessor(genericSelector, payloadType)) : new MethodInvokingSelector(genericSelector));
return this.register(new FilterEndpointSpec(new MessageFilter(selector)), endpointConfigurer);
}
Aggregations