Last active
December 7, 2019 22:45
-
-
Save mmornati/896f0218d4c365c6aa72fe544f6475f8 to your computer and use it in GitHub Desktop.
Send message in a Transactional Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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