0

My RMQ listener:

@RabbitListener(
  bindings = @QueueBinding(
    value = @Queue,
    exchange = @Exchange(
      value = "${rabbitmq.exchange.sales-exchange.name}",
      type = "${rabbitmq.exchange.sales-exchange.type}",
      ignoreDeclarationExceptions = "true"
    ),
    key = "${rabbitmq.exchange.sales-exchange.sales-bk}"
  )
)
public void listenSalesOrderCreatedMessage(
  @Headers Map<String, Object> headers,
  @Payload SalesDTO payload
)
{
  log.info("Message Received!");
}

salesDTO:

package org.laptop.sale.event.subscriber.dto;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public class SalesOrderCreatedEventDTO implements Serializable {

  @JsonProperty("vendorDetails")
  @Valid
  private VendorDetails vendorDetails;

  @JsonInclude(JsonInclude.Include.NON_NULL)
  @Getter
  @Setter
  public static class VendorDetails implements Serializable {
    @JsonProperty("name")
    private String name;
    @JsonProperty("vendorCommission")
    private Double vendorCommission;
  }
}

My @Around Aspect to be executed before and after the message call:

package org.laptop.sale.event.subscriber;

@Component
@Aspect
@Slf4j
public class SubscriberAdvice {
  @Around("execution(* org.laptop.sale.event.subscriber..*(..)))")
  public void payloadValidationFailed(ProceedingJoinPoint joinPoint) {
    try {
      joinPoint.proceed();
    } catch (Throwable throwable) {
      log.error("Exception in the process execution {}", throwable);
    }
  }
}

Invalid Message payload:

{
  "vendorDetails": {
    "name": "Vendor Name",
    "vendorCommission": "wrongData"
  }
}

Valid Message payload:

{
  "vendorDetails": {
    "name": "Vendor Name",
    "vendorCommission": 8000.1
  }
}

Here the the program flow is entering the aspect only incase of Valid payload. It's not entering the Aspect incase of Invalid Payload. I tried with @AfterThrowing as well. that did not work either

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • How did you confirm if the call to the method to intercept is happening with invalid payload ? If the method call to valid payload can be intercepted , it will happen with invalid payload too as the point cut expression is for method execution. – R.G Mar 03 '20 at 07:48
  • 1
    I don't use Spring, RabbitMQ or JSON, but I noticed two things in your code: (1) Your listener has a `SalesDTO` payload while you show to us a class named `SalesOrderCreatedEventDTO`. This seems to be a mismatch. (2) The DTO class wants a `Double vendorCommission` but your invalid payload contains a `String` value. I imagine that deserialisation would fail already here, which is why the subsequent listener never gets executed in the first place. That would explain why the aspect cannot intercept it. If my guess is right, please let me know, then I can convert the comment into an answer. – kriegaex Mar 04 '20 at 01:10
  • Two people tried to help you, yet there is no feedback. Please be so polite as to reply so we all know if someone should write an answer which you can accept in order to close the question or if there is any follow-up problem. Thank you so much. – kriegaex Mar 21 '20 at 06:09

0 Answers0