controller for sending kafka messages
This commit is contained in:
parent
6ad105d04a
commit
80c2b7a272
2 changed files with 127 additions and 0 deletions
|
|
@ -0,0 +1,60 @@
|
|||
package ru.spcex.clearing.backendapi.controller.test;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonRawValue;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import ru.spcex.clearing.backendapi.domain.actions.IAction;
|
||||
import ru.spcex.clearing.platform.messaging.domain.ActionType;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.registry.TradingClearingRegistryNewRequest;
|
||||
|
||||
public class AnyKafkaMessageAction implements IAction<TradingClearingRegistryNewRequest> {
|
||||
@ApiModelProperty(value = "полное имя класса payload для BaseRequest", example = "ru.spcex.clearing.platform.messaging.domain.cud.schedule.LauncherCommandRequest")
|
||||
@JsonProperty
|
||||
private String fullClassName;
|
||||
@JsonProperty
|
||||
private String topicName;
|
||||
@JsonRawValue
|
||||
public String json;
|
||||
|
||||
@JsonProperty("json")
|
||||
private void unpackRawJson(JsonNode json) {
|
||||
this.json = json.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TradingClearingRegistryNewRequest toRequest() {
|
||||
var req = new TradingClearingRegistryNewRequest();
|
||||
return req;
|
||||
}
|
||||
|
||||
@ApiModelProperty(hidden = true)
|
||||
@Override
|
||||
public ActionType getActionType() {
|
||||
return ActionType.SYSTEM;
|
||||
}
|
||||
|
||||
public String getFullClassName() {
|
||||
return fullClassName;
|
||||
}
|
||||
|
||||
public void setFullClassName(String fullClassName) {
|
||||
this.fullClassName = fullClassName;
|
||||
}
|
||||
|
||||
public String getTopicName() {
|
||||
return topicName;
|
||||
}
|
||||
|
||||
public void setTopicName(String topicName) {
|
||||
this.topicName = topicName;
|
||||
}
|
||||
|
||||
public String getJson() {
|
||||
return json;
|
||||
}
|
||||
|
||||
public void setJson(String json) {
|
||||
this.json = json;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package ru.spcex.clearing.backendapi.controller.test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import io.swagger.annotations.ApiResponse;
|
||||
import io.swagger.annotations.ApiResponses;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import ru.spcex.clearing.backendapi.errors.BackEndError;
|
||||
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.error.ValidationException;
|
||||
import ru.spcex.platform.utils.text.TextUtil;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/anonymous/kafka-api")
|
||||
public class KafkaApiController {
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
private final KafkaSender kafkaSender;
|
||||
private static final ObjectMapper json = new ObjectMapper();
|
||||
static {
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public KafkaApiController(KafkaSender kafkaSender) {
|
||||
this.kafkaSender = kafkaSender;
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Test backend-api availability.")
|
||||
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = String.class)})
|
||||
@RequestMapping(method = RequestMethod.POST, path = "/any/message", produces = MediaType.TEXT_PLAIN_VALUE)
|
||||
@ResponseBody
|
||||
public String processGet(@ApiParam(value = "Параметры команды в JSON формате.", required = true)
|
||||
@RequestBody AnyKafkaMessageAction bankAccountNewAction) throws ClassNotFoundException, ValidationException, JsonProcessingException {
|
||||
log.info("Call test method for backend-api controller");
|
||||
validate(bankAccountNewAction);
|
||||
Class<?> parameterType = ClassUtils.forName(bankAccountNewAction.getFullClassName(), ClassUtils.getDefaultClassLoader());
|
||||
JavaType requestType = json.getTypeFactory().constructSimpleType(parameterType, null);
|
||||
Object obj = json.readValue(bankAccountNewAction.getJson(), requestType);
|
||||
Long idOfBaseRequestMessage = kafkaSender.sendRequestToQueue(bankAccountNewAction.getTopicName(), obj);
|
||||
return "success: baseRequest.id = " + idOfBaseRequestMessage;
|
||||
}
|
||||
|
||||
private void validate(AnyKafkaMessageAction bankAccountNewAction) throws ValidationException {
|
||||
if (TextUtil.isEmpty(bankAccountNewAction.getTopicName())) {
|
||||
throw new ValidationException(new EnumMessage(BackEndError.ValidationError, "topicName"));
|
||||
}
|
||||
if (TextUtil.isEmpty(bankAccountNewAction.getFullClassName())) {
|
||||
throw new ValidationException(new EnumMessage(BackEndError.ValidationError, "fullClassName"));
|
||||
}
|
||||
if (TextUtil.isEmpty(bankAccountNewAction.getJson())) {
|
||||
throw new ValidationException(new EnumMessage(BackEndError.ValidationError, "json"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue