Search in sources :

Example 11 with JMSContext

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

the class JMSCompletionListenerExample method main.

public static void main(final String[] args) throws Exception {
    JMSContext jmsContext = null;
    try {
        // Step 2. Perfom a lookup on the queue
        Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
        // Step 3. Perform a lookup on the Connection Factory
        ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616?confirmationWindowSize=10240");
        // Step 4.Create a JMS Context
        jmsContext = cf.createContext();
        // Step 5. Create a message producer.
        JMSProducer producer = jmsContext.createProducer();
        final CountDownLatch latch = new CountDownLatch(1);
        // Step 6. We want to send the message Asynchronously and be notified when the Broker receives it so we set a completion handler
        producer.setAsync(new CompletionListener() {

            @Override
            public void onCompletion(Message message) {
                System.out.println("message acknowledged by ActiveMQ");
                latch.countDown();
            }

            @Override
            public void onException(Message message, Exception e) {
                e.printStackTrace();
            }
        });
        // Step 6. Send the Message
        producer.send(queue, "this is a string");
        // Step 7. wait for the Completion handler
        if (!latch.await(5, TimeUnit.SECONDS)) {
            throw new IllegalStateException("Completion listener not called as expected.");
        }
    } finally {
        if (jmsContext != null) {
            jmsContext.close();
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) Message(javax.jms.Message) CompletionListener(javax.jms.CompletionListener) JMSProducer(javax.jms.JMSProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) JMSContext(javax.jms.JMSContext)

Example 12 with JMSContext

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

the class JMSContextExample method main.

public static void main(final String[] args) throws Exception {
    // Instantiate the queue
    Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
    // Using closeable interface
    try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
        JMSContext jmsContext = cf.createContext()) {
        // Create a message producer, note that we can chain all this into one statement
        jmsContext.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT).send(queue, "this is a string");
        // Create a Consumer and receive the payload of the message direct.
        String payLoad = jmsContext.createConsumer(queue).receiveBody(String.class);
        System.out.println("payLoad = " + payLoad);
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) Queue(javax.jms.Queue) JMSContext(javax.jms.JMSContext)

Example 13 with JMSContext

use of javax.jms.JMSContext 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 14 with JMSContext

use of javax.jms.JMSContext 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 15 with JMSContext

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

Aggregations

JMSContext (javax.jms.JMSContext)120 Test (org.junit.Test)57 JMSConsumer (javax.jms.JMSConsumer)44 ConnectionFactory (javax.jms.ConnectionFactory)35 Destination (javax.jms.Destination)30 TextMessage (javax.jms.TextMessage)28 JMSProducer (javax.jms.JMSProducer)27 Message (javax.jms.Message)18 JMSRuntimeException (javax.jms.JMSRuntimeException)13 TemporaryQueue (javax.jms.TemporaryQueue)13 InitialContext (javax.naming.InitialContext)12 JMSException (javax.jms.JMSException)10 Queue (javax.jms.Queue)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)7 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 QueueConnectionFactory (javax.jms.QueueConnectionFactory)4 Context (javax.naming.Context)4 NamingException (javax.naming.NamingException)4