Reputation: 461
In my app, I need to customized the call of the save method over a POST Restful service to post an event to a RabbitMQ queue.
Each time a consumer of my API is firing a POST on my resource, I want to publish an event on my RabbitMQ queue to make some asynchronous processing.
Right now, I use @RepositoryRestResource and Spring-Data-Jpa to expose a CRUD API over my Spring-Data JPA Repository. It does the job, very straightforward and simple. I'd like to stick with that so in the case of a POST (save method) I'd like to compose or change the behaviour. I need to store the data in my database but also to publish an event in the RabbitMQ queue.
I tried several solution but I failed.
May be you have the solution . How to I extend a particular method in my Rest CRUD repository ?
Upvotes: 1
Views: 1973
Reputation: 7941
You can use a custom org.springframework.context.ApplicationListener
. Spring Data REST offers the convenient base class AbstractRepositoryEventListener
.
@Component
public class PublishToRabbitMQAfterSavingYourEntity extends AbstractRepositoryEventListener<YourEntity> {
@Override
public void onAfterSave(YourEntity entity) {
// publish to RabbitMQ
}
}
Upvotes: 2
Reputation: 317
One way that I have solved this kind of problem in the past is to use Aspect Oriented Programming, and luckily since you are using the Spring Framework it is quite easy and well documented. You could put "Around" advice around the constructor for the domain objects (just a suggestion) and have it send a message to the RabbitMQ Exchange.
Another way of doing this is to use the Log4j AMQP appender and log the object prior to saving it.
Upvotes: 2