Search in sources :

Example 1 with Command

use of io.gravitee.repository.management.model.Command in project gravitee-management-rest-api by gravitee-io.

the class CommandServiceImpl method ack.

@Override
public void ack(String messageId) {
    try {
        Optional<Command> optMsg = commandRepository.findById(messageId);
        // if not found, this is probably because it has been deleted
        if (optMsg.isPresent()) {
            Command msg = optMsg.get();
            if (msg.getAcknowledgments() == null) {
                msg.setAcknowledgments(Collections.singletonList(node.id()));
            } else if (!msg.getAcknowledgments().contains(node.id())) {
                msg.getAcknowledgments().add(node.id());
            }
            commandRepository.update(msg);
        }
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to acknowledge a message", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Command(io.gravitee.repository.management.model.Command)

Example 2 with Command

use of io.gravitee.repository.management.model.Command in project gravitee-management-rest-api by gravitee-io.

the class CommandServiceImpl method send.

@Override
public void send(NewCommandEntity messageEntity) {
    if (messageEntity.getTo() == null || messageEntity.getTo().isEmpty()) {
        throw new Message2RecipientNotFoundException();
    }
    Command command = new Command();
    command.setId(UuidString.generateRandom());
    command.setEnvironmentId(GraviteeContext.getCurrentEnvironment());
    command.setFrom(node.id());
    command.setTo(messageEntity.getTo());
    command.setTags(convert(messageEntity.getTags()));
    long now = System.currentTimeMillis();
    command.setCreatedAt(new Date(now));
    command.setUpdatedAt(command.getCreatedAt());
    command.setExpiredAt(new Date(now + (messageEntity.getTtlInSeconds() * 1000)));
    if (messageEntity.getContent() != null) {
        command.setContent(messageEntity.getContent());
    }
    try {
        commandRepository.create(command);
    } catch (TechnicalException ex) {
        logger.error("An error occurs while trying to create {}", command, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + command, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Message2RecipientNotFoundException(io.gravitee.rest.api.service.exceptions.Message2RecipientNotFoundException) Command(io.gravitee.repository.management.model.Command) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Command (io.gravitee.repository.management.model.Command)2 Message2RecipientNotFoundException (io.gravitee.rest.api.service.exceptions.Message2RecipientNotFoundException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 Date (java.util.Date)1