Search in sources :

Example 1 with JMSContext

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

the class ClusteredMessagingTestCase method testClusteredTopic.

@Test
public void testClusteredTopic() throws Exception {
    InitialContext contextFromServer0 = createJNDIContextFromServer0();
    InitialContext contextFromServer1 = createJNDIContextFromServer1();
    try (JMSContext jmsContext0 = createJMSContext(createJNDIContextFromServer0());
        JMSContext jmsContext1 = createJMSContext(createJNDIContextFromServer1())) {
        JMSConsumer consumer0 = jmsContext0.createConsumer((Destination) contextFromServer0.lookup(jmsTopicLookup));
        JMSConsumer consumer1 = jmsContext1.createConsumer((Destination) contextFromServer1.lookup(jmsTopicLookup));
        String text = UUID.randomUUID().toString();
        // WIP test if the problem is that the view is not yet propagated
        Thread.sleep(ClusteringTestConstants.GRACE_TIME_TO_MEMBERSHIP_CHANGE);
        // send a message to the topic on server 0
        sendMessage(contextFromServer0, jmsTopicLookup, text);
        // consumers receive it on both servers
        receiveMessage(consumer0, text);
        receiveMessage(consumer1, text);
        String anotherText = UUID.randomUUID().toString();
        // send another message to topic on server 1
        sendMessage(contextFromServer1, jmsTopicLookup, anotherText);
        // consumers receive it on both servers
        receiveMessage(consumer0, anotherText);
        receiveMessage(consumer1, anotherText);
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 2 with JMSContext

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

the class AbstractJMSContext method getContext.

synchronized JMSContext getContext(String injectionPointId, JMSInfo info, ConnectionFactory connectionFactory) {
    JMSContext context = contexts.get(injectionPointId);
    if (context == null) {
        context = createContext(info, connectionFactory);
        contexts.put(injectionPointId, context);
    }
    return context;
}
Also used : JMSContext(javax.jms.JMSContext)

Example 3 with JMSContext

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

the class JMSTopicConsumerTest method testSendAndReceiveOnAutoCreatedTopicJMS2.

@Test(timeout = 60000)
public void testSendAndReceiveOnAutoCreatedTopicJMS2() throws Exception {
    ConnectionFactory cf = new JmsConnectionFactory(getBrokerQpidJMSConnectionURI());
    JMSContext context = cf.createContext();
    String topicName = UUID.randomUUID().toString();
    SimpleString simpleTopicName = SimpleString.toSimpleString(topicName);
    try {
        Topic topic = context.createTopic(topicName);
        JMSProducer producer = context.createProducer();
        TextMessage message = context.createTextMessage("test-message");
        // this will auto-create the address, but not the subscription queue
        producer.send(topic, message);
        assertNotNull(server.getAddressInfo(simpleTopicName));
        assertEquals(RoutingType.MULTICAST, server.getAddressInfo(simpleTopicName).getRoutingType());
        assertTrue(server.getAddressInfo(simpleTopicName).isAutoCreated());
        assertTrue(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());
        // this will auto-create the subscription queue
        JMSConsumer consumer = context.createConsumer(topic);
        assertFalse(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());
        producer.send(topic, message);
        context.start();
        message = (TextMessage) consumer.receive(1000);
        assertNotNull(message);
        assertNotNull(message.getText());
        assertEquals("test-message", message.getText());
        consumer.close();
        assertTrue(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());
    } finally {
        context.close();
    }
}
Also used : JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSProducer(javax.jms.JMSProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Topic(javax.jms.Topic) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 4 with JMSContext

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

the class ExclusiveTest method testExclusiveWithJMS2Producer.

@Test
public void testExclusiveWithJMS2Producer() throws Exception {
    ConnectionFactory fact = getCF();
    JMSContext ctx = addContext(getCF().createContext(JMSContext.SESSION_TRANSACTED));
    try {
        JMSProducer producer = ctx.createProducer();
        Destination queue = ctx.createQueue(queueName.toString());
        JMSConsumer consumer1 = ctx.createConsumer(queue);
        JMSConsumer consumer2 = ctx.createConsumer(queue);
        JMSConsumer consumer3 = ctx.createConsumer(queue);
        ctx.start();
        for (int j = 0; j < 100; j++) {
            TextMessage message = ctx.createTextMessage("Message" + j);
            producer.send(queue, message);
        }
        ctx.commit();
        // All msgs should go to the first consumer
        for (int j = 0; j < 100; j++) {
            TextMessage tm = (TextMessage) consumer1.receive(10000);
            assertNotNull(tm);
            tm.acknowledge();
            assertEquals("Message" + j, tm.getText());
            tm = (TextMessage) consumer2.receiveNoWait();
            assertNull(tm);
            tm = (TextMessage) consumer3.receiveNoWait();
            assertNull(tm);
        }
        ctx.commit();
    } finally {
        ctx.close();
    }
}
Also used : ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination) Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 5 with JMSContext

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

the class JMSSharedConsumerExample method main.

public static void main(final String[] args) throws Exception {
    InitialContext initialContext = null;
    JMSContext jmsContext = null;
    JMSContext jmsContext2 = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perfom a lookup on the queue
        Topic topic = (Topic) initialContext.lookup("topic/exampleTopic");
        // Step 3. Perform a lookup on the Connection Factory
        ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4.Create a JMS Context
        jmsContext = cf.createContext();
        // Step 5. Create a message producer.
        JMSProducer producer = jmsContext.createProducer();
        // Step 6. Create a shared consumer
        JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(topic, "sc1");
        // Step 7. Create a second JMS Context for a second shared consumer
        jmsContext2 = cf.createContext();
        // Step 8. Create the second shared consumer
        JMSConsumer jmsConsumer2 = jmsContext2.createSharedConsumer(topic, "sc1");
        // Step 9. send 2 messages
        producer.send(topic, "this is a String!");
        producer.send(topic, "this is a second String!");
        // Step 10. receive the messages shared by both consumers
        String body = jmsConsumer.receiveBody(String.class, 5000);
        System.out.println("body = " + body);
        body = jmsConsumer2.receiveBody(String.class, 5000);
        System.out.println("body = " + body);
    } finally {
        // Step 11. Be sure to close our JMS resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (jmsContext != null) {
            jmsContext.close();
        }
        if (jmsContext2 != null) {
            jmsContext2.close();
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) Topic(javax.jms.Topic) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext)

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