List of usage examples for org.springframework.amqp.core MessageProperties setContentLength
public void setContentLength(long contentLength)
From source file:amqp.spring.converter.StringConverter.java
@Override protected Message createMessage(Object object, MessageProperties messageProperties) { try {/*from www . j a v a2 s. co m*/ byte[] body = null; if (object != null) { body = object.toString().getBytes(this.encoding); } String msgContentType = this.contentType == null ? DEFAULT_CONTENT_TYPE : this.contentType; messageProperties.setContentType(msgContentType); messageProperties.setContentEncoding(this.encoding); messageProperties.setContentLength(body != null ? body.length : 0); return new Message(body, messageProperties); } catch (UnsupportedEncodingException ex) { LOG.error("Cannot encode strings as {}", this.encoding, ex); throw new MessageConversionException("Cannot encode strings as " + this.encoding, ex); } }
From source file:com.dc.gameserver.extComponents.SpringRabbitmq.FastJsonMessageConverter.java
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException { byte[] bytes = null; try {/*from ww w . jav a 2 s .c o m*/ String jsonString = JSON.toJSONString(objectToConvert); bytes = jsonString.getBytes(this.defaultCharset); } catch (UnsupportedEncodingException e) { throw new MessageConversionException("Failed to convert Message content", e); } messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON); messageProperties.setContentEncoding(this.defaultCharset); messageProperties.setContentLength(bytes.length); return new Message(bytes, messageProperties); }
From source file:com.github.liyp.rabbitmq.demo.FastJsonMessageConverter.java
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException { byte[] bytes = null; try {//from www . jav a 2 s. c om String jsonString = JSON.toJSONString(objectToConvert); bytes = jsonString.getBytes(this.defaultCharset); } catch (UnsupportedEncodingException e) { throw new MessageConversionException("Failed to convert Message content", e); } messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON); messageProperties.setContentEncoding(this.defaultCharset); if (bytes != null) { messageProperties.setContentLength(bytes.length); } return new Message(bytes, messageProperties); }
From source file:amqp.spring.converter.XStreamConverter.java
@Override protected Message createMessage(Object object, MessageProperties messageProperties) { try {//from www.j a v a 2 s.c o m byte[] body = null; if (object != null) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); StaxWriter writer = new StaxWriter(new QNameMap(), this.outputFactory.createXMLStreamWriter(outStream)); this.objectMapper.marshal(object, writer); body = outStream.toByteArray(); } messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON); messageProperties.setContentEncoding(this.encoding); messageProperties.setContentLength(body != null ? body.length : 0); classMapper.fromClass(object.getClass(), messageProperties); return new Message(body, messageProperties); } catch (XMLStreamException ex) { String typeId = (String) messageProperties.getHeaders() .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME); LOG.error("XMLStreamException trying to marshal message of type {}", typeId, ex); throw new MessageConversionException("Could not marshal message of type " + typeId, ex); } catch (XStreamException ex) { //For some reason messages appear to be getting eaten at this stage... very nasty when you try to troubleshoot. String typeId = (String) messageProperties.getHeaders() .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME); LOG.error("XStreamException trying to marshal message of type {}", typeId, ex); throw new MessageConversionException("Could not marshal message of type " + typeId, ex); } }
From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java
/** * Execute the specified listener, committing or rolling back the transaction afterwards (if necessary). * * @param channel the Rabbit Channel to operate on * @param messageIn the received Rabbit Message * @throws Exception Any Exception.//from w w w . j av a 2s . c o m * * @see #invokeListener * @see #handleListenerException */ protected void executeListener(Channel channel, Message messageIn) throws Exception { if (!isRunning()) { if (logger.isWarnEnabled()) { logger.warn( "Rejecting received message because the listener container has been stopped: " + messageIn); } throw new MessageRejectedWhileStoppingException(); } try { Message message = messageIn; if (this.afterReceivePostProcessors != null) { for (MessagePostProcessor processor : this.afterReceivePostProcessors) { message = processor.postProcessMessage(message); } } Object batchFormat = message.getMessageProperties().getHeaders() .get(MessageProperties.SPRING_BATCH_FORMAT); if (MessageProperties.BATCH_FORMAT_LENGTH_HEADER4.equals(batchFormat) && this.deBatchingEnabled) { ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBody()); MessageProperties messageProperties = message.getMessageProperties(); messageProperties.getHeaders().remove(MessageProperties.SPRING_BATCH_FORMAT); while (byteBuffer.hasRemaining()) { int length = byteBuffer.getInt(); if (length < 0 || length > byteBuffer.remaining()) { throw new ListenerExecutionFailedException("Bad batched message received", new MessageConversionException( "Insufficient batch data at offset " + byteBuffer.position()), message); } byte[] body = new byte[length]; byteBuffer.get(body); messageProperties.setContentLength(length); // Caveat - shared MessageProperties. Message fragment = new Message(body, messageProperties); invokeListener(channel, fragment); } } else { invokeListener(channel, message); } } catch (Exception ex) { if (messageIn.getMessageProperties().isFinalRetryForMessageWithNoId()) { if (this.statefulRetryFatalWithNullMessageId) { throw new FatalListenerExecutionException( "Illegal null id in message. Failed to manage retry for message: " + messageIn); } else { throw new ListenerExecutionFailedException("Cannot retry message more than once without an ID", new AmqpRejectAndDontRequeueException("Not retryable; rejecting and not requeuing", ex), messageIn); } } handleListenerException(ex); throw ex; } }
From source file:org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.java
@Override protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException { byte[] bytes; try {/*from w w w .j a va 2s .c o m*/ String jsonString = this.jsonObjectMapper.writeValueAsString(objectToConvert); bytes = jsonString.getBytes(getDefaultCharset()); } catch (IOException e) { throw new MessageConversionException("Failed to convert Message content", e); } messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON); messageProperties.setContentEncoding(getDefaultCharset()); messageProperties.setContentLength(bytes.length); if (getClassMapper() == null) { getJavaTypeMapper().fromJavaType(this.jsonObjectMapper.constructType(objectToConvert.getClass()), messageProperties); } else { getClassMapper().fromClass(objectToConvert.getClass(), messageProperties); } return new Message(bytes, messageProperties); }
From source file:org.springframework.amqp.support.converter.JsonMessageConverter.java
@Override protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException { byte[] bytes = null; try {//from w w w . j a va2 s .c o m String jsonString = jsonObjectMapper.writeValueAsString(objectToConvert); bytes = jsonString.getBytes(this.defaultCharset); } catch (UnsupportedEncodingException e) { throw new MessageConversionException("Failed to convert Message content", e); } catch (JsonGenerationException e) { throw new MessageConversionException("Failed to convert Message content", e); } catch (JsonMappingException e) { throw new MessageConversionException("Failed to convert Message content", e); } catch (IOException e) { throw new MessageConversionException("Failed to convert Message content", e); } messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON); messageProperties.setContentEncoding(this.defaultCharset); if (bytes != null) { messageProperties.setContentLength(bytes.length); } if (getClassMapper() == null) { getJavaTypeMapper().fromJavaType(type(objectToConvert.getClass()), messageProperties); } else { getClassMapper().fromClass(objectToConvert.getClass(), messageProperties); } return new Message(bytes, messageProperties); }