Search in sources :

Example 11 with MessageEOFException

use of javax.jms.MessageEOFException in project qpid-broker-j by apache.

the class MessageVerifier method verifyMessageTypeAndContent.

private static void verifyMessageTypeAndContent(final MessageDescription messageDescription, final Message message) throws VerificationException {
    final MessageDescription.MessageType messageType = messageDescription.getMessageType();
    Object expectedMessageContent = messageDescription.getContent();
    Serializable actualContent;
    Class<? extends Message> expectedMessageClass;
    try {
        switch(messageType) {
            case MESSAGE:
                expectedMessageClass = Message.class;
                actualContent = null;
                break;
            case BYTES_MESSAGE:
                expectedMessageClass = BytesMessage.class;
                final int bodyLength = (int) ((BytesMessage) message).getBodyLength();
                actualContent = new byte[bodyLength];
                ((BytesMessage) message).readBytes((byte[]) actualContent);
                break;
            case MAP_MESSAGE:
                expectedMessageClass = MapMessage.class;
                final HashMap<Object, Object> content = new HashMap<>();
                final MapMessage mapMessage = (MapMessage) message;
                for (Object name : Collections.list(mapMessage.getMapNames())) {
                    content.put(name, mapMessage.getObject((String) name));
                }
                actualContent = content;
                break;
            case OBJECT_MESSAGE:
                expectedMessageClass = ObjectMessage.class;
                actualContent = ((ObjectMessage) message).getObject();
                break;
            case STREAM_MESSAGE:
                expectedMessageClass = StreamMessage.class;
                actualContent = new ArrayList<>();
                try {
                    while (true) {
                        ((List) actualContent).add(((StreamMessage) message).readObject());
                    }
                } catch (MessageEOFException e) {
                // pass
                }
                break;
            case TEXT_MESSAGE:
                expectedMessageClass = TextMessage.class;
                actualContent = ((TextMessage) message).getText();
                break;
            default:
                throw new RuntimeException(String.format("unexpected message type '%s'", messageType));
        }
        verifyEquals("Unexpected message type", expectedMessageClass, message.getClass());
        verifyEquals("Unexpected message content", expectedMessageContent, actualContent);
    } catch (JMSException e) {
        throw new RuntimeException("Unexpected exception during message type and/or content verification", e);
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) MessageEOFException(javax.jms.MessageEOFException) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with MessageEOFException

use of javax.jms.MessageEOFException in project qpid-broker-j by apache.

the class StreamMessageTest method testStreamMessageEOF.

@Test
public void testStreamMessageEOF() throws Exception {
    Queue queue = createQueue(getTestName());
    Connection consumerConnection = getConnection();
    try {
        Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        MessageConsumer consumer = consumerSession.createConsumer(queue);
        Connection producerConnection = getConnection();
        try {
            Session producerSession = producerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            MessageProducer producer = producerSession.createProducer(queue);
            StreamMessage msg = producerSession.createStreamMessage();
            msg.writeByte((byte) 42);
            producer.send(msg);
            consumerConnection.start();
            Message receivedMessage = consumer.receive(getReceiveTimeout());
            assertTrue(receivedMessage instanceof StreamMessage);
            StreamMessage streamMessage = (StreamMessage) receivedMessage;
            streamMessage.readByte();
            try {
                streamMessage.readByte();
                fail("Expected exception not thrown");
            } catch (Exception e) {
                assertTrue("Expected MessageEOFException: " + e, e instanceof MessageEOFException);
            }
            try {
                streamMessage.writeByte((byte) 42);
                fail("Expected exception not thrown");
            } catch (MessageNotWriteableException e) {
            // pass
            }
        } finally {
            producerConnection.close();
        }
    } finally {
        consumerConnection.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) StreamMessage(javax.jms.StreamMessage) Message(javax.jms.Message) MessageEOFException(javax.jms.MessageEOFException) Connection(javax.jms.Connection) StreamMessage(javax.jms.StreamMessage) MessageProducer(javax.jms.MessageProducer) MessageNotWriteableException(javax.jms.MessageNotWriteableException) Queue(javax.jms.Queue) MessageEOFException(javax.jms.MessageEOFException) MessageNotWriteableException(javax.jms.MessageNotWriteableException) Session(javax.jms.Session) Test(org.junit.Test)

Example 13 with MessageEOFException

use of javax.jms.MessageEOFException in project rabbitmq-jms-client by rabbitmq.

the class RMQStreamMessage method readPrimitiveType.

private Object readPrimitiveType(Class<?> type) throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (this.readbuf != null) {
        throw new MessageFormatException("You must call 'int readBytes(byte[])' since the buffer is not empty");
    }
    boolean success = true;
    try {
        this.bin.mark(0);
        Object o = RMQMessage.readPrimitive(in);
        if (o instanceof byte[]) {
            if (type == ByteArray.class || type == Object.class) {
                return o;
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte[]"));
            }
        } else if (type == ByteArray.class) {
            if (o == null) {
                return null;
            }
            throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte[]"));
        } else if (type == Boolean.class) {
            if (o == null) {
                return Boolean.FALSE;
            } else if (o instanceof Boolean) {
                return o;
            } else if (o instanceof String) {
                return Boolean.parseBoolean((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "boolean"));
            }
        } else if (type == Byte.class) {
            if (o instanceof Byte) {
                return o;
            } else if (o instanceof String) {
                return Byte.parseByte((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte"));
            }
        } else if (type == Short.class) {
            if (o instanceof Byte) {
                return (short) (Byte) o;
            } else if (o instanceof Short) {
                return o;
            } else if (o instanceof String) {
                return Short.parseShort((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "byte"));
            }
        } else if (type == Integer.class) {
            if (o instanceof Byte) {
                return (int) (Byte) o;
            } else if (o instanceof Short) {
                return (int) (Short) o;
            } else if (o instanceof Integer) {
                return o;
            } else if (o instanceof String) {
                return Integer.parseInt((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "int"));
            }
        } else if (type == Character.class) {
            if (o instanceof Character) {
                return o;
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "char"));
            }
        } else if (type == Long.class) {
            if (o instanceof Byte) {
                return (long) (Byte) o;
            } else if (o instanceof Short) {
                return (long) (Short) o;
            } else if (o instanceof Integer) {
                return (long) (Integer) o;
            } else if (o instanceof Long) {
                return o;
            } else if (o instanceof String) {
                return Long.parseLong((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "long"));
            }
        } else if (type == Float.class) {
            if (o instanceof Float) {
                return (Float) o;
            } else if (o instanceof String) {
                return Float.parseFloat((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "float"));
            }
        } else if (type == Double.class) {
            if (o instanceof Float) {
                return (double) (Float) o;
            } else if (o instanceof Double) {
                return (Double) o;
            } else if (o instanceof String) {
                return Double.parseDouble((String) o);
            } else {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "double"));
            }
        } else if (type == String.class) {
            if (o == null) {
                return null;
            } else if (o instanceof byte[]) {
                throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, "String"));
            } else {
                return o.toString();
            }
        } else if (type == Object.class) {
            return o;
        } else {
            throw new MessageFormatException(String.format(UNABLE_TO_CAST, o, type.toString()));
        }
    } catch (NumberFormatException x) {
        success = false;
        throw x;
    } catch (ClassNotFoundException x) {
        success = false;
        throw new RMQJMSException(x);
    } catch (EOFException x) {
        success = false;
        throw new MessageEOFException(MSG_EOF);
    } catch (UTFDataFormatException x) {
        success = false;
        throw new RMQMessageFormatException(x);
    } catch (IOException x) {
        success = false;
        throw new RMQJMSException(x);
    } catch (Exception x) {
        success = false;
        if (x instanceof JMSException) {
            throw (JMSException) x;
        } else {
            throw new RMQJMSException(x);
        }
    } finally {
        if (!success) {
            this.bin.reset();
        }
    }
}
Also used : MessageEOFException(javax.jms.MessageEOFException) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) RMQMessageFormatException(com.rabbitmq.jms.util.RMQMessageFormatException) MessageEOFException(javax.jms.MessageEOFException) EOFException(java.io.EOFException) RMQMessageFormatException(com.rabbitmq.jms.util.RMQMessageFormatException) MessageFormatException(javax.jms.MessageFormatException) MessageNotReadableException(javax.jms.MessageNotReadableException) IOException(java.io.IOException) RMQMessageFormatException(com.rabbitmq.jms.util.RMQMessageFormatException) IOException(java.io.IOException) MessageEOFException(javax.jms.MessageEOFException) EOFException(java.io.EOFException) JMSException(javax.jms.JMSException) MessageNotWriteableException(javax.jms.MessageNotWriteableException) MessageFormatException(javax.jms.MessageFormatException) MessageNotReadableException(javax.jms.MessageNotReadableException) UTFDataFormatException(java.io.UTFDataFormatException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) UTFDataFormatException(java.io.UTFDataFormatException)

Example 14 with MessageEOFException

use of javax.jms.MessageEOFException in project rabbitmq-jms-client by rabbitmq.

the class RMQBytesMessage method recreate.

public static RMQMessage recreate(BytesMessage msg) throws JMSException {
    msg.reset();
    long bodyLength = msg.getBodyLength();
    int bodySize = (int) Math.min(Math.max(0L, bodyLength), Integer.MAX_VALUE);
    if (bodyLength != bodySize)
        throw new JMSException(String.format("BytesMessage body invalid length (%s). Negative or too large.", bodyLength));
    try {
        RMQBytesMessage rmqBMsg = new RMQBytesMessage();
        RMQMessage.copyAttributes(rmqBMsg, msg);
        byte[] byteArray = new byte[bodySize];
        if (bodyLength > 0) {
            if (// must read the whole body at once
            bodySize != msg.readBytes(byteArray, bodySize))
                throw new MessageEOFException("Cannot read all of non-RMQ Message body.");
        }
        rmqBMsg.writeBytes(byteArray);
        // make body read-only and set pointer to start.
        rmqBMsg.reset();
        return rmqBMsg;
    } catch (OutOfMemoryError e) {
        throw new RMQJMSException("Body too large for conversion to RMQMessage.", e);
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) MessageEOFException(javax.jms.MessageEOFException) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException)

Example 15 with MessageEOFException

use of javax.jms.MessageEOFException in project rabbitmq-jms-client by rabbitmq.

the class RMQBytesMessage method readFloat.

/**
 * {@inheritDoc}
 */
@Override
public float readFloat() throws JMSException {
    if (!this.reading)
        throw new MessageNotReadableException(NOT_READABLE);
    if (this.pos + Bits.NUM_BYTES_IN_FLOAT > this.buf.length)
        throw new MessageEOFException(MSG_EOF);
    float flt = Bits.getFloat(this.buf, this.pos);
    this.pos += Bits.NUM_BYTES_IN_FLOAT;
    return flt;
}
Also used : MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Aggregations

MessageEOFException (javax.jms.MessageEOFException)35 MessageNotReadableException (javax.jms.MessageNotReadableException)21 MessageFormatException (javax.jms.MessageFormatException)16 MessageConsumer (javax.jms.MessageConsumer)7 MessageProducer (javax.jms.MessageProducer)7 Session (javax.jms.Session)6 StreamMessage (javax.jms.StreamMessage)6 Test (org.junit.Test)6 BytesMessage (javax.jms.BytesMessage)5 JMSException (javax.jms.JMSException)4 MessageNotWriteableException (javax.jms.MessageNotWriteableException)4 ArrayList (java.util.ArrayList)3 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)2 EOFException (java.io.EOFException)2 HashMap (java.util.HashMap)2 Connection (javax.jms.Connection)2 MapMessage (javax.jms.MapMessage)2 Message (javax.jms.Message)2 Queue (javax.jms.Queue)2 StreamMessageUtil.streamReadInteger (org.apache.activemq.artemis.reader.StreamMessageUtil.streamReadInteger)2