Skip to content

Instantly share code, notes, and snippets.

@mmornati
Created December 8, 2019 21:23
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/f453d8093280e269bbb57b1c7d29b73e to your computer and use it in GitHub Desktop.
Save mmornati/f453d8093280e269bbb57b1c7d29b73e to your computer and use it in GitHub Desktop.
Produce Message
@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