org.springframework.amqp.support.converter.MessageConversionException Java Examples

The following examples show how to use org.springframework.amqp.support.converter.MessageConversionException. 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: ImageMessageConverter.java    From code with Apache License 2.0 6 votes vote down vote up
@Override
public Object fromMessage(Message message) throws MessageConversionException {
	System.err.println("-----------Image MessageConverter----------");
	
	Object _extName = message.getMessageProperties().getHeaders().get("extName");
	String extName = _extName == null ? "png" : _extName.toString();
	
	byte[] body = message.getBody();
	String fileName = UUID.randomUUID().toString();
	String path = "d:/010_test/" + fileName + "." + extName;
	File f = new File(path);
	try {
		Files.copy(new ByteArrayInputStream(body), f.toPath());
	} catch (IOException e) {
		e.printStackTrace();
	}
	return f;
}
 
Example #2
Source File: ProtobufMessageConverter.java    From common-project with Apache License 2.0 6 votes vote down vote up
@Override
public Message toMessage(Object obj, MessageProperties messageProperties) throws MessageConversionException {
    String messageType = obj.getClass().getSimpleName();
    Codec<Object> codec = codecMap.get(messageType);
    if (codec == null) {
        throw new MessageConversionException("不支持转换的消息类型:" + messageType);
    }
    messageProperties.setHeader("messageType", messageType);
    byte[] body;
    try {
        body = codec.encode(obj);
    } catch (Exception e) {
        throw new MessageConversionException("编码失败:" + messageType, e);
    }
    return new Message(body, messageProperties);
}
 
Example #3
Source File: TextMessageConverter.java    From code with Apache License 2.0 5 votes vote down vote up
@Override
public Object fromMessage(Message message) throws MessageConversionException {
	String contentType = message.getMessageProperties().getContentType();
	// testSendMessage4Text
	if(null != contentType && contentType.contains("text")) {
		return new String(message.getBody());
	}
	return message.getBody();
}
 
Example #4
Source File: PDFMessageConverter.java    From code with Apache License 2.0 5 votes vote down vote up
@Override
public Object fromMessage(Message message) throws MessageConversionException {
	System.err.println("-----------PDF MessageConverter----------");
	
	byte[] body = message.getBody();
	String fileName = UUID.randomUUID().toString();
	String path = "d:/010_test/" + fileName + ".pdf";
	File f = new File(path);
	try {
		Files.copy(new ByteArrayInputStream(body), f.toPath());
	} catch (IOException e) {
		e.printStackTrace();
	}
	return f;
}
 
Example #5
Source File: ProtobufMessageConverter.java    From common-project with Apache License 2.0 5 votes vote down vote up
@Override
public Object fromMessage(Message message) throws MessageConversionException {
    String messageType = message.getMessageProperties().getHeaders().get("messageType").toString();
    Codec<Object> codec = codecMap.get(messageType);
    if (codec == null) {
        throw new MessageConversionException("不支持转换的消息类型:" + messageType);
    }
    Object obj;
    try {
        obj = codec.decode(message.getBody());
    } catch (Exception e) {
        throw new MessageConversionException("解码失败:" + messageType, e);
    }
    return obj;
}
 
Example #6
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests not allowed body in message")
public void createThingWithWrongBody() {
    final String knownThingId = "3";

    assertThatExceptionOfType(MessageConversionException.class)
            .as("MessageConversionException was excepeted due to wrong body")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(
                    createMessage("Not allowed Body".getBytes(), getThingCreatedMessageProperties(knownThingId)),
                    MessageType.THING_CREATED.name(), TENANT, VIRTUAL_HOST));
}
 
Example #7
Source File: BaseAmqpServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests invalid null message content")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void convertMessageWithNullContent() {
    try {
        baseAmqpService.convertMessage(new Message(null, createJsonProperties()), DmfActionUpdateStatus.class);
        fail("Expected MessageConversionException for inavlid JSON");
    } catch (final MessageConversionException e) {
        // expected
    }

}
 
Example #8
Source File: BaseAmqpServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests invalid empty message content")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void updateActionStatusWithEmptyContent() {
    try {
        baseAmqpService.convertMessage(new Message("".getBytes(), createJsonProperties()),
                DmfActionUpdateStatus.class);
        fail("Expected MessageConversionException for inavlid JSON");
    } catch (final MessageConversionException e) {
        // expected
    }
}
 
Example #9
Source File: BaseAmqpServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests invalid json message content")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void updateActionStatusWithInvalidJsonContent() {
    try {
        baseAmqpService.convertMessage(new Message("Invalid Json".getBytes(), createJsonProperties()),
                DmfActionUpdateStatus.class);
        fail("Expected MessageConversionException for inavlid JSON");
    } catch (final MessageConversionException e) {
        // expected
    }
}
 
Example #10
Source File: ImageMessageConverter.java    From code with Apache License 2.0 4 votes vote down vote up
@Override
public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
	throw new MessageConversionException(" convert error ! ");
}
 
Example #11
Source File: TextMessageConverter.java    From code with Apache License 2.0 4 votes vote down vote up
@Override
public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
	return new Message(object.toString().getBytes(), messageProperties);
}
 
Example #12
Source File: PDFMessageConverter.java    From code with Apache License 2.0 4 votes vote down vote up
@Override
public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
	throw new MessageConversionException(" convert error ! ");
}
 
Example #13
Source File: RabbitMessageChannelBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Override
public Object fromMessage(Message message) throws MessageConversionException {
	return message.getBody();
}
 
Example #14
Source File: BaseAmqpService.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
protected void checkMessageBody(@NotNull final Message message) {
    if (isMessageBodyEmpty(message)) {
        throw new MessageConversionException("Message body cannot be null");
    }
}
 
Example #15
Source File: DelayedRequeueExceptionStrategy.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isMessageException(final Throwable cause) {
    return cause instanceof InvalidTargetAddressException || cause instanceof MessageConversionException
            || cause instanceof MessageHandlingException;
}