Search in sources :

Example 16 with JMSProducer

use of javax.jms.JMSProducer 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)

Example 17 with JMSProducer

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

the class MessagePropertyConversionTest method msgNullPropertyConversionTests.

@Test
public void msgNullPropertyConversionTests() throws Exception {
    JMSContext ctx = addContext(getConnectionFactory().createContext());
    JMSProducer producer = ctx.createProducer();
    try {
        producer.setProperty(null, true);
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        producer.setProperty(null, "string");
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        producer.setProperty(null, 1);
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        producer.setProperty(null, 1.0);
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        producer.setProperty(null, 1L);
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        producer.setProperty(null, 1.10f);
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        producer.setProperty(null, (byte) 1);
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        producer.setProperty(null, (short) 1);
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        producer.setProperty(null, new SimpleString("foo"));
        ProxyAssertSupport.fail("expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // pass
    }
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSProducer(javax.jms.JMSProducer) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 18 with JMSProducer

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

the class MessagePropertyConversionTest method testObjectString.

@Test
public void testObjectString() throws Exception {
    JMSContext ctx = addContext(getConnectionFactory().createContext());
    JMSProducer producer = ctx.createProducer();
    producer.setProperty("astring", "test");
    Object prop = producer.getObjectProperty("astring");
    ProxyAssertSupport.assertNotNull(prop);
    ProxyAssertSupport.assertTrue(prop instanceof String);
}
Also used : JMSProducer(javax.jms.JMSProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 19 with JMSProducer

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

the class OutgoingConnectionNoJTATest method testSimpleSendNoXAJMSContext.

@Test
public void testSimpleSendNoXAJMSContext() throws Exception {
    Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
    try (ClientSessionFactory sf = locator.createSessionFactory();
        ClientSession session = sf.createSession();
        ClientConsumer consVerify = session.createConsumer(MDBQUEUE);
        JMSContext jmsctx = qraConnectionFactory.createContext()) {
        session.start();
        // These next 4 lines could be written in a single line however it makes difficult for debugging
        JMSProducer producer = jmsctx.createProducer();
        producer.setProperty("strvalue", "hello");
        TextMessage msgsend = jmsctx.createTextMessage("hello");
        producer.send(q, msgsend);
        ClientMessage msg = consVerify.receive(1000);
        assertNotNull(msg);
        assertEquals("hello", msg.getStringProperty("strvalue"));
    }
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) JMSProducer(javax.jms.JMSProducer) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Queue(javax.jms.Queue) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 20 with JMSProducer

use of javax.jms.JMSProducer in project wildfly by wildfly.

the class AppScopedBean method sendMessage.

public void sendMessage() {
    JMSProducer producer = context.createProducer();
    producer.send(queue, "a message");
}
Also used : JMSProducer(javax.jms.JMSProducer)

Aggregations

JMSProducer (javax.jms.JMSProducer)39 Test (org.junit.Test)32 JMSContext (javax.jms.JMSContext)22 JMSConsumer (javax.jms.JMSConsumer)21 TextMessage (javax.jms.TextMessage)20 Message (javax.jms.Message)11 BytesMessage (javax.jms.BytesMessage)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)8 ConnectionFactory (javax.jms.ConnectionFactory)7 Destination (javax.jms.Destination)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 StreamMessage (javax.jms.StreamMessage)6 JMSException (javax.jms.JMSException)5 JMSRuntimeException (javax.jms.JMSRuntimeException)4 Queue (javax.jms.Queue)4 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)4 Topic (javax.jms.Topic)3 CompletionListener (javax.jms.CompletionListener)2 IllegalStateRuntimeException (javax.jms.IllegalStateRuntimeException)2 InvalidDestinationRuntimeException (javax.jms.InvalidDestinationRuntimeException)2