List of usage examples for javax.jms MessageFormatException MessageFormatException
public MessageFormatException(String reason)
From source file:com.amazon.sqs.javamessaging.SQSMessageProducer.java
void sendInternal(Queue queue, Message message) throws JMSException { checkClosed();/*from ww w.ja va 2 s .c o m*/ String sqsMessageBody = null; String messageType = null; if (message instanceof SQSMessage) { message.setJMSDestination(queue); if (message instanceof SQSBytesMessage) { sqsMessageBody = Base64.encodeAsString(((SQSBytesMessage) message).getBodyAsBytes()); messageType = SQSMessage.BYTE_MESSAGE_TYPE; } else if (message instanceof SQSObjectMessage) { sqsMessageBody = ((SQSObjectMessage) message).getMessageBody(); messageType = SQSMessage.OBJECT_MESSAGE_TYPE; } else if (message instanceof SQSTextMessage) { sqsMessageBody = ((SQSTextMessage) message).getText(); messageType = SQSMessage.TEXT_MESSAGE_TYPE; } } else { throw new MessageFormatException( "Unrecognized message type. Messages have to be one of: SQSBytesMessage, SQSObjectMessage, or SQSTextMessage"); } if (sqsMessageBody == null || sqsMessageBody.isEmpty()) { throw new JMSException("Message body cannot be null or empty"); } Map<String, MessageAttributeValue> messageAttributes = propertyToMessageAttribute((SQSMessage) message); addMessageTypeReservedAttribute(messageAttributes, (SQSMessage) message, messageType); SendMessageRequest sendMessageRequest = new SendMessageRequest(((SQSQueueDestination) queue).getQueueUrl(), sqsMessageBody); sendMessageRequest.setMessageAttributes(messageAttributes); String messageId = amazonSQSClient.sendMessage(sendMessageRequest).getMessageId(); LOG.info("Message sent to SQS with SQS-assigned messageId: " + messageId); /** TODO: Do not support disableMessageID for now.*/ message.setJMSMessageID(String.format(SQSMessagingClientConstants.MESSAGE_ID_FORMAT, messageId)); ((SQSMessage) message).setSQSMessageId(messageId); }
From source file:com.amazon.sqs.javamessaging.message.SQSBytesMessage.java
/** * Writes an object to the bytes message stream. * <P>/*from w w w. j a va 2 s . c o m*/ * This method works only for the boxed primitive object types * (Integer, Double, Long ...), String objects, and byte arrays. * * @param value * The Java object to be written * @throws JMSException * If the JMS provider fails to write the message due to some * internal error. * @throws MessageNotWriteableException * If the message is in read-only mode. * @throws MessageFormatException * If the object is of an invalid type. * @throws NullPointerException * If the object is null. */ @Override public void writeObject(Object value) throws JMSException { if (value == null) { throw new NullPointerException("Cannot write null value of object"); } if (value instanceof Boolean) { writeBoolean((Boolean) value); } else if (value instanceof Character) { writeChar((Character) value); } else if (value instanceof Byte) { writeByte((Byte) value); } else if (value instanceof Short) { writeShort((Short) value); } else if (value instanceof Integer) { writeInt((Integer) value); } else if (value instanceof Long) { writeLong((Long) value); } else if (value instanceof Float) { writeFloat((Float) value); } else if (value instanceof Double) { writeDouble((Double) value); } else if (value instanceof String) { writeUTF(value.toString()); } else if (value instanceof byte[]) { writeBytes((byte[]) value); } else { throw new MessageFormatException("Cannot write non-primitive type: " + value.getClass()); } }
From source file:org.apache.camel.component.jms.JmsBinding.java
/** * /*from w w w.j a va2 s . c o m*/ * Create the {@link Message} * * @return jmsMessage or null if the mapping was not successfully */ protected Message createJmsMessageForType(Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context, JmsMessageType type) throws JMSException { switch (type) { case Text: { TextMessage message = session.createTextMessage(); String payload = context.getTypeConverter().convertTo(String.class, exchange, body); message.setText(payload); return message; } case Bytes: { BytesMessage message = session.createBytesMessage(); byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange, body); message.writeBytes(payload); return message; } case Map: { MapMessage message = session.createMapMessage(); Map payload = context.getTypeConverter().convertTo(Map.class, exchange, body); populateMapMessage(message, payload, context); return message; } case Object: Serializable payload; try { payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class, exchange, body); } catch (NoTypeConversionAvailableException e) { // cannot convert to serializable then thrown an exception to avoid sending a null message JMSException cause = new MessageFormatException(e.getMessage()); cause.initCause(e); throw cause; } return session.createObjectMessage(payload); default: break; } return null; }
From source file:org.mule.transport.jms.JmsMessageUtils.java
private static Message listToMessage(List<?> value, Session session) throws JMSException { StreamMessage sMsg = session.createStreamMessage(); for (Iterator<?> iter = value.iterator(); iter.hasNext();) { Object o = iter.next();/*from w w w . ja va 2s. c o m*/ if (validateStreamMessageType(o)) { sMsg.writeObject(o); } else { throw new MessageFormatException( String.format("Invalid type passed to StreamMessage: %s . Allowed types are: " + "Boolean, Byte, Short, Character, Integer, Long, Float, Double," + "String and byte[]", ClassUtils.getShortClassName(o, "null"))); } } return sMsg; }
From source file:org.skyscreamer.nevado.jms.message.NevadoBytesMessage.java
/** * Writes an object to the bytes message stream. * <P>// w ww . j a v a 2s . c om * This method works only for the objectified primitive object types (<code>Integer</code>,<code>Double</code>, * <code>Long</code> ...), <code>String</code> objects, and byte arrays. * * @param value the object in the Java programming language ("Java object") to be written; it must not be null * @throws JMSException if the JMS provider fails to write the message due to some internal error. * @throws MessageFormatException if the object is of an invalid type. * @throws MessageNotWriteableException if the message is in read-only mode. * @throws java.lang.NullPointerException if the parameter <code>value</code> is null. */ public void writeObject(Object value) throws JMSException { if (value == null) { throw new NullPointerException(); } initializeWriting(); if (value instanceof Boolean) { writeBoolean(((Boolean) value).booleanValue()); } else if (value instanceof Character) { writeChar(((Character) value).charValue()); } else if (value instanceof Byte) { writeByte(((Byte) value).byteValue()); } else if (value instanceof Short) { writeShort(((Short) value).shortValue()); } else if (value instanceof Integer) { writeInt(((Integer) value).intValue()); } else if (value instanceof Double) { writeDouble(((Double) value).doubleValue()); } else if (value instanceof Long) { writeLong(((Long) value).longValue()); } else if (value instanceof Float) { writeFloat(((Float) value).floatValue()); } else if (value instanceof Double) { writeDouble(((Double) value).doubleValue()); } else if (value instanceof String) { writeUTF(value.toString()); } else if (value instanceof byte[]) { writeBytes((byte[]) value); } else { throw new MessageFormatException("Cannot write non-primitive type:" + value.getClass()); } }
From source file:org.skyscreamer.nevado.jms.message.NevadoStreamMessage.java
/** * Reads a <code>boolean</code> from the stream message. * * @return the <code>boolean</code> value read * @throws JMSException//from w ww . j a v a 2s . c om * if the JMS provider fails to read the message due to some * internal error. * @throws MessageEOFException * if unexpected end of message stream has been reached. * @throws MessageFormatException * if this type conversion is invalid. * @throws javax.jms.MessageNotReadableException * if the message is in write-only mode. */ public boolean readBoolean() throws JMSException { initializeReading(); try { this.dataIn.mark(10); int type = this.dataIn.read(); if (type == -1) { throw new MessageEOFException("reached end of data"); } if (type == MarshallingSupport.BOOLEAN_TYPE) { return this.dataIn.readBoolean(); } if (type == MarshallingSupport.STRING_TYPE) { return Boolean.valueOf(this.dataIn.readUTF()).booleanValue(); } if (type == MarshallingSupport.NULL) { this.dataIn.reset(); throw new NullPointerException("Cannot convert NULL value to boolean."); } else { this.dataIn.reset(); throw new MessageFormatException(" not a boolean type"); } } catch (EOFException e) { String exMessage = "Reached premature EOF: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } catch (IOException e) { String exMessage = "Could not read boolean: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } }
From source file:org.skyscreamer.nevado.jms.message.NevadoStreamMessage.java
/** * Reads a <code>byte</code> value from the stream message. * * @return the next byte from the stream message as a 8-bit * <code>byte</code> * @throws JMSException//from w w w .ja v a2 s. c o m * if the JMS provider fails to read the message due to some * internal error. * @throws MessageEOFException * if unexpected end of message stream has been reached. * @throws MessageFormatException * if this type conversion is invalid. * @throws javax.jms.MessageNotReadableException * if the message is in write-only mode. */ public byte readByte() throws JMSException { initializeReading(); try { this.dataIn.mark(10); int type = this.dataIn.read(); if (type == -1) { throw new MessageEOFException("reached end of data"); } if (type == MarshallingSupport.BYTE_TYPE) { return this.dataIn.readByte(); } if (type == MarshallingSupport.STRING_TYPE) { return Byte.valueOf(this.dataIn.readUTF()).byteValue(); } if (type == MarshallingSupport.NULL) { this.dataIn.reset(); throw new NullPointerException("Cannot convert NULL value to byte."); } else { this.dataIn.reset(); throw new MessageFormatException(" not a byte type"); } } catch (NumberFormatException mfe) { try { this.dataIn.reset(); } catch (IOException ioe) { JMSException jmsEx = new MessageFormatException(ioe.getMessage()); jmsEx.setLinkedException(ioe); throw jmsEx; } throw mfe; } catch (EOFException e) { String exMessage = "Reached premature EOF: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } catch (IOException e) { String exMessage = "Could not read boolean: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } }
From source file:org.skyscreamer.nevado.jms.message.NevadoStreamMessage.java
/** * Reads a 16-bit integer from the stream message. * * @return a 16-bit integer from the stream message * @throws JMSException//from w w w . ja va 2 s. c o m * if the JMS provider fails to read the message due to some * internal error. * @throws MessageEOFException * if unexpected end of message stream has been reached. * @throws MessageFormatException * if this type conversion is invalid. * @throws MessageNotReadableException * if the message is in write-only mode. */ public short readShort() throws JMSException { initializeReading(); try { this.dataIn.mark(17); int type = this.dataIn.read(); if (type == -1) { throw new MessageEOFException("reached end of data"); } if (type == MarshallingSupport.SHORT_TYPE) { return this.dataIn.readShort(); } if (type == MarshallingSupport.BYTE_TYPE) { return this.dataIn.readByte(); } if (type == MarshallingSupport.STRING_TYPE) { return Short.valueOf(this.dataIn.readUTF()).shortValue(); } if (type == MarshallingSupport.NULL) { this.dataIn.reset(); throw new NullPointerException("Cannot convert NULL value to short."); } else { this.dataIn.reset(); throw new MessageFormatException(" not a short type"); } } catch (NumberFormatException mfe) { try { this.dataIn.reset(); } catch (IOException ioe) { JMSException jmsEx = new MessageFormatException(ioe.getMessage()); jmsEx.setLinkedException(ioe); throw jmsEx; } throw mfe; } catch (EOFException e) { String exMessage = "Reached premature EOF: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } catch (IOException e) { String exMessage = "Could not read boolean: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } }
From source file:org.skyscreamer.nevado.jms.message.NevadoStreamMessage.java
/** * Reads a Unicode character value from the stream message. * * @return a Unicode character from the stream message * @throws JMSException//from w w w. j a v a2s . c om * if the JMS provider fails to read the message due to some * internal error. * @throws MessageEOFException * if unexpected end of message stream has been reached. * @throws MessageFormatException * if this type conversion is invalid * @throws MessageNotReadableException * if the message is in write-only mode. */ public char readChar() throws JMSException { initializeReading(); try { this.dataIn.mark(17); int type = this.dataIn.read(); if (type == -1) { throw new MessageEOFException("reached end of data"); } if (type == MarshallingSupport.CHAR_TYPE) { return this.dataIn.readChar(); } if (type == MarshallingSupport.NULL) { this.dataIn.reset(); throw new NullPointerException("Cannot convert NULL value to char."); } else { this.dataIn.reset(); throw new MessageFormatException(" not a char type"); } } catch (NumberFormatException mfe) { try { this.dataIn.reset(); } catch (IOException ioe) { JMSException jmsEx = new MessageFormatException(ioe.getMessage()); jmsEx.setLinkedException(ioe); throw jmsEx; } throw mfe; } catch (EOFException e) { String exMessage = "Reached premature EOF: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } catch (IOException e) { String exMessage = "Could not read boolean: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } }
From source file:org.skyscreamer.nevado.jms.message.NevadoStreamMessage.java
/** * Reads a 32-bit integer from the stream message. * * @return a 32-bit integer value from the stream message, interpreted as an * <code>int</code>//from w w w. java2 s. c om * @throws JMSException * if the JMS provider fails to read the message due to some * internal error. * @throws MessageEOFException * if unexpected end of message stream has been reached. * @throws MessageFormatException * if this type conversion is invalid. * @throws MessageNotReadableException * if the message is in write-only mode. */ public int readInt() throws JMSException { initializeReading(); try { this.dataIn.mark(33); int type = this.dataIn.read(); if (type == -1) { throw new MessageEOFException("reached end of data"); } if (type == MarshallingSupport.INTEGER_TYPE) { return this.dataIn.readInt(); } if (type == MarshallingSupport.SHORT_TYPE) { return this.dataIn.readShort(); } if (type == MarshallingSupport.BYTE_TYPE) { return this.dataIn.readByte(); } if (type == MarshallingSupport.STRING_TYPE) { return Integer.valueOf(this.dataIn.readUTF()).intValue(); } if (type == MarshallingSupport.NULL) { this.dataIn.reset(); throw new NullPointerException("Cannot convert NULL value to int."); } else { this.dataIn.reset(); throw new MessageFormatException(" not an int type"); } } catch (NumberFormatException mfe) { try { this.dataIn.reset(); } catch (IOException ioe) { JMSException jmsEx = new MessageFormatException(ioe.getMessage()); jmsEx.setLinkedException(ioe); throw jmsEx; } throw mfe; } catch (EOFException e) { String exMessage = "Reached premature EOF: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } catch (IOException e) { String exMessage = "Could not read boolean: " + e.getMessage(); _log.error(exMessage, e); throw new JMSException(exMessage); } }