javax.jms.MessageNotReadableException Java Examples

The following examples show how to use javax.jms.MessageNotReadableException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SQSBytesMessageTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Test clear body
 */
@Test
public void testClearBody() throws JMSException, IOException {

    SQSBytesMessage msg = new SQSBytesMessage();

    byte[] byteArray = new byte[] { 1, 0, 'a', 65 };
    msg.writeBytes(byteArray);

    msg.clearBody();

    byte[] readByteArray = new byte[4];

    /*
     * Verify message is in write-only mode
     */
    try {
        msg.readBytes(readByteArray);
    } catch(MessageNotReadableException exception) {
        assertEquals("Message is not readable", exception.getMessage());
    }

    msg.writeBytes(byteArray);
}
 
Example #2
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public byte readByte() 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).byteValue();
      } else if (value instanceof String) {
         byte result = Byte.parseByte((String) value);
         position++;
         return result;
      } else {
         throw new MessageFormatException("Invalid conversion");
      }
   } catch (IndexOutOfBoundsException e) {
      throw new MessageEOFException("");
   }
}
 
Example #3
Source File: PriorityMessageQueueTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnreadablePrioirtyIsStillEnqueued() throws JMSException {
    JmsInboundMessageDispatch message = createEnvelopeWithMessageThatCannotReadPriority();
    queue.enqueue(createEnvelope(9));
    queue.enqueue(message);
    queue.enqueue(createEnvelope(1));

    JmsInboundMessageDispatch envelope = queue.dequeueNoWait();
    assertEquals(9, envelope.getMessage().getJMSPriority());

    envelope = queue.dequeueNoWait();
    try {
        envelope.getMessage().getJMSPriority();
        fail("Unreadable priority message should sit at default level");
    } catch (MessageNotReadableException mnre) {}
    envelope = queue.dequeueNoWait();
    assertEquals(1, envelope.getMessage().getJMSPriority());

    assertTrue(queue.isEmpty());
}
 
Example #4
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public char readChar() 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 Character) {
         position++;
         return ((Character) value).charValue();
      } else {
         throw new MessageFormatException("Invalid conversion");
      }
   } catch (IndexOutOfBoundsException e) {
      throw new MessageEOFException("");
   }
}
 
Example #5
Source File: JmsBytesMessageTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
/**
 * Test that attempting to call {@link BytesMessage#getBodyLength()} on a received message after calling
 * {@link BytesMessage#clearBody()} causes {@link MessageNotReadableException} to be thrown due to being write-only.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test
public void testGetBodyLengthOnClearedReceivedMessageThrowsMessageNotReadableException() throws Exception {
    byte[] content = "myBytesData".getBytes();
    JmsTestBytesMessageFacade facade = new JmsTestBytesMessageFacade(content);

    JmsBytesMessage bytesMessage = new JmsBytesMessage(facade);
    bytesMessage.onDispatch();
    assertEquals("Unexpected message length", content.length, bytesMessage.getBodyLength());
    bytesMessage.clearBody();

    try {
        bytesMessage.getBodyLength();
        fail("expected exception to be thrown");
    } catch (MessageNotReadableException mnre) {
        // expected
    }
}
 
Example #6
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public float readFloat() 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).floatValue();
      } else if (value instanceof String) {
         float result = Float.parseFloat((String) value);
         position++;
         return result;
      } else {
         throw new MessageFormatException("Invalid conversion");
      }
   } catch (IndexOutOfBoundsException e) {
      throw new MessageEOFException("");
   }
}
 
Example #7
Source File: JmsTextMessageTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadOnlyBody() throws JMSException {
    JmsTextMessage textMessage = factory.createTextMessage();
    textMessage.setText("test");
    textMessage.setReadOnlyBody(true);
    try {
        textMessage.getText();
    } catch (MessageNotReadableException e) {
        fail("should be readable");
    }
    try {
        textMessage.setText("test");
        fail("should throw exception");
    } catch (MessageNotWriteableException mnwe) {
    }
}
 
Example #8
Source File: JmsTextMessageTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testClearBody() throws JMSException, IOException {
    JmsTextMessage textMessage = factory.createTextMessage();
    textMessage.setText("string");
    textMessage.clearBody();
    assertFalse(textMessage.isReadOnlyBody());
    assertNull(textMessage.getText());
    try {
        textMessage.setText("String");
        textMessage.getText();
    } catch (MessageNotWriteableException mnwe) {
        fail("should be writeable");
    } catch (MessageNotReadableException mnre) {
        fail("should be readable");
    }
}
 
Example #9
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public Object readObject() throws JMSException {
   if (bodyWriteOnly) {
      throw new MessageNotReadableException("The message body is writeonly");
   }
   try {
      Object value = content.get(position);
      position++;
      offset = 0;

      return value;
   } catch (IndexOutOfBoundsException e) {
      throw new MessageEOFException("");
   }
}
 
Example #10
Source File: SQSBytesMessageTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test before reset the message is not readable
 */
@Test(expected = MessageNotReadableException.class)
public void testReadable() throws JMSException {
    when(mockSQSSession.createBytesMessage()).thenReturn(new SQSBytesMessage());
    SQSBytesMessage msg = (SQSBytesMessage) mockSQSSession.createBytesMessage(); 
    
    byte[] byteArray = new byte[] { 'a', 0, 34, 65 };
    msg.writeBytes(byteArray);
    
    msg.readInt();
}
 
Example #11
Source File: PriorityMessageQueueTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
private JmsInboundMessageDispatch createEnvelopeWithMessageThatCannotReadPriority() throws JMSException {
    JmsInboundMessageDispatch envelope = new JmsInboundMessageDispatch(sequence++);

    JmsMessage message = Mockito.mock(JmsMessage.class);
    Mockito.when(message.getJMSPriority()).thenThrow(new MessageNotReadableException("Message is not readable"));

    envelope.setMessage(message);
    return envelope;
}
 
Example #12
Source File: JmsBytesMessageTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * Test that attempting to read bytes from a new message (without calling {@link BytesMessage#reset()} first) causes a
 * {@link MessageNotReadableException} to be thrown due to being write-only.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test(expected = MessageNotReadableException.class)
public void testNewBytesMessageThrowsMessageNotReadableOnReadBytes() throws Exception {
    JmsBytesMessage bytesMessage = factory.createBytesMessage();
    byte[] receivedBytes = new byte[1];
    bytesMessage.readBytes(receivedBytes);
}
 
Example #13
Source File: JmsStreamMessageTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewMessageIsWriteOnlyThrowsMNRE() throws Exception {
    JmsStreamMessage streamMessage = factory.createStreamMessage();

    try {
        streamMessage.readBoolean();
        fail("Expected exception to be thrown as message is not readable");
    } catch (MessageNotReadableException mnre) {
        // expected
    }
}
 
Example #14
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@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("");
   }
}
 
Example #15
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public long readLong() 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).longValue();
      } else if (value instanceof Short) {
         position++;
         return ((Short) value).longValue();
      } else if (value instanceof Integer) {
         position++;
         return ((Integer) value).longValue();
      } else if (value instanceof Long) {
         position++;
         return ((Long) value).longValue();
      } else if (value instanceof String) {
         long result = Long.parseLong((String) value);
         position++;
         return result;
      } else {
         throw new MessageFormatException("Invalid conversion");
      }
   } catch (IndexOutOfBoundsException e) {
      throw new MessageEOFException("");
   }
}
 
Example #16
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@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("");
   }
}
 
Example #17
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public short readShort() 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).shortValue();
      } else if (value instanceof Short) {
         position++;
         return ((Short) value).shortValue();
      } else if (value instanceof String) {
         short result = Short.parseShort((String) value);
         position++;
         return result;
      } else {
         throw new MessageFormatException("Invalid conversion");
      }
   } catch (IndexOutOfBoundsException e) {
      throw new MessageEOFException("");
   }
}
 
Example #18
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@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("");
   }

}
 
Example #19
Source File: ActiveMQTextMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void testReadOnlyBody() throws JMSException {
   ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
   textMessage.setText("test");
   textMessage.setReadOnlyBody(true);
   try {
      textMessage.getText();
   } catch (MessageNotReadableException e) {
      fail("should be readable");
   }
   try {
      textMessage.setText("test");
      fail("should throw exception");
   } catch (MessageNotWriteableException mnwe) {
   }
}
 
Example #20
Source File: ActiveMQTextMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void testClearBody() throws JMSException, IOException {
   ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
   textMessage.setText("string");
   textMessage.clearBody();
   assertFalse(textMessage.isReadOnlyBody());
   assertNull(textMessage.getText());
   try {
      textMessage.setText("String");
      textMessage.getText();
   } catch (MessageNotWriteableException mnwe) {
      fail("should be writeable");
   } catch (MessageNotReadableException mnre) {
      fail("should be readable");
   }
}
 
Example #21
Source File: MockJMSMessage.java    From pooled-jms with Apache License 2.0 4 votes vote down vote up
protected void checkWriteOnlyBody() throws MessageNotReadableException {
    if (!readOnlyBody) {
        throw new MessageNotReadableException("Message body is write-only");
    }
}
 
Example #22
Source File: ActiveMQJMSClientBundle.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Message(id = 139014, value = "Message is write-only")
MessageNotReadableException messageNotReadable();
 
Example #23
Source File: JmsMessage.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
protected void checkWriteOnlyBody() throws MessageNotReadableException {
    if (!readOnlyBody) {
        throw new MessageNotReadableException("Message body is write-only");
    }
}
 
Example #24
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public int readBytes(final byte[] value) throws JMSException {
   if (bodyWriteOnly) {
      throw new MessageNotReadableException("The message body is writeonly");
   }
   try {
      Object myObj = content.get(position);
      if (myObj == null) {
         throw new NullPointerException("Value is null");
      } else if (!(myObj instanceof byte[])) {
         throw new MessageFormatException("Invalid conversion");
      }
      byte[] obj = (byte[]) myObj;

      if (obj.length == 0) {
         position++;
         offset = 0;
         return 0;
      }

      if (offset >= obj.length) {
         position++;
         offset = 0;
         return -1;
      }

      if (obj.length - offset < value.length) {
         System.arraycopy(obj, offset, value, 0, obj.length);

         position++;
         offset = 0;

         return obj.length - offset;
      } else {
         System.arraycopy(obj, offset, value, 0, value.length);
         offset += value.length;

         return value.length;
      }

   } catch (IndexOutOfBoundsException e) {
      throw new MessageEOFException("");
   }
}
 
Example #25
Source File: SimpleJMSStreamMessage.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@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("");
   }
}
 
Example #26
Source File: SQSBytesMessage.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 4 votes vote down vote up
void checkCanRead() throws JMSException {
    if (bytes == null) {
        throw new MessageNotReadableException("Message is not readable");
    }
}
 
Example #27
Source File: JmsBytesMessageTest.java    From qpid-jms with Apache License 2.0 2 votes vote down vote up
/**
 * Test that attempting to call {@link BytesMessage#getBodyLength()} on a new message causes
 * a {@link MessageNotReadableException} to be thrown due to being write-only.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test(expected = MessageNotReadableException.class)
public void testGetBodyLengthOnNewMessageThrowsMessageNotReadableException() throws Exception {
    JmsBytesMessage bytesMessage = factory.createBytesMessage();
    bytesMessage.getBodyLength();
}