Search in sources :

Example 1 with MessageFormatRuntimeException

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

the class ActiveMQJMSProducer method send.

@Override
public JMSProducer send(Destination destination, byte[] body) {
    BytesMessage message = context.createBytesMessage();
    if (body != null) {
        try {
            message.writeBytes(body);
        } catch (JMSException e) {
            throw new MessageFormatRuntimeException(e.getMessage());
        }
    }
    send(destination, message);
    return this;
}
Also used : BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException)

Example 2 with MessageFormatRuntimeException

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

the class ActiveMQJMSProducer method send.

@Override
public JMSProducer send(Destination destination, Map<String, Object> body) {
    MapMessage message = context.createMapMessage();
    if (body != null) {
        try {
            for (Entry<String, Object> entry : body.entrySet()) {
                final String name = entry.getKey();
                final Object v = entry.getValue();
                if (v instanceof String) {
                    message.setString(name, (String) v);
                } else if (v instanceof Long) {
                    message.setLong(name, (Long) v);
                } else if (v instanceof Double) {
                    message.setDouble(name, (Double) v);
                } else if (v instanceof Integer) {
                    message.setInt(name, (Integer) v);
                } else if (v instanceof Character) {
                    message.setChar(name, (Character) v);
                } else if (v instanceof Short) {
                    message.setShort(name, (Short) v);
                } else if (v instanceof Boolean) {
                    message.setBoolean(name, (Boolean) v);
                } else if (v instanceof Float) {
                    message.setFloat(name, (Float) v);
                } else if (v instanceof Byte) {
                    message.setByte(name, (Byte) v);
                } else if (v instanceof byte[]) {
                    byte[] array = (byte[]) v;
                    message.setBytes(name, array, 0, array.length);
                } else {
                    message.setObject(name, v);
                }
            }
        } catch (JMSException e) {
            throw new MessageFormatRuntimeException(e.getMessage());
        }
    }
    send(destination, message);
    return this;
}
Also used : MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException)

Example 3 with MessageFormatRuntimeException

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

the class ActiveMQJMSProducer method getPropertyNames.

@Override
public Set<String> getPropertyNames() {
    try {
        Set<SimpleString> simplePropNames = properties.getPropertyNames();
        Set<String> propNames = new HashSet<>(simplePropNames.size());
        for (SimpleString str : simplePropNames) {
            propNames.add(str.toString());
        }
        return propNames;
    } catch (ActiveMQPropertyConversionException ce) {
        throw new MessageFormatRuntimeException(ce.getMessage());
    } catch (RuntimeException e) {
        throw new JMSRuntimeException(e.getMessage());
    }
}
Also used : MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException) JMSRuntimeException(javax.jms.JMSRuntimeException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException) ActiveMQPropertyConversionException(org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException) HashSet(java.util.HashSet) JMSRuntimeException(javax.jms.JMSRuntimeException)

Example 4 with MessageFormatRuntimeException

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

the class JmsProducerTest method testSetters.

@Test
public void testSetters() {
    long v = random.nextLong();
    producer.setDeliveryDelay(v);
    Assert.assertEquals(v, producer.getDeliveryDelay());
    long l = random.nextLong();
    producer.setTimeToLive(l);
    Assert.assertEquals(l, producer.getTimeToLive());
    String id = "ID: jms2-tests-correlation-id" + random.nextLong();
    producer.setJMSCorrelationID(id);
    Assert.assertEquals(id, producer.getJMSCorrelationID());
    // set a property of an invalid type (ArrayList)
    try {
        producer.setProperty("name1", new ArrayList<String>(2));
        fail("didn't get expected MessageFormatRuntimeException");
    } catch (MessageFormatRuntimeException e) {
    // expected.
    }
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException) Test(org.junit.Test)

Example 5 with MessageFormatRuntimeException

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

the class MessagePropertyConversionTest method msgPropertyConversionTests.

@Test
public void msgPropertyConversionTests() throws Exception {
    JMSContext ctx = addContext(getConnectionFactory().createContext());
    JMSProducer producer = ctx.createProducer();
    boolean bool = true;
    byte bValue = 1;
    short nShort = 2;
    int nInt = 3;
    long nLong = 4;
    float nFloat = 5;
    double nDouble = 6;
    producer.setProperty("aboolean", bool);
    producer.setProperty("abyte", bValue);
    producer.setProperty("ashort", nShort);
    producer.setProperty("anint", nInt);
    producer.setProperty("afloat", nFloat);
    producer.setProperty("adouble", nDouble);
    producer.setProperty("astring", "test");
    producer.setProperty("along", nLong);
    producer.setProperty("true", "true");
    producer.setProperty("false", "false");
    producer.setProperty("anotherString", "1");
    String myBool = producer.getStringProperty("aboolean");
    if (Boolean.valueOf(myBool).booleanValue() != bool) {
        ProxyAssertSupport.fail("conversion from boolean to string failed");
    }
    try {
        producer.getByteProperty("aboolean");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("did not catch expected Exception -- boolean to byte");
    }
    try {
        producer.getShortProperty("aboolean");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getIntProperty("aboolean");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getLongProperty("aboolean");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getFloatProperty("aboolean");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    // invalid - boolean to double
    try {
        producer.getDoubleProperty("aboolean");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    String myByte = producer.getStringProperty("abyte");
    if (Byte.valueOf(myByte).byteValue() != bValue) {
        ProxyAssertSupport.fail("conversion from byte to string failed");
    }
    if (producer.getShortProperty("abyte") != bValue) {
        ProxyAssertSupport.fail("conversion from byte to short failed");
    }
    if (producer.getIntProperty("abyte") != bValue) {
        ProxyAssertSupport.fail("conversion from byte to int failed");
    }
    if (producer.getLongProperty("abyte") != bValue) {
        ProxyAssertSupport.fail("conversion from byte to long failed");
    }
    try {
        producer.getBooleanProperty("abyte");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getFloatProperty("abyte");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getDoubleProperty("abyte");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    String myshort = producer.getStringProperty("ashort");
    if (Short.valueOf(myshort).shortValue() != nShort) {
        ProxyAssertSupport.fail("conversion from short to string failed");
    }
    if (producer.getIntProperty("ashort") != nShort) {
        ProxyAssertSupport.fail("conversion from short to int failed");
    }
    if (producer.getLongProperty("ashort") != nShort) {
        ProxyAssertSupport.fail("conversion from short to long failed");
    }
    try {
        producer.getBooleanProperty("ashort");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getByteProperty("ashort");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getFloatProperty("ashort");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getDoubleProperty("ashort");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    if (Integer.valueOf(producer.getStringProperty("anint")).intValue() != nInt) {
        ProxyAssertSupport.fail("conversion from int to string failed");
    }
    if (producer.getLongProperty("anint") != nInt) {
        ProxyAssertSupport.fail("conversion from int to long failed");
    }
    try {
        producer.getBooleanProperty("anint");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getByteProperty("anint");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getShortProperty("anint");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getFloatProperty("anint");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getDoubleProperty("anint");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    if (Long.valueOf(producer.getStringProperty("along")).longValue() != nLong) {
        ProxyAssertSupport.fail("conversion from long to string failed");
    }
    try {
        producer.getBooleanProperty("along");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getByteProperty("along");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getShortProperty("along");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getIntProperty("along");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getFloatProperty("along");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getDoubleProperty("along");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    if (Float.valueOf(producer.getStringProperty("afloat")).floatValue() != nFloat) {
        ProxyAssertSupport.fail("conversion from float to string failed");
    }
    if (producer.getDoubleProperty("afloat") != nFloat) {
        ProxyAssertSupport.fail("conversion from long to double failed");
    }
    try {
        producer.getBooleanProperty("afloat");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getByteProperty("afloat");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getShortProperty("afloat");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getIntProperty("afloat");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getLongProperty("afloat");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    if (Double.valueOf(producer.getStringProperty("adouble")).doubleValue() != nDouble) {
        ProxyAssertSupport.fail("conversion from double to string failed");
    }
    try {
        producer.getBooleanProperty("adouble");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getByteProperty("adouble");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getShortProperty("adouble");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getIntProperty("adouble");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    try {
        producer.getLongProperty("adouble");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    // invalid - double to float
    try {
        producer.getFloatProperty("adouble");
        ProxyAssertSupport.fail("MessageFormatRuntimeException expected");
    } catch (MessageFormatRuntimeException me) {
    // pass
    } catch (Exception ee) {
        ProxyAssertSupport.fail("Caught unexpected exception: " + ee);
    }
    if ((producer.getBooleanProperty("true")) != true) {
        ProxyAssertSupport.fail("conversion from string to boolean - expect true  - failed");
    }
    if ((producer.getBooleanProperty("false")) != false) {
        ProxyAssertSupport.fail("conversion from string to boolean expect false - failed");
    }
    if (producer.getByteProperty("anotherString") != 1) {
        ProxyAssertSupport.fail("conversion from string to byte failed");
    }
    if (producer.getShortProperty("anotherString") != 1) {
        ProxyAssertSupport.fail("conversion from string to short failed");
    }
    if (producer.getIntProperty("anotherString") != 1) {
        ProxyAssertSupport.fail("conversion from string to int failed");
    }
    if (producer.getLongProperty("anotherString") != 1) {
        ProxyAssertSupport.fail("conversion from string to long failed");
    }
    if (producer.getFloatProperty("anotherString") != 1) {
        ProxyAssertSupport.fail("conversion from string to float failed");
    }
    if (producer.getDoubleProperty("anotherString") != 1) {
        ProxyAssertSupport.fail("conversion from string to double failed");
    }
}
Also used : JMSProducer(javax.jms.JMSProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException) JMSContext(javax.jms.JMSContext) MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException) JMSException(javax.jms.JMSException) MessageFormatException(javax.jms.MessageFormatException) Test(org.junit.Test)

Aggregations

MessageFormatRuntimeException (javax.jms.MessageFormatRuntimeException)10 JMSException (javax.jms.JMSException)6 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)5 Test (org.junit.Test)3 BytesMessage (javax.jms.BytesMessage)2 JMSProducer (javax.jms.JMSProducer)2 JMSRuntimeException (javax.jms.JMSRuntimeException)2 MapMessage (javax.jms.MapMessage)2 ActiveMQPropertyConversionException (org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 CompletionListener (javax.jms.CompletionListener)1 JMSContext (javax.jms.JMSContext)1 MessageFormatException (javax.jms.MessageFormatException)1