Search in sources :

Example 6 with MessageNotWriteableException

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

the class JMSLargeMessageTest method testWaitOnOutputStream.

@Test
public void testWaitOnOutputStream() throws Exception {
    int msgSize = 1024 * 1024;
    conn = cf.createConnection();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer prod = session.createProducer(queue1);
    BytesMessage m = session.createBytesMessage();
    m.setObjectProperty("JMS_AMQ_InputStream", ActiveMQTestBase.createFakeLargeStream(msgSize));
    prod.send(m);
    conn.close();
    conn = cf.createConnection();
    session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer cons = session.createConsumer(queue1);
    conn.start();
    BytesMessage rm = (BytesMessage) cons.receive(10000);
    Assert.assertNotNull(rm);
    final AtomicLong numberOfBytes = new AtomicLong(0);
    final AtomicInteger numberOfErrors = new AtomicInteger(0);
    OutputStream out = new OutputStream() {

        int position = 0;

        @Override
        public void write(final int b) throws IOException {
            numberOfBytes.incrementAndGet();
            if (ActiveMQTestBase.getSamplebyte(position++) != b) {
                System.out.println("Wrong byte at position " + position);
                numberOfErrors.incrementAndGet();
            }
        }
    };
    try {
        rm.setObjectProperty("JMS_AMQ_InputStream", ActiveMQTestBase.createFakeLargeStream(100));
        Assert.fail("Exception expected!");
    } catch (MessageNotWriteableException expected) {
    }
    rm.setObjectProperty("JMS_AMQ_SaveStream", out);
    Assert.assertEquals(msgSize, numberOfBytes.get());
    Assert.assertEquals(0, numberOfErrors.get());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OutputStream(java.io.OutputStream) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) MessageNotWriteableException(javax.jms.MessageNotWriteableException) Session(javax.jms.Session) Test(org.junit.Test)

Example 7 with MessageNotWriteableException

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

the class MessageBodyTest method testWriteOnReceivedBody.

/**
 * Test that a call to the <code>TextMessage.setText()</code> method on a
 * received message raises a <code>javax.jms.MessageNotWriteableException</code>.
 */
@Test
public void testWriteOnReceivedBody() {
    try {
        TextMessage message = senderSession.createTextMessage();
        message.setText("foo");
        sender.send(message);
        Message m = receiver.receive(TestConfig.TIMEOUT);
        Assert.assertTrue("The message should be an instance of TextMessage.\n", m instanceof TextMessage);
        TextMessage msg = (TextMessage) m;
        msg.setText("bar");
        Assert.fail("should raise a MessageNotWriteableException (sec. 3.11.2)");
    } catch (MessageNotWriteableException e) {
    } catch (JMSException e) {
        fail(e);
    }
}
Also used : TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) MessageNotWriteableException(javax.jms.MessageNotWriteableException) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 8 with MessageNotWriteableException

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

the class FailoverTransportBrokerTest 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 9 with MessageNotWriteableException

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

the class MessageBodyTest method testMapMessage.

@Test
public void testMapMessage() throws Exception {
    MapMessage m1 = queueProducerSession.createMapMessage();
    // Some arbitrary values
    boolean myBool = true;
    byte myByte = 13;
    short myShort = 15321;
    int myInt = 0x71ab6c80;
    long myLong = 0x20bf1e3fb6fa31dfL;
    float myFloat = Float.MAX_VALUE - 23465;
    double myDouble = Double.MAX_VALUE - 72387633;
    String myString = "abcdef&^*&!^ghijkl\uD5E2\uCAC7\uD2BB\uB7DD\uB7C7\uB3A3\uBCE4\uB5A5";
    m1.setBoolean("myBool", myBool);
    m1.setByte("myByte", myByte);
    m1.setShort("myShort", myShort);
    m1.setInt("myInt", myInt);
    m1.setLong("myLong", myLong);
    m1.setFloat("myFloat", myFloat);
    m1.setDouble("myDouble", myDouble);
    m1.setString("myString", myString);
    m1.setObject("myBool", new Boolean(myBool));
    m1.setObject("myByte", new Byte(myByte));
    m1.setObject("myShort", new Short(myShort));
    m1.setObject("myInt", new Integer(myInt));
    m1.setObject("myLong", new Long(myLong));
    m1.setObject("myFloat", new Float(myFloat));
    m1.setObject("myDouble", new Double(myDouble));
    m1.setObject("myString", myString);
    try {
        m1.setObject("myIllegal", new Object());
        ProxyAssertSupport.fail();
    } catch (javax.jms.MessageFormatException e) {
    }
    queueProducer.send(m1);
    MapMessage m2 = (MapMessage) queueConsumer.receive(2000);
    ProxyAssertSupport.assertNotNull(m2);
    ProxyAssertSupport.assertEquals(myBool, m2.getBoolean("myBool"));
    ProxyAssertSupport.assertEquals(myByte, m2.getByte("myByte"));
    ProxyAssertSupport.assertEquals(myShort, m2.getShort("myShort"));
    ProxyAssertSupport.assertEquals(myInt, m2.getInt("myInt"));
    ProxyAssertSupport.assertEquals(myLong, m2.getLong("myLong"));
    ProxyAssertSupport.assertEquals(myFloat, m2.getFloat("myFloat"), 0);
    ProxyAssertSupport.assertEquals(myDouble, m2.getDouble("myDouble"), 0);
    ProxyAssertSupport.assertEquals(myString, m2.getString("myString"));
    // Properties should now be read-only
    try {
        m2.setBoolean("myBool", myBool);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    try {
        m2.setByte("myByte", myByte);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    try {
        m2.setShort("myShort", myShort);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    try {
        m2.setInt("myInt", myInt);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    try {
        m2.setLong("myLong", myLong);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    try {
        m2.setFloat("myFloat", myFloat);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    try {
        m2.setDouble("myDouble", myDouble);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    try {
        m2.setString("myString", myString);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    ProxyAssertSupport.assertTrue(m2.itemExists("myBool"));
    ProxyAssertSupport.assertTrue(m2.itemExists("myByte"));
    ProxyAssertSupport.assertTrue(m2.itemExists("myShort"));
    ProxyAssertSupport.assertTrue(m2.itemExists("myInt"));
    ProxyAssertSupport.assertTrue(m2.itemExists("myLong"));
    ProxyAssertSupport.assertTrue(m2.itemExists("myFloat"));
    ProxyAssertSupport.assertTrue(m2.itemExists("myDouble"));
    ProxyAssertSupport.assertTrue(m2.itemExists("myString"));
    ProxyAssertSupport.assertFalse(m2.itemExists("sausages"));
    HashSet<String> itemNames = new HashSet<>();
    Enumeration<String> en = m2.getMapNames();
    while (en.hasMoreElements()) {
        String propName = en.nextElement();
        itemNames.add(propName);
    }
    ProxyAssertSupport.assertEquals(8, itemNames.size());
    ProxyAssertSupport.assertTrue(itemNames.contains("myBool"));
    ProxyAssertSupport.assertTrue(itemNames.contains("myByte"));
    ProxyAssertSupport.assertTrue(itemNames.contains("myShort"));
    ProxyAssertSupport.assertTrue(itemNames.contains("myInt"));
    ProxyAssertSupport.assertTrue(itemNames.contains("myLong"));
    ProxyAssertSupport.assertTrue(itemNames.contains("myFloat"));
    ProxyAssertSupport.assertTrue(itemNames.contains("myDouble"));
    ProxyAssertSupport.assertTrue(itemNames.contains("myString"));
    // Check property conversions
    // Boolean property can be read as String but not anything else
    ProxyAssertSupport.assertEquals(String.valueOf(myBool), m2.getString("myBool"));
    try {
        m2.getByte("myBool");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getShort("myBool");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getInt("myBool");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getLong("myBool");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getFloat("myBool");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getDouble("myBool");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    // byte item can be read as short, int, long or String
    ProxyAssertSupport.assertEquals(myByte, m2.getShort("myByte"));
    ProxyAssertSupport.assertEquals(myByte, m2.getInt("myByte"));
    ProxyAssertSupport.assertEquals(myByte, m2.getLong("myByte"));
    ProxyAssertSupport.assertEquals(String.valueOf(myByte), m2.getString("myByte"));
    try {
        m2.getBoolean("myByte");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getFloat("myByte");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getDouble("myByte");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    // short item can be read as int, long or String
    ProxyAssertSupport.assertEquals(myShort, m2.getInt("myShort"));
    ProxyAssertSupport.assertEquals(myShort, m2.getLong("myShort"));
    ProxyAssertSupport.assertEquals(String.valueOf(myShort), m2.getString("myShort"));
    try {
        m2.getByte("myShort");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getBoolean("myShort");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getFloat("myShort");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getDouble("myShort");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    // int item can be read as long or String
    ProxyAssertSupport.assertEquals(myInt, m2.getLong("myInt"));
    ProxyAssertSupport.assertEquals(String.valueOf(myInt), m2.getString("myInt"));
    try {
        m2.getShort("myInt");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getByte("myInt");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getBoolean("myInt");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getFloat("myInt");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getDouble("myInt");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    // long item can be read as String
    ProxyAssertSupport.assertEquals(String.valueOf(myLong), m2.getString("myLong"));
    try {
        m2.getInt("myLong");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getShort("myLong");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getByte("myLong");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getBoolean("myLong");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getFloat("myLong");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getDouble("myLong");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    // float can be read as double or String
    ProxyAssertSupport.assertEquals(String.valueOf(myFloat), m2.getString("myFloat"));
    ProxyAssertSupport.assertEquals(myFloat, m2.getDouble("myFloat"), 0);
    try {
        m2.getInt("myFloat");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getShort("myFloat");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getLong("myFloat");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getByte("myFloat");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getBoolean("myFloat");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    // double can be read as String
    ProxyAssertSupport.assertEquals(String.valueOf(myDouble), m2.getString("myDouble"));
    try {
        m2.getFloat("myDouble");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getInt("myDouble");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getShort("myDouble");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getByte("myDouble");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getBoolean("myDouble");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    try {
        m2.getFloat("myDouble");
        ProxyAssertSupport.fail();
    } catch (MessageFormatException e) {
    }
    m2.clearBody();
    ProxyAssertSupport.assertFalse(m2.getMapNames().hasMoreElements());
    // Test String -> Numeric and bool conversions
    MapMessage m3 = queueProducerSession.createMapMessage();
    m3.setString("myBool", String.valueOf(myBool));
    m3.setString("myByte", String.valueOf(myByte));
    m3.setString("myShort", String.valueOf(myShort));
    m3.setString("myInt", String.valueOf(myInt));
    m3.setString("myLong", String.valueOf(myLong));
    m3.setString("myFloat", String.valueOf(myFloat));
    m3.setString("myDouble", String.valueOf(myDouble));
    m3.setString("myIllegal", "xyz123");
    ProxyAssertSupport.assertEquals(myBool, m3.getBoolean("myBool"));
    ProxyAssertSupport.assertEquals(myByte, m3.getByte("myByte"));
    ProxyAssertSupport.assertEquals(myShort, m3.getShort("myShort"));
    ProxyAssertSupport.assertEquals(myInt, m3.getInt("myInt"));
    ProxyAssertSupport.assertEquals(myLong, m3.getLong("myLong"));
    ProxyAssertSupport.assertEquals(myFloat, m3.getFloat("myFloat"), 0);
    ProxyAssertSupport.assertEquals(myDouble, m3.getDouble("myDouble"), 0);
    m3.getBoolean("myIllegal");
    try {
        m3.getByte("myIllegal");
        ProxyAssertSupport.fail();
    } catch (NumberFormatException e) {
    }
    try {
        m3.getShort("myIllegal");
        ProxyAssertSupport.fail();
    } catch (NumberFormatException e) {
    }
    try {
        m3.getInt("myIllegal");
        ProxyAssertSupport.fail();
    } catch (NumberFormatException e) {
    }
    try {
        m3.getLong("myIllegal");
        ProxyAssertSupport.fail();
    } catch (NumberFormatException e) {
    }
    try {
        m3.getFloat("myIllegal");
        ProxyAssertSupport.fail();
    } catch (NumberFormatException e) {
    }
    try {
        m3.getDouble("myIllegal");
        ProxyAssertSupport.fail();
    } catch (NumberFormatException e) {
    }
}
Also used : MapMessage(javax.jms.MapMessage) HashSet(java.util.HashSet) MessageFormatException(javax.jms.MessageFormatException) MessageNotWriteableException(javax.jms.MessageNotWriteableException) MessageFormatException(javax.jms.MessageFormatException) Test(org.junit.Test)

Example 10 with MessageNotWriteableException

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

the class MessageBodyTest method testObjectMessage.

@Test
public void testObjectMessage() throws Exception {
    TestSerializable obj = new TestSerializable();
    obj.str = "abcdefg";
    ObjectMessage m1 = queueProducerSession.createObjectMessage(obj);
    queueProducer.send(m1);
    ObjectMessage m2 = (ObjectMessage) queueConsumer.receive(2000);
    ProxyAssertSupport.assertNotNull(m2);
    TestSerializable obj2 = (TestSerializable) m2.getObject();
    ProxyAssertSupport.assertEquals(obj.str, obj2.str);
    ObjectMessage m3 = queueProducerSession.createObjectMessage();
    m3.setObject(obj);
    queueProducer.send(m3);
    obj.str = "xyz123";
    ObjectMessage m4 = (ObjectMessage) queueConsumer.receive(2000);
    ProxyAssertSupport.assertNotNull(m4);
    TestSerializable obj3 = (TestSerializable) m4.getObject();
    ProxyAssertSupport.assertEquals("abcdefg", obj3.str);
    try {
        m4.setObject(obj);
        ProxyAssertSupport.fail();
    } catch (MessageNotWriteableException e) {
    }
    m4.clearBody();
    m4.setObject(obj);
    TestSerializable obj4 = (TestSerializable) m4.getObject();
    ProxyAssertSupport.assertNotNull(obj4);
}
Also used : ObjectMessage(javax.jms.ObjectMessage) MessageNotWriteableException(javax.jms.MessageNotWriteableException) Test(org.junit.Test)

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