Skip to content

Instantly share code, notes, and snippets.

@mmornati
Last active December 7, 2019 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmornati/896f0218d4c365c6aa72fe544f6475f8 to your computer and use it in GitHub Desktop.
Save mmornati/896f0218d4c365c6aa72fe544f6475f8 to your computer and use it in GitHub Desktop.
Send message in a Transactional Function
@Transactional
public void myTransactionalMethod(Operation operation) {
service.storeOperation(operation);
sendMessage(operation.getId());
}
private boolean sendMessage(Long operationId) {
try {
String operationJson = objectMapper.writeValueAsString(operation);
Message message = MessageBuilder
.withBody(operationJson.getBytes())
.setContentType(MessageProperties.CONTENT_TYPE_JSON)
.build();
rabbitTemplate
.convertAndSend(Exchanges.MY_EXCHANGE,
Exchanges.MY_ROUTING_KEY,
message);
return true;
} catch (AmqpException e) {
LOGGER.error(() -> e.getMessage());
LOGGER.debug(() -> "Error sending message to RabbitMQ", e);
} catch (JsonProcessingException e) {
LOGGER.error(() -> e.getMessage());
LOGGER.debug(() -> "Error converting message to JSON", e);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment