org.springframework.amqp.AmqpRejectAndDontRequeueException Java Examples

The following examples show how to use org.springframework.amqp.AmqpRejectAndDontRequeueException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeleteModuleConsumer.java    From abixen-platform with GNU Lesser General Public License v2.1 6 votes vote down vote up
@StreamListener(DeleteModuleProcessor.INPUT)
public void consume(final Message<DeleteModuleCommand> message) {
    final DeleteModuleCommand deleteModuleCommand = message.getPayload();
    log.info("Received message {}", deleteModuleCommand);

    try {
        switch (deleteModuleCommand.getModuleTypeName()) {
            case "multi-visualisation":
                chartConfigurationManagementService.deleteChartConfiguration(deleteModuleCommand.getModuleId());
                break;
            default:
                throw new PlatformRuntimeException("Wrong module type name: " + deleteModuleCommand.getModuleTypeName());
        }
    } catch (Exception e) {
        log.error("Can not delete module: {}", e);
        throw new AmqpRejectAndDontRequeueException(e);
    }

    log.info("Module id: {}, type: {} removed successfully.", deleteModuleCommand.getModuleId(), deleteModuleCommand.getModuleTypeName());
}
 
Example #2
Source File: DmfReceiverService.java    From hawkbit-examples with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Method to validate if content type is set in the message properties.
 *
 * @param message
 *            the message to get validated
 */
private void checkContentTypeJson(final Message message) {
    if (message.getBody().length == 0) {
        return;
    }
    final MessageProperties messageProperties = message.getMessageProperties();
    final String headerContentType = (String) messageProperties.getHeaders().get("content-type");
    if (null != headerContentType) {
        messageProperties.setContentType(headerContentType);
    }
    final String contentType = messageProperties.getContentType();
    if (contentType != null && contentType.contains("json")) {
        return;
    }
    throw new AmqpRejectAndDontRequeueException("Content-Type is not JSON compatible");
}
 
Example #3
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
@Test
@Description("Tests the update of an action of a target without a exist action id")
public void updateActionStatusWithoutExistActionId() {
    final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
    messageProperties.setHeader(MessageHeaderKey.TOPIC, EventTopic.UPDATE_ACTION_STATUS.name());
    when(controllerManagementMock.findActionWithDetails(anyLong())).thenReturn(Optional.empty());

    final DmfActionUpdateStatus actionUpdateStatus = createActionUpdateStatus(DmfActionStatus.DOWNLOAD);
    final Message message = amqpMessageHandlerService.getMessageConverter().toMessage(actionUpdateStatus,
            messageProperties);

    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no action id was set")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.EVENT.name(), TENANT,
                    VIRTUAL_HOST));
}
 
Example #4
Source File: AmqpMessageHandlerService.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Method to create a new target or to find the target if it already exists and
 * update its poll time, status and optionally its name.
 *
 * @param message
 *            the message that contains replyTo property and optionally the name
 *            in body
 * @param virtualHost
 *            the virtual host
 */
private void registerTarget(final Message message, final String virtualHost) {
    final String thingId = getStringHeaderKey(message, MessageHeaderKey.THING_ID, THING_ID_NULL);
    final String replyTo = message.getMessageProperties().getReplyTo();

    if (StringUtils.isEmpty(replyTo)) {
        logAndThrowMessageError(message, "No ReplyTo was set for the createThing message.");
    }

    try {
        final URI amqpUri = IpUtil.createAmqpUri(virtualHost, replyTo);
        final Target target;
        if (isOptionalMessageBodyEmpty(message)) {
            target = controllerManagement.findOrRegisterTargetIfItDoesNotExist(thingId, amqpUri);
        } else {
            checkContentTypeJson(message);

                target = controllerManagement.findOrRegisterTargetIfItDoesNotExist(thingId, amqpUri, convertMessage(message, DmfCreateThing.class).getName());

        }
        LOG.debug("Target {} reported online state.", thingId);
        sendUpdateCommandToTarget(target);
    } catch (final EntityAlreadyExistsException e) {
        throw new AmqpRejectAndDontRequeueException("Tried to register previously registered target, message will be ignored!", e);
    }
}
 
Example #5
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
@Test
@Description("Tests a invalid message without event topic")
public void invalidEventTopic() {
    final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
    final Message message = new Message(new byte[0], messageProperties);

    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to unknown message type").isThrownBy(
                    () -> amqpMessageHandlerService.onMessage(message, "unknownMessageType", TENANT, VIRTUAL_HOST));

    messageProperties.setHeader(MessageHeaderKey.TOPIC, "wrongTopic");
    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to unknown topic").isThrownBy(() -> amqpMessageHandlerService
                    .onMessage(message, MessageType.EVENT.name(), TENANT, VIRTUAL_HOST));

    messageProperties.setHeader(MessageHeaderKey.TOPIC, EventTopic.CANCEL_DOWNLOAD.name());
    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "because there was no event topic")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.EVENT.name(), TENANT,
                    VIRTUAL_HOST));
}
 
Example #6
Source File: DeleteModuleConsumer.java    From abixen-platform with GNU Lesser General Public License v2.1 6 votes vote down vote up
@StreamListener(DeleteModuleProcessor.INPUT)
public void consume(final Message<DeleteModuleCommand> message) {
    final DeleteModuleCommand deleteModuleCommand = message.getPayload();
    log.info("Received message {}", deleteModuleCommand);

    try {
        switch (deleteModuleCommand.getModuleTypeName()) {
            case "web-content":
                webContentModuleConfigurationService.deleteByModuleId(deleteModuleCommand.getModuleId());
                break;
            default:
                throw new PlatformRuntimeException("Wrong module type name: " + deleteModuleCommand.getModuleTypeName());
        }
    } catch (Exception e) {
        log.error("Can not delete module: {}", e);
        throw new AmqpRejectAndDontRequeueException(e);
    }

    log.info("Module id: {}, type: {} removed successfully.", deleteModuleCommand.getModuleId(), deleteModuleCommand.getModuleTypeName());
}
 
Example #7
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests the creation of a thing without a 'reply to' header in message.")
public void createThingWithoutReplyTo() {
    final MessageProperties messageProperties = createMessageProperties(MessageType.THING_CREATED, null);
    messageProperties.setHeader(MessageHeaderKey.THING_ID, "1");
    final Message message = messageConverter.toMessage("", messageProperties);

    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no replyTo header was set")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_CREATED.name(), TENANT,
                    VIRTUAL_HOST));
}
 
Example #8
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests the deletion of a target/thing with missing thingId")
public void deleteThingWithoutThingId() {
    // prepare invalid message
    final MessageProperties messageProperties = createMessageProperties(MessageType.THING_REMOVED);
    final Message message = messageConverter.toMessage(new byte[0], messageProperties);

    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no thingId was set")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_REMOVED.name(), TENANT,
                    VIRTUAL_HOST));
}
 
Example #9
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests the update of an action of a target without a exist action id")
public void updateActionStatusWithoutActionId() {
    when(controllerManagementMock.findActionWithDetails(anyLong())).thenReturn(Optional.empty());
    final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
    messageProperties.setHeader(MessageHeaderKey.TOPIC, EventTopic.UPDATE_ACTION_STATUS.name());
    final DmfActionUpdateStatus actionUpdateStatus = new DmfActionUpdateStatus(1L, DmfActionStatus.DOWNLOAD);
    final Message message = amqpMessageHandlerService.getMessageConverter().toMessage(actionUpdateStatus,
            messageProperties);

    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no action id was set")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.EVENT.name(), TENANT,
                    VIRTUAL_HOST));
}
 
Example #10
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests the call of the same method that incoming RabbitMQ messages would access with an unknown message type.")
public void unknownMessageType() {
    final String type = "bumlux";
    final MessageProperties messageProperties = createMessageProperties(MessageType.THING_CREATED);
    messageProperties.setHeader(MessageHeaderKey.THING_ID, "");
    final Message message = messageConverter.toMessage(new byte[0], messageProperties);

    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to unknown message type")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
}
 
Example #11
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests the creation of a target/thing without a thingID by calling the same method that incoming RabbitMQ messages would access.")
public void createThingWithoutID() {
    final MessageProperties messageProperties = createMessageProperties(MessageType.THING_CREATED);
    final Message message = messageConverter.toMessage(new byte[0], messageProperties);

    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no thingId was set")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_CREATED.name(), TENANT,
                    VIRTUAL_HOST));
}
 
Example #12
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests not allowed content-type in message")
public void wrongContentType() {
    final MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("xml");
    final Message message = new Message(new byte[0], messageProperties);
    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to wrong content type")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_CREATED.name(), TENANT,
                    VIRTUAL_HOST));
}
 
Example #13
Source File: AmqpAuthenticationMessageHandler.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Executed on an authentication request.
 * 
 * @param message
 *            the amqp message
 * @return the rpc message back to supplier.
 */
@RabbitListener(queues = "${hawkbit.dmf.rabbitmq.authenticationReceiverQueue:authentication_receiver}", containerFactory = "listenerContainerFactory")
public Message onAuthenticationRequest(final Message message) {
    checkContentTypeJson(message);
    final SecurityContext oldContext = SecurityContextHolder.getContext();
    try {
        return handleAuthenticationMessage(message);
    } catch (final RuntimeException ex) {
        throw new AmqpRejectAndDontRequeueException(ex);
    } finally {
        SecurityContextHolder.setContext(oldContext);
    }
}
 
Example #14
Source File: AmqpMessageHandlerService.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * * Executed if a amqp message arrives.
 * 
 * @param message
 *            the message
 * @param type
 *            the type
 * @param tenant
 *            the tenant
 * @param virtualHost
 *            the virtual host
 * @return the rpc message back to supplier.
 */
public Message onMessage(final Message message, final String type, final String tenant, final String virtualHost) {
    if (StringUtils.isEmpty(type) || StringUtils.isEmpty(tenant)) {
        throw new AmqpRejectAndDontRequeueException("Invalid message! tenant and type header are mandatory!");
    }

    final SecurityContext oldContext = SecurityContextHolder.getContext();
    try {
        final MessageType messageType = MessageType.valueOf(type);
        switch (messageType) {
        case THING_CREATED:
            setTenantSecurityContext(tenant);
            registerTarget(message, virtualHost);
            break;
        case THING_REMOVED:
            setTenantSecurityContext(tenant);
            deleteTarget(message);
            break;
        case EVENT:
            checkContentTypeJson(message);
            setTenantSecurityContext(tenant);
            handleIncomingEvent(message);
            break;
        case PING:
            if (isCorrelationIdNotEmpty(message)) {
                amqpMessageDispatcherService.sendPingReponseToDmfReceiver(message, tenant, virtualHost);
            }
            break;
        default:
            logAndThrowMessageError(message, "No handle method was found for the given message type.");
        }
    } catch (final IllegalArgumentException ex) {
        throw new AmqpRejectAndDontRequeueException("Invalid message!", ex);
    } finally {
        SecurityContextHolder.setContext(oldContext);
    }
    return null;
}
 
Example #15
Source File: BaseAmqpService.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
protected static void checkContentTypeJson(final Message message) {
    final MessageProperties messageProperties = message.getMessageProperties();
    if (messageProperties.getContentType() != null && messageProperties.getContentType().contains("json")) {
        return;
    }
    throw new AmqpRejectAndDontRequeueException("Content-Type is not JSON compatible");
}
 
Example #16
Source File: BaseAmqpService.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
protected static final void logAndThrowMessageError(final Message message, final String error) {
    LOGGER.debug("Warning! \"{}\" reported by message: {}", error, message);
    throw new AmqpRejectAndDontRequeueException(error);
}
 
Example #17
Source File: MessageListenerRetryTest.java    From sinavi-jfw with Apache License 2.0 4 votes vote down vote up
public void handleMessage(RetryTestBean message) {
    unretry.countDown();
    throw new AmqpRejectAndDontRequeueException("test");
}
 
Example #18
Source File: CustomErrorHandler.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void handleError(Throwable t) {
    if (!(t.getCause() instanceof BusinessException)) {
        throw new AmqpRejectAndDontRequeueException("Error Handler converted exception to fatal", t);
    }
}