Skip to content
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

Set replyPostProcessor default value from the message container factory #1425

Closed
anassder opened this issue Feb 25, 2022 · 4 comments · Fixed by #1526
Closed

Set replyPostProcessor default value from the message container factory #1425

anassder opened this issue Feb 25, 2022 · 4 comments · Fixed by #1526
Assignees
Labels
ideal-for-user-contribution An issue that would ideal for a user to get started with contributing. type: enhancement
Milestone

Comments

@anassder
Copy link

Expected Behavior
Use default value (bean name or class) for @RabbitListener annotation's attribute replyPostProcessor using message container factory like we do for AfterReceivePostProcessors and BeforeSendReplyPostProcessors.

Current Behavior
We can set replyPostProcessor value only from @RabbitListener annotation.
Example :

@RabbitListener(queues = "test.header", group = "testGroup", replyPostProcessor = "echoCustomHeader")
public String capitalizeWithHeader(String in) {
    return in.toUpperCase();
}

@Bean
public ReplyPostProcessor echoCustomHeader() {
    return (req, resp) -> {
        resp.getMessageProperties().setHeader("myHeader", req.getMessageProperties().getHeader("myHeader"));
        return resp;
    };
}

Context
Currently we need to add a custom header (that we get from inbound request) to our responses, and we can't do that using AfterReceivePostProcessors or BeforeSendReplyPostProcessors because MessagePostProcessor.postProcessMessage give only the current message as parameter.
The best solution for this probleme is to use replyPostProcessor, but we can't change all @RabbitListener annotations to add this attribute.
That's why I suggest to add new method to the container factory to make changing default replyPostProcessor value possible. (like for setAfterReceivePostProcessors and setBeforeSendReplyPostProcessors)

@anassder anassder changed the title Adding Set replyPostProcessor default value from the message container factory Feb 25, 2022
@artembilan
Copy link
Member

Agreed.
This is really a valid requirement.
We have a logic like this already:

JavaUtils.INSTANCE // NOSONAR
					.acceptIfNotNull(this.beforeSendReplyPostProcessors,
							messageListener::setBeforeSendReplyPostProcessors)

where the mentioned replyPostProcessor is really a property of the same messageListener.
So, that BaseRabbitListenerContainerFactory can really have such a new replyPostProcessor option exposed.

Feel free to come back to us with a contribution: https://github.com/spring-projects/spring-amqp/blob/main/CONTRIBUTING.adoc

Thank you!

@garyrussell
Copy link
Contributor

It's actually this code that sets the RPP...

JavaUtils.INSTANCE
.acceptIfNotNull(endpoint.getReplyPostProcessor(), messageListener::setReplyPostProcessor)

Not the setBeforeSendReplyPostProcessors.

In the meantime, you can use this...

@Bean
public ReplyPostProcessor echoCustomHeader(AbstractRabbitListenerContainerFactory<?> factory) {
	ReplyPostProcessor rpp = (req, resp) -> {
		resp.getMessageProperties().setHeader("myHeader", req.getMessageProperties().getHeader("myHeader"));
		return resp;
	};
	factory.setContainerCustomizer(container -> {
		MessagingMessageListenerAdapter listener = ((MessagingMessageListenerAdapter) container
				.getMessageListener());
		listener.setReplyPostProcessor(rpp);
	});
	return rpp;
}

@garyrussell garyrussell added this to the Backlog milestone Mar 1, 2022
@garyrussell garyrussell added the ideal-for-user-contribution An issue that would ideal for a user to get started with contributing. label Mar 1, 2022
@artembilan
Copy link
Member

Yes, but it also could be done by the BaseRabbitListenerContainerFactory as a common place for such a property.
The @RabbitListener one can override, of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ideal-for-user-contribution An issue that would ideal for a user to get started with contributing. type: enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants