我打算编写一个方法,每当一条消息与RabbitMQ排队时就会被调用。该方法是一个用@RabbitListener
注释的void方法,并在内部调用class的(void)方法,该方法是侦听器类的属性。到目前为止,效果很好。
在这种称为方法的方法中,我想与另一个微服务进行通信,该微服务应该向我提供一些我需要的信息。
实际上,无论何时从RabbitMQ收到消息,都必须对CRM进行一些更改。
我该如何测试/模拟呢?我该如何模拟对另一个微服务的调用(因为我还不知道我将获得的信息的结构如何)?
public class X {
private static final Logger LOGGER = LoggerFactory.getLogger(X.class);
private final CrmService crmService;
public void changeStatus(Map<?,?> payload,MyStatus status) {
final ObjectMapper mapper = new ObjectMapper();
final JsonNode entities = mapper.valueToTree(payload.get("entities"));
final JsonNode contract = entities.get("contract");
final Long customerId = contract.get("customerId").asLong();
final String email = contract.get("email").asText();
final CrmApiModel crmApiModel = new CrmApiModel();
CrmApiModelProperty apiProp = new CrmApiModelProperty("status",status);
crmApiModel.addProperty(apiProp);
ResponseEntity<?> result = null;
try {
LOGGER.info(String.format("Updating status in CRM for customer %s...",customerId));
result = crmService.updateContactProperty(customerId,crmApiModel);
LOGGER.info(String.format("Customer %s updated in CRM with status %s",customerId,status));
} catch (HttpClientErrorException hceEx) {
if (hceEx.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
LOGGER.info(String.format("Status of contact with ID %s updated to %s",status));
}
if (hceEx.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
// Customer is not existent in CRM --> create the contact...
LOGGER.warn(String.format("<<<<< Customer with ID %s not found in CRM. It gets created...>>>>>",customerId));
final Long userId = contract.get("userId").asLong();
final JsonNode member;
final JsonNode user;
try {
member = contactServiceConnector.getMemberById(customerId); <<< call to another microservice
user = contactServiceConnector.getUserById(userId); <<< call to another microservice
crmApiModel.addProperty(new CrmApiModelProperty("firstname",user.get("person/firstName")));
crmApiModel.addProperty(new CrmApiModelProperty("lastname",user.get("person/lastName")));
crmApiModel.addProperty(new CrmApiModelProperty("birthday",member.get("person/birthday")));
crmApiModel.addProperty(new CrmApiModelProperty("gender",user.get("member/gender")));
crmApiModel.addProperty(new CrmApiModelProperty("nationality",user.get("member/nationality")));
crmApiModel.addProperty(new CrmApiModelProperty("email",user.get("member/currentEmail")));
} catch (InternalCommunicationException e) {
e.printStackTrace();
throw e;
}
crmService.createContact(crmApiModel);
}
if (hceEx.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
System.err.println("HttpStatus.BAD_REQUEST");
}
}
}
}