Created
December 8, 2019 21:23
-
-
Save mmornati/f453d8093280e269bbb57b1c7d29b73e to your computer and use it in GitHub Desktop.
Produce Message
This file contains 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
@Component | |
@RequiredArgsConstructor | |
public class CreateOperationRabbitMessage { | |
private static final LoggerService LOGGER = LoggerServiceFactory.getLoggerService(CreateOperationRabbitMessage.class); | |
private final RabbitTemplate rabbitTemplate; | |
private static final ObjectMapper MAPPER = new ObjectMapper(); | |
@Override | |
public boolean sendMessage(OperationMessage operationMessage) { | |
try { | |
String orderJson = MAPPER.writeValueAsString(operationMessage); | |
Message message = MessageBuilder | |
.withBody(orderJson.getBytes()) | |
.setContentType(MessageProperties.CONTENT_TYPE_JSON) | |
.build(); | |
rabbitTemplate | |
.convertAndSend(Exchanges.OPERATIONS_EXCHANGE, | |
Exchanges.OPERATION_ROUTING_KEY, | |
message); | |
return true; | |
} catch (AmqpException e) { | |
LOGGER.warn(() -> "Error sending message to RabbitMQ"); | |
LOGGER.debug(() -> "Error sending message to RabbitMQ", e); | |
} catch (JsonProcessingException e) { | |
LOGGER.warn(() -> "Error converting message to JSON"); | |
LOGGER.debug(() -> "Error sending message to RabbitMQ", e); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment