Search in sources :

Example 1 with MessageNotWriteableException

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

the class ObjectMessageTest method testClearBodyAndProperties.

@Test
public void testClearBodyAndProperties() throws Exception {
    Queue queue = createQueue(getTestName());
    Connection connection = getConnection();
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        A object = new A(1, "test");
        ObjectMessage msg = session.createObjectMessage(object);
        msg.setStringProperty("testProperty", "testValue");
        MessageProducer producer = session.createProducer(queue);
        producer.send(msg);
        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();
        Message receivedMessage = consumer.receive(getReceiveTimeout());
        assertTrue("ObjectMessage should be received", receivedMessage instanceof ObjectMessage);
        ObjectMessage objectMessage = (ObjectMessage) receivedMessage;
        Object received = objectMessage.getObject();
        assertTrue("Unexpected object type received", received instanceof A);
        assertEquals("Unexpected object received", object, received);
        assertEquals("Unexpected property value", "testValue", receivedMessage.getStringProperty("testProperty"));
        try {
            objectMessage.setObject("Test text");
            fail("Message should not be writable");
        } catch (MessageNotWriteableException e) {
        // pass
        }
        objectMessage.clearBody();
        try {
            objectMessage.setObject("Test text");
        } catch (MessageNotWriteableException e) {
            fail("Message should be writable");
        }
        try {
            objectMessage.setStringProperty("test", "test");
            fail("Message should not be writable");
        } catch (MessageNotWriteableException mnwe) {
        // pass
        }
        objectMessage.clearProperties();
        try {
            objectMessage.setStringProperty("test", "test");
        } catch (MessageNotWriteableException mnwe) {
            fail("Message should be writable");
        }
    } finally {
        connection.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) ObjectMessage(javax.jms.ObjectMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) MessageNotWriteableException(javax.jms.MessageNotWriteableException) Queue(javax.jms.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 2 with MessageNotWriteableException

use of javax.jms.MessageNotWriteableException 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 3 with MessageNotWriteableException

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

the class RMQObjectMessage method setObject.

@Override
public void setObject(Serializable object) throws JMSException {
    if (isReadonlyBody())
        throw new MessageNotWriteableException("Message not writeable");
    try {
        if (object == null) {
            buf = null;
        } else {
            /*
                 * We have to serialise the object now
                 */
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bout);
            out.writeObject(object);
            out.flush();
            buf = bout.toByteArray();
        }
    } catch (IOException x) {
        throw new RMQJMSException(x);
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MessageNotWriteableException(javax.jms.MessageNotWriteableException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 4 with MessageNotWriteableException

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

the class BrokerTestSupport method createMessage.

protected Message createMessage(ProducerInfo producerInfo, ActiveMQDestination destination) {
    ActiveMQTextMessage message = new ActiveMQTextMessage();
    message.setMessageId(new MessageId(producerInfo, ++msgIdGenerator));
    message.setDestination(destination);
    message.setPersistent(false);
    try {
        message.setText("Test Message Payload.");
    } catch (MessageNotWriteableException e) {
    }
    return message;
}
Also used : MessageNotWriteableException(javax.jms.MessageNotWriteableException) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) MessageId(org.apache.activemq.command.MessageId)

Example 5 with MessageNotWriteableException

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

the class BrokerNetworkWithStuckMessagesTest method createMessage.

protected Message createMessage(ProducerInfo producerInfo, ActiveMQDestination destination) {
    ActiveMQTextMessage message = new ActiveMQTextMessage();
    message.setMessageId(new MessageId(producerInfo, ++msgIdGenerator));
    message.setDestination(destination);
    message.setPersistent(false);
    try {
        message.setText("Test Message Payload.");
    } catch (MessageNotWriteableException e) {
    }
    return message;
}
Also used : MessageNotWriteableException(javax.jms.MessageNotWriteableException) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) MessageId(org.apache.activemq.command.MessageId)

Aggregations

MessageNotWriteableException (javax.jms.MessageNotWriteableException)14 Test (org.junit.Test)8 ActiveMQTextMessage (org.apache.activemq.command.ActiveMQTextMessage)5 Message (javax.jms.Message)4 MessageId (org.apache.activemq.command.MessageId)4 MessageConsumer (javax.jms.MessageConsumer)3 MessageProducer (javax.jms.MessageProducer)3 ObjectMessage (javax.jms.ObjectMessage)3 Session (javax.jms.Session)3 TextMessage (javax.jms.TextMessage)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 BytesMessage (javax.jms.BytesMessage)2 Connection (javax.jms.Connection)2 MapMessage (javax.jms.MapMessage)2 MessageFormatException (javax.jms.MessageFormatException)2 Queue (javax.jms.Queue)2 StreamMessage (javax.jms.StreamMessage)2 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1