Search in sources :

Example 6 with MessageEOFException

use of javax.jms.MessageEOFException in project activemq-artemis by apache.

the class SimpleJMSStreamMessage method readDouble.

@Override
public double readDouble() throws JMSException {
    if (bodyWriteOnly) {
        throw new MessageNotReadableException("The message body is writeonly");
    }
    try {
        Object value = content.get(position);
        offset = 0;
        if (value == null) {
            throw new NullPointerException("Value is null");
        } else if (value instanceof Float) {
            position++;
            return ((Float) value).doubleValue();
        } else if (value instanceof Double) {
            position++;
            return ((Double) value).doubleValue();
        } else if (value instanceof String) {
            double result = Double.parseDouble((String) value);
            position++;
            return result;
        } else {
            throw new MessageFormatException("Invalid conversion");
        }
    } catch (IndexOutOfBoundsException e) {
        throw new MessageEOFException("");
    }
}
Also used : MessageFormatException(javax.jms.MessageFormatException) MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 7 with MessageEOFException

use of javax.jms.MessageEOFException in project activemq-artemis by apache.

the class SimpleJMSStreamMessage method readBoolean.

// Public --------------------------------------------------------
// StreamMessage implementation ----------------------------------
@Override
public boolean readBoolean() throws JMSException {
    if (bodyWriteOnly) {
        throw new MessageNotReadableException("The message body is writeonly");
    }
    try {
        Object value = content.get(position);
        offset = 0;
        if (value == null) {
            throw new NullPointerException("Value is null");
        } else if (value instanceof Boolean) {
            position++;
            return ((Boolean) value).booleanValue();
        } else if (value instanceof String) {
            boolean result = Boolean.valueOf((String) value).booleanValue();
            position++;
            return result;
        } else {
            throw new MessageFormatException("Invalid conversion");
        }
    } catch (IndexOutOfBoundsException e) {
        throw new MessageEOFException("");
    }
}
Also used : MessageFormatException(javax.jms.MessageFormatException) MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 8 with MessageEOFException

use of javax.jms.MessageEOFException in project activemq-artemis by apache.

the class SimpleJMSStreamMessage method readInt.

@Override
public int readInt() throws JMSException {
    if (bodyWriteOnly) {
        throw new MessageNotReadableException("The message body is writeonly");
    }
    try {
        Object value = content.get(position);
        offset = 0;
        if (value == null) {
            throw new NullPointerException("Value is null");
        } else if (value instanceof Byte) {
            position++;
            return ((Byte) value).intValue();
        } else if (value instanceof Short) {
            position++;
            return ((Short) value).intValue();
        } else if (value instanceof Integer) {
            position++;
            return ((Integer) value).intValue();
        } else if (value instanceof String) {
            int result = Integer.parseInt((String) value);
            position++;
            return result;
        } else {
            throw new MessageFormatException("Invalid conversion");
        }
    } catch (IndexOutOfBoundsException e) {
        throw new MessageEOFException("");
    }
}
Also used : MessageFormatException(javax.jms.MessageFormatException) MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 9 with MessageEOFException

use of javax.jms.MessageEOFException in project activemq-artemis by apache.

the class SimpleJMSStreamMessage method readString.

@Override
public String readString() throws JMSException {
    if (bodyWriteOnly) {
        throw new MessageNotReadableException("The message body is writeonly");
    }
    try {
        Object value = content.get(position);
        offset = 0;
        if (value == null) {
            position++;
            return null;
        } else if (value instanceof Boolean) {
            position++;
            return ((Boolean) value).toString();
        } else if (value instanceof Byte) {
            position++;
            return ((Byte) value).toString();
        } else if (value instanceof Short) {
            position++;
            return ((Short) value).toString();
        } else if (value instanceof Character) {
            position++;
            return ((Character) value).toString();
        } else if (value instanceof Integer) {
            position++;
            return ((Integer) value).toString();
        } else if (value instanceof Long) {
            position++;
            return ((Long) value).toString();
        } else if (value instanceof Float) {
            position++;
            return ((Float) value).toString();
        } else if (value instanceof Double) {
            position++;
            return ((Double) value).toString();
        } else if (value instanceof String) {
            position++;
            return (String) value;
        } else {
            throw new MessageFormatException("Invalid conversion");
        }
    } catch (IndexOutOfBoundsException e) {
        throw new MessageEOFException("");
    }
}
Also used : MessageFormatException(javax.jms.MessageFormatException) MessageEOFException(javax.jms.MessageEOFException) MessageNotReadableException(javax.jms.MessageNotReadableException)

Example 10 with MessageEOFException

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

the class AmqpManagementFacade method performOperationUsingAmqpManagement.

public Object performOperationUsingAmqpManagement(final String name, final String operation, final Session session, final String type, Map<String, Object> arguments) throws JMSException {
    Destination replyToDestination;
    Destination replyConsumerDestination;
    if (_protocol == Protocol.AMQP_1_0) {
        replyToDestination = session.createTemporaryQueue();
        replyConsumerDestination = replyToDestination;
    } else {
        replyToDestination = session.createQueue(AMQP_0_X_REPLY_TO_DESTINATION);
        replyConsumerDestination = session.createQueue(AMQP_0_X_CONSUMER_REPLY_DESTINATION);
    }
    MessageConsumer consumer = session.createConsumer(replyConsumerDestination);
    MessageProducer producer = session.createProducer(session.createQueue(_managementAddress));
    MapMessage opMessage = session.createMapMessage();
    opMessage.setStringProperty("type", type);
    opMessage.setStringProperty("operation", operation);
    opMessage.setStringProperty("index", "object-path");
    opMessage.setJMSReplyTo(replyToDestination);
    opMessage.setStringProperty("key", name);
    for (Map.Entry<String, Object> argument : arguments.entrySet()) {
        Object value = argument.getValue();
        if (value.getClass().isPrimitive() || value instanceof String) {
            opMessage.setObjectProperty(argument.getKey(), value);
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonifiedValue;
            try {
                jsonifiedValue = objectMapper.writeValueAsString(value);
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException(String.format("Cannot convert the argument '%s' to JSON to meet JMS type restrictions", argument.getKey()));
            }
            opMessage.setObjectProperty(argument.getKey(), jsonifiedValue);
        }
    }
    producer.send(opMessage);
    if (session.getTransacted()) {
        session.commit();
    }
    Message response = consumer.receive(5000);
    try {
        int statusCode = response.getIntProperty("statusCode");
        if (statusCode < 200 || statusCode > 299) {
            throw new OperationUnsuccessfulException(response.getStringProperty("statusDescription"), statusCode);
        }
        if (response instanceof StreamMessage) {
            StreamMessage bodyStream = (StreamMessage) response;
            List<Object> result = new ArrayList<>();
            boolean done = false;
            do {
                try {
                    result.add(bodyStream.readObject());
                } catch (MessageEOFException mfe) {
                    // Expected - end of stream
                    done = true;
                }
            } while (!done);
            return result;
        } else if (response instanceof MapMessage) {
            MapMessage bodyMap = (MapMessage) response;
            Map<String, Object> result = new TreeMap<>();
            Enumeration mapNames = bodyMap.getMapNames();
            while (mapNames.hasMoreElements()) {
                String key = (String) mapNames.nextElement();
                result.put(key, bodyMap.getObject(key));
            }
            return result;
        } else if (response instanceof ObjectMessage) {
            return ((ObjectMessage) response).getObject();
        } else if (response instanceof BytesMessage) {
            BytesMessage bytesMessage = (BytesMessage) response;
            if (bytesMessage.getBodyLength() == 0) {
                return null;
            } else {
                byte[] buf = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(buf);
                return buf;
            }
        }
        throw new IllegalArgumentException("Cannot parse the results from a management operation.  JMS response message : " + response);
    } finally {
        if (session.getTransacted()) {
            session.commit();
        }
        consumer.close();
        if (_protocol == Protocol.AMQP_1_0) {
            ((TemporaryQueue) replyToDestination).delete();
        }
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) StreamMessage(javax.jms.StreamMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageEOFException(javax.jms.MessageEOFException) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) BytesMessage(javax.jms.BytesMessage) ObjectMessage(javax.jms.ObjectMessage) TemporaryQueue(javax.jms.TemporaryQueue) StreamMessage(javax.jms.StreamMessage) MessageProducer(javax.jms.MessageProducer) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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