@Override public javax.jms.Message toMessage(Object object, Session session) throws JMSException, org.springframework.jms.support.converter.MessageConversionException { throw new org.springframework.jms.support.converter.MessageConversionException("Test exception"); } });
logger.error(ex.getMessage());
@Override public Object getPayload() { if (this.payload == null) { try { this.payload = unwrapPayload(); } catch (JMSException ex) { throw new MessageConversionException( "Failed to extract payload from [" + this.message + "]", ex); } } return this.payload; }
protected Message<?> toMessagingMessage(javax.jms.Message jmsMessage) { try { return (Message<?>) getMessagingMessageConverter().fromMessage(jmsMessage); } catch (JMSException ex) { throw new MessageConversionException("Could not convert JMS message", ex); } }
@Override public Object fromMessage(Message message) throws JMSException, MessageConversionException { try { JavaType targetJavaType = getJavaTypeForMessage(message); return convertToObject(message, targetJavaType); } catch (IOException ex) { throw new MessageConversionException("Failed to convert JSON message content", ex); } }
String typeId = message.getStringProperty(this.typeIdPropertyName); if (typeId == null) { throw new MessageConversionException( "Could not find type id property [" + this.typeIdPropertyName + "] on message [" + message.getJMSMessageID() + "] from destination [" + message.getJMSDestination() + "]"); throw new MessageConversionException("Failed to resolve type id [" + typeId + "]", ex);
/** * This implementation unmarshals the given {@link Message} into an object. * @see #unmarshalFromTextMessage * @see #unmarshalFromBytesMessage */ @Override public Object fromMessage(Message message) throws JMSException, MessageConversionException { Assert.state(this.unmarshaller != null, "No Unmarshaller set"); try { if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; return unmarshalFromTextMessage(textMessage, this.unmarshaller); } else if (message instanceof BytesMessage) { BytesMessage bytesMessage = (BytesMessage) message; return unmarshalFromBytesMessage(bytesMessage, this.unmarshaller); } else { return unmarshalFromMessage(message, this.unmarshaller); } } catch (IOException ex) { throw new MessageConversionException("Could not access message content: " + message, ex); } catch (XmlMappingException ex) { throw new MessageConversionException("Could not unmarshal message: " + message, ex); } }
/** * Create a JMS MapMessage for the given Map. * @param map the Map to convert * @param session current JMS session * @return the resulting message * @throws JMSException if thrown by JMS methods * @see javax.jms.Session#createMapMessage */ protected MapMessage createMessageForMap(Map<?, ?> map, Session session) throws JMSException { MapMessage message = session.createMapMessage(); for (Map.Entry<?, ?> entry : map.entrySet()) { Object key = entry.getKey(); if (!(key instanceof String)) { throw new MessageConversionException("Cannot convert non-String key of type [" + ObjectUtils.nullSafeClassName(key) + "] to JMS MapMessage entry"); } message.setObject((String) key, entry.getValue()); } return message; }
/** * Extract the message body from the given JMS message. * @param message the JMS {@code Message} * @return the content of the message, to be passed into the listener method * as an argument * @throws MessageConversionException if the message could not be extracted */ protected Object extractMessage(Message message) { try { MessageConverter converter = getMessageConverter(); if (converter != null) { return converter.fromMessage(message); } return message; } catch (JMSException ex) { throw new MessageConversionException("Could not convert JMS message", ex); } }
@Override public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { Message message; try { switch (this.targetType) { case TEXT: message = mapToTextMessage(object, session, this.objectMapper.writer()); break; case BYTES: message = mapToBytesMessage(object, session, this.objectMapper.writer()); break; default: message = mapToMessage(object, session, this.objectMapper.writer(), this.targetType); } } catch (IOException ex) { throw new MessageConversionException("Could not map JSON object [" + object + "]", ex); } setTypeIdOnMessage(object, message); return message; }
/** * This implementation marshals the given object to a {@link javax.jms.TextMessage} or * {@link javax.jms.BytesMessage}. The desired message type can be defined by setting * the {@link #setTargetType "marshalTo"} property. * @see #marshalToTextMessage * @see #marshalToBytesMessage */ @Override public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { Assert.state(this.marshaller != null, "No Marshaller set"); try { switch (this.targetType) { case TEXT: return marshalToTextMessage(object, session, this.marshaller); case BYTES: return marshalToBytesMessage(object, session, this.marshaller); default: return marshalToMessage(object, session, this.marshaller, this.targetType); } } catch (XmlMappingException | IOException ex) { throw new MessageConversionException("Could not marshal [" + object + "]", ex); } }
/** * Convert a BytesMessage to a Java Object with the specified type. * @param message the input message * @param targetJavaType the target type * @return the message converted to an object * @throws JMSException if thrown by JMS * @throws IOException in case of I/O errors */ protected Object convertFromBytesMessage(BytesMessage message, JavaType targetJavaType) throws JMSException, IOException { String encoding = this.encoding; if (this.encodingPropertyName != null && message.propertyExists(this.encodingPropertyName)) { encoding = message.getStringProperty(this.encodingPropertyName); } byte[] bytes = new byte[(int) message.getBodyLength()]; message.readBytes(bytes); try { String body = new String(bytes, encoding); return this.objectMapper.readValue(body, targetJavaType); } catch (UnsupportedEncodingException ex) { throw new MessageConversionException("Cannot convert bytes to String", ex); } }
protected Message toMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, MessageConversionException { Message message; try { switch (this.targetType) { case TEXT: message = mapToTextMessage(object, session, objectWriter); break; case BYTES: message = mapToBytesMessage(object, session, objectWriter); break; default: message = mapToMessage(object, session, objectWriter, this.targetType); } } catch (IOException ex) { throw new MessageConversionException("Could not map JSON object [" + object + "]", ex); } setTypeIdOnMessage(object, message); return message; }
/** * Build a JMS message to be sent as response based on the given result object. * @param session the JMS Session to operate on * @param result the content of the message, as returned from the listener method * @return the JMS {@code Message} (never {@code null}) * @throws JMSException if thrown by JMS API methods * @see #setMessageConverter */ protected Message buildMessage(Session session, Object result) throws JMSException { Object content = preProcessResponse(result instanceof JmsResponse ? ((JmsResponse<?>) result).getResponse() : result); MessageConverter converter = getMessageConverter(); if (converter != null) { if (content instanceof org.springframework.messaging.Message) { return this.messagingMessageConverter.toMessage(content, session); } else { return converter.toMessage(content, session); } } if (!(content instanceof Message)) { throw new MessageConversionException( "No MessageConverter specified - cannot handle message [" + content + "]"); } return (Message) content; }
throw new MessageConversionException("Cannot convert object of type [" + ObjectUtils.nullSafeClassName(object) + "] to JMS message. Supported message " + "payloads are: String, byte array, Map<String,?>, Serializable object.");
/** * Template method that allows for custom message unmarshalling. Invoked when {@link #fromMessage(Message)} is * invoked with a message that is not a {@link TextMessage} or {@link BytesMessage}. * <p/> * Default implemenetation throws a {@link MessageConversionException}. * * @param message the message * @param unmarshaller the unmarshaller to use * @return the unmarshalled object * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors */ protected Object unmarshalFromMessage(Message message, Unmarshaller unmarshaller) throws JMSException, IOException { throw new MessageConversionException( "MarshallingMessageConverter only supports TextMessages and BytesMessages"); } }
/** * Template method that allows for custom message marshalling. Invoked when {@link #setMarshalTo(int)} is not {@link * #MARSHAL_TO_TEXT_MESSAGE} or {@link #MARSHAL_TO_BYTES_MESSAGE}. * <p/> * Default implemenetation throws a {@link MessageConversionException}. * * @param object the object to marshal * @param session the JMS session * @param marshaller the marshaller to use * @return the resulting message * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors */ protected Message marshalToMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException { throw new MessageConversionException( "Unknown 'marshalTo' value [" + marshalTo + "]. Cannot convert object to Message"); }
@Override public Object getPayload() { if (this.payload == null) { try { this.payload = unwrapPayload(); } catch (JMSException ex) { throw new MessageConversionException( "Failed to extract payload from [" + this.message + "]", ex); } } return this.payload; }
@Override public Object getPayload() { if (this.payload == null) { try { this.payload = unwrapPayload(); } catch (JMSException ex) { throw new MessageConversionException( "Failed to extract payload from [" + this.message + "]", ex); } } return this.payload; }
/** * Determine a Jackson JavaType for the given JMS Message, * typically parsing a type id message property. * <p>The default implementation parses the configured type id property name * and consults the configured type id mapping. This can be overridden with * a different strategy, e.g. doing some heuristics based on message origin. * @param message the JMS Message to set the type id on * @throws javax.jms.JMSException if thrown by JMS methods * @see #setTypeIdOnMessage(Object, javax.jms.Message) * @see #setTypeIdPropertyName(String) * @see #setTypeIdMappings(java.util.Map) */ protected JavaType getJavaTypeForMessage(Message message) throws JMSException { String typeId = message.getStringProperty(this.typeIdPropertyName); if (typeId == null) { throw new MessageConversionException("Could not find type id property [" + this.typeIdPropertyName + "]"); } Class mappedClass = this.idClassMappings.get(typeId); if (mappedClass != null) { return this.objectMapper.getTypeFactory().constructType(mappedClass); } try { return this.objectMapper.getTypeFactory().constructType( ClassUtils.forName(typeId, getClass().getClassLoader())); } catch (Throwable ex) { throw new MessageConversionException("Failed to resolve type id [" + typeId + "]", ex); } }