upon validation failed like any message field is empty or null directly send to dead letter queue of Azure service bus at first instance (without its maximum Retry.) I tried SurviceBusSenderClient and ServiceBusSenderAsynch and send method but its retry for max time that set at azure bus. but i want it should go in first instance only when validations fails.
`This is my Config class to listen messages from service bus
@Configuration
@EnableConfigurationProperties({ServiceBusListenConnectionDetails.class})
@ConditionalOnExpression(
"${messaging.enabled:true}"
)
public class AzureServiceBusInboundMessagesListenerConfiguration {
@Bean
public ServiceBusInboundChannelAdapter inboundMessagesServiceBusInboundChannelAdapter(ServiceBusListenConnectionDetails listenConnectionDetails) {
ServiceBusInboundChannelAdapter adapter = serviceBusInboundChannelAdapter(listenConnectionDetails.getInboundMessages());
System.out.print(adapter);
return adapter;
}
@Override
@Bean(name = InputChannelConst.INBOUND_MESSAGES_QUEUE_CHANNEL)
protected MessageChannel inputChannel() {
return input();
}
}
In service class i get message and check for validation as below
@ServiceActivator(inputChannel = InputChannelConst.INBOUND_MESSAGES_QUEUE_CHANNEL) public void beforeSenttoSegment(byte[] payload, @Header(AzureHeaders.CHECKPOINTER) Checkpointer checkPointer)
throws IOException {
objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
InboundMessage<String> inboundMessageEventPayload = objectMapper.readValue(payload,
new TypeReference<InboundMessage<String>>() {
});
try {payloadvalidator.payloadValidator(property);
segmentService.sendInboundMessageEventToSegment(inboundMessageEventPayload, property);
checkPointer.success().doOnSuccess(s -> LOGGER.info("Inbound Message: '{}' successfully check-pointed", inboundMessageEventPayload))
.doOnError(e -> LOGGER.error("Error found when check-pointed Inbound Message", e)).block();
} catch (CustomValidationException e) {
LOGGER.error("Failed to process the Inbound message", e.getValidationErrors());
checkPointer.failure().block();
throw e;
}
this is my validator class
@Component
public class PayloadValidator {your text
private final Validator validator;
public <T> void payloadValidator(T payload) {
Set<ConstraintViolation<T>> violations = validator.validate(payload); if (!violations.isEmpty()) { Map<String, String> validationErrors = new HashMap<>(); for (ConstraintViolation<T> violation : violations) { validationErrors.put(violation.getPropertyPath().toString(), violation.getMessage()); } throw new CustomValidationException("Payload validation failed", validationErrors);
}
}
Anita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.