Skip to content

Allow @Externalized annotation to use SpEL expressions #881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Chitranshagarwal2000 opened this issue Oct 16, 2024 · 3 comments
Closed

Allow @Externalized annotation to use SpEL expressions #881

Chitranshagarwal2000 opened this issue Oct 16, 2024 · 3 comments
Assignees
Labels
in: event publication registry Event publication registry type: improvement Minor improvements
Milestone

Comments

@Chitranshagarwal2000
Copy link

Chitranshagarwal2000 commented Oct 16, 2024

Hello,
I have a Spring Boot Application. I am using spring Modulith and I want events to be externalized to AWS SQS. Everything works fine but the @Externalized annotation doesn't seem to be taking values from application.yml file. Below is the sample code.

@Externalized("${aws.order-completion-queue}")
public record OrderCompletedEvent(
        String prefix,
        String companyName,
        String firstName,
        String lastName,
        String email,
        String contactNumber,
        String orderId
) {
}

application.yml

aws:
 order-completion-queue: order-completion-dev-queue.fifo

FYI - I have verified the indentation and also that the queue exists. I am using the same placeholder on @SqsListener in my application and that works fine.
Also hardcoding the queue name works.

Please suggest if I am doing something wrong, if not what would be the best way to use the yml file properties in @Externalized annotation

Edit 1 - This is the error that I am getting

Caused by: java.util.concurrent.CompletionException: io.awspring.cloud.sqs.QueueAttributesResolvingException: Error resolving attributes for queue ${aws.order-completion-queue} with strategy CREATE and queueAttributesNames []
	at java.base/java.util.concurrent.CompletableFuture.encodeRelay(CompletableFuture.java:368) ~[na:na]
	at java.base/java.util.concurrent.CompletableFuture.completeRelay(CompletableFuture.java:377) ~[na:na]
	at java.base/java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:1152) ~[na:na]
	... 42 common frames omitted
Caused by: io.awspring.cloud.sqs.QueueAttributesResolvingException: Error resolving attributes for queue ${aws.order-completion-queue} with strategy CREATE and queueAttributesNames []
	at io.awspring.cloud.sqs.QueueAttributesResolver.wrapException(QueueAttributesResolver.java:90) ~[spring-cloud-aws-sqs-3.2.0.jar:3.2.0]
	at java.base/java.util.concurrent.CompletableFuture.uniExceptionally(CompletableFuture.java:990) ~[na:na]
	at java.base/java.util.concurrent.CompletableFuture$UniExceptionally.tryFire(CompletableFuture.java:974) ~[na:na]
	... 42 common frames omitted
Caused by: software.amazon.awssdk.services.sqs.model.SqsException: Can only include alphanumeric characters, hyphens, or underscores. 1 to 80 in length (Service: Sqs, Status Code: 400, Request ID: eb9ee289-d753-4a3c-84b6-22cd769ffc29)
	at software.amazon.awssdk.services.sqs.model.SqsException$BuilderImpl.build(SqsException.java:104) ~[sqs-2.25.70.jar:na]
	at software.amazon.awssdk.services.sqs.model.SqsException$BuilderImpl.build(SqsException.java:58) ~[sqs-2.25.70.jar:na]

I am using Spring 3.3.4 and below is a snippet of my pom consisting of modulith specific dependencies

<dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-starter-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-actuator</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-observability</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-events-aws-sqs</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-starter-jpa</artifactId>
        </dependency>
@odrotbohm
Copy link
Member

The routing target declared in an annotation is currently not processed at all, but used as is. You can currently already work around this limitation by providing a programmatic routing lookup by declaring a bean of type EventExternalizationConfiguration using the route(Class<T>, Function<T, RoutingTarget>) method.

That said, we could pipe the routing target through the SpEL processing we already apply for the routing key. You'd have to use a SpEL expression such as #{env['aws.order-completion-queue']}. Would that be okay for you?

@odrotbohm odrotbohm self-assigned this Oct 18, 2024
@odrotbohm odrotbohm added in: event publication registry Event publication registry meta: waiting for feedback Waiting for feedback of the original reporter labels Oct 18, 2024
@Chitranshagarwal2000
Copy link
Author

I was able to do that with EventExternalizationConfiguration. While I was struggling with this I did try SpEL #{environment['aws.order-completion-queue']}. I noticed I have to use env instead of environment, but yeah this will work for me. Will try and let you know

By the way love the work. Thanks for a great project 👍🏻

odrotbohm added a commit that referenced this issue Oct 18, 2024
We now support SpEL expressions in routing targets for events to be externalized. Introduced a BrokerRouting.getTarget(Object) overload to allow access to the event object in the SpEL expression. To support those, event externalizers will have to call that method where they previously called ….getTarget().
@odrotbohm odrotbohm added type: improvement Minor improvements and removed meta: waiting for feedback Waiting for feedback of the original reporter labels Oct 18, 2024
@odrotbohm odrotbohm added this to the 1.3 RC1 milestone Oct 18, 2024
@odrotbohm
Copy link
Member

This in place now. The latest snapshots should support SpEL expressions in the target definitions as well.

@odrotbohm odrotbohm changed the title @Externalized annotation to take values from application.yml file Allow @Externalized annotation to use SpEL expressions Oct 26, 2024
odrotbohm added a commit to odrotbohm/spring-cloud-aws that referenced this issue Jan 9, 2025
To benefit from the Support for SpEL expressions using the event when calculating the routing target. See spring-projects/spring-modulith#881 for details.
maciejwalkowiak added a commit to awspring/spring-cloud-aws that referenced this issue Jan 17, 2025
* Upgrade to Spring Modulith 1.3.1.
* Add Spring Modulith 1.3's header support for externalization.
* Pass event into externalization target calculation.

To benefit from the Support for SpEL expressions using the event when calculating the routing target. See spring-projects/spring-modulith#881 for details.
---------

Co-authored-by: Maciej Walkowiak <[email protected]>
kcsurapaneni pushed a commit to kcsurapaneni/spring-cloud-aws that referenced this issue Jun 13, 2025
* Upgrade to Spring Modulith 1.3.1.
* Add Spring Modulith 1.3's header support for externalization.
* Pass event into externalization target calculation.

To benefit from the Support for SpEL expressions using the event when calculating the routing target. See spring-projects/spring-modulith#881 for details.
---------

Co-authored-by: Maciej Walkowiak <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: event publication registry Event publication registry type: improvement Minor improvements
Projects
None yet
Development

No branches or pull requests

2 participants