Skip to content

Commit

Permalink
Optimize MessageTriggerAction for Java DSL (spring-projects#8595)
Browse files Browse the repository at this point in the history
Currently, the `MessageTriggerAction.trigger` is configured
on the `BaseIntegrationFlowDefinition` to be called via reflection
in the `MessagingMethodInvokerHelper`

* Represent a `MessageTriggerAction.trigger` as a `Consumer<Message<?>>` method reference
and use a `LambdaMessageProcessor` for direct call
* Add a `Consumer` support for `LambdaMessageProcessor`
  • Loading branch information
artembilan authored Apr 13, 2023
1 parent b997295 commit aaaa489
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ protected MessageChannel getCurrentMessageChannel() {
*/
protected InterceptableChannel currentInterceptableChannel() {
MessageChannel currentChannel = getCurrentMessageChannel();
if (currentChannel instanceof InterceptableChannel) {
return (InterceptableChannel) currentChannel;
if (currentChannel instanceof InterceptableChannel interceptableChannel) {
return interceptableChannel;
}
else {
DirectChannel newCurrentChannel = new DirectChannel();
Expand Down Expand Up @@ -1161,7 +1161,7 @@ public B bridge() {
* @see GenericEndpointSpec
*/
public B bridge(Consumer<GenericEndpointSpec<BridgeHandler>> endpointConfigurer) {
return register(new GenericEndpointSpec<>(new BridgeHandler()), endpointConfigurer);
return handle(new BridgeHandler(), endpointConfigurer);
}

/**
Expand Down Expand Up @@ -2836,7 +2836,9 @@ public B trigger(MessageTriggerAction triggerAction) {
public B trigger(MessageTriggerAction triggerAction,
Consumer<GenericEndpointSpec<ServiceActivatingHandler>> endpointConfigurer) {

return handle(new ServiceActivatingHandler(triggerAction, "trigger"), endpointConfigurer);
Consumer<Message<?>> trigger = triggerAction::trigger;
return handle(new ServiceActivatingHandler(new LambdaMessageProcessor(trigger, Message.class)),
endpointConfigurer);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 the original author or authors.
* Copyright 2016-2023 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.
Expand All @@ -21,6 +21,7 @@
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -98,7 +99,8 @@ public LambdaMessageProcessor(Object target, @Nullable Class<?> expectedType) {
}

private static boolean isExplicit(Object target) {
return target instanceof Function<?, ?> ||
return target instanceof Consumer<?> ||
target instanceof Function<?, ?> ||
target instanceof GenericHandler<?> ||
target instanceof GenericSelector<?> ||
target instanceof GenericTransformer<?, ?>;
Expand Down Expand Up @@ -189,7 +191,11 @@ else if (Map.class.isAssignableFrom(parameterType)) {

@SuppressWarnings({"unchecked", "rawtypes"})
private Object invokeMethod(Object[] args) throws InvocationTargetException, IllegalAccessException {
if (this.target instanceof Function function) {
if (this.target instanceof Consumer consumer) {
consumer.accept(args[0]);
return null;
}
else if (this.target instanceof Function function) {
return function.apply(args[0]);
}
else if (this.target instanceof GenericSelector selector) {
Expand Down

0 comments on commit aaaa489

Please sign in to comment.