Search in sources :

Example 61 with JMSContext

use of javax.jms.JMSContext in project eap-additional-testsuite by jboss-set.

the class TransactionScopedJMSContextTestCase method sendAndReceiveWithContext.

@Test
public void sendAndReceiveWithContext() throws JMSException, InterruptedException {
    int numThreads = 5;
    int numMessages = 10;
    launcher.start(numThreads, numMessages);
    int receivedMessages = 0;
    try (JMSContext context = factory.createContext()) {
        JMSConsumer consumer = context.createConsumer(queue);
        Message m;
        do {
            m = consumer.receive(1000);
            if (m != null) {
                receivedMessages++;
            }
        } while (m != null);
    }
    assertEquals(numThreads * numMessages, receivedMessages);
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 62 with JMSContext

use of javax.jms.JMSContext in project eap-additional-testsuite by jboss-set.

the class InjectedJMSContextTestCase method sendAndReceiveWithContext.

@Test
public void sendAndReceiveWithContext() throws JMSException {
    String text = UUID.randomUUID().toString();
    try (JMSContext context = factory.createContext()) {
        TemporaryQueue tempQueue = context.createTemporaryQueue();
        context.createProducer().send(tempQueue, text);
        assertMessageIsReceived(tempQueue, context, text, false);
    }
}
Also used : TemporaryQueue(javax.jms.TemporaryQueue) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 63 with JMSContext

use of javax.jms.JMSContext in project eap-additional-testsuite by jboss-set.

the class InjectedJMSContextTestCase method tearDown.

@After
public void tearDown() throws JMSException {
    // drain the queue to remove any pending messages from it
    try (JMSContext context = factory.createContext()) {
        JMSConsumer consumer = context.createConsumer(queue);
        Message m;
        do {
            m = consumer.receiveNoWait();
        } while (m != null);
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) JMSContext(javax.jms.JMSContext) After(org.junit.After)

Example 64 with JMSContext

use of javax.jms.JMSContext in project eap-additional-testsuite by jboss-set.

the class InjectedJMSContextTestCase method sendAndReceiveFromMDB.

private void sendAndReceiveFromMDB(boolean rollback) throws JMSException {
    String text = "sendAndReceiveFromMDB " + rollback;
    try (JMSContext context = factory.createContext()) {
        TemporaryQueue replyTo = context.createTemporaryQueue();
        context.createProducer().setJMSReplyTo(replyTo).setProperty("rollback", rollback).send(queue, text);
        assertMessageIsReceived(replyTo, context, text, rollback);
    }
}
Also used : TemporaryQueue(javax.jms.TemporaryQueue) JMSContext(javax.jms.JMSContext)

Example 65 with JMSContext

use of javax.jms.JMSContext in project eap-additional-testsuite by jboss-set.

the class AbstractSaslTestBase method sendAndReceiveMsg.

protected void sendAndReceiveMsg(int remotingPort, boolean expectedSaslFail, String username, String password) {
    Context namingContext = null;
    try {
        // Set up the namingContext for the JNDI lookup
        final Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, WildFlyInitialContextFactory.class.getName());
        env.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
        env.put(Context.PROVIDER_URL, "remote://" + HOST_FMT + ":" + remotingPort);
        namingContext = new InitialContext(env);
        // Perform the JNDI lookups
        ConnectionFactory connectionFactory = null;
        try {
            connectionFactory = (ConnectionFactory) namingContext.lookup(CONNECTION_FACTORY);
            assertFalse("JNDI lookup should have failed.", expectedSaslFail);
        } catch (NamingException e) {
            if (expectedSaslFail) {
                // only SASL failures are expected
                assertTrue("Unexpected cause of lookup failure", e.getCause() instanceof SaslException);
                return;
            }
            throw e;
        }
        Destination destination = (Destination) namingContext.lookup(NAME);
        int count = 1;
        try (JMSContext context = (username != null ? connectionFactory.createContext(username, password) : connectionFactory.createContext())) {
            // Send the specified number of messages
            for (int i = 0; i < count; i++) {
                context.createProducer().send(destination, MESSAGE);
            }
            // Create the JMS consumer
            JMSConsumer consumer = context.createConsumer(destination);
            // Then receive the same number of messages that were sent
            for (int i = 0; i < count; i++) {
                String text = consumer.receiveBody(String.class, 5000);
                Assert.assertEquals(MESSAGE, text);
            }
        }
    } catch (NamingException e) {
        LOGGER.error("Naming problem occured.", e);
        throw new RuntimeException(e);
    } finally {
        if (namingContext != null) {
            try {
                namingContext.close();
            } catch (NamingException e) {
                LOGGER.error("Naming problem occured during closing context.", e);
            }
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) Context(javax.naming.Context) WildFlyInitialContextFactory(org.wildfly.naming.client.WildFlyInitialContextFactory) Destination(javax.jms.Destination) JMSConsumer(javax.jms.JMSConsumer) Properties(java.util.Properties) SaslException(javax.security.sasl.SaslException) InitialContext(javax.naming.InitialContext) ConnectionFactory(javax.jms.ConnectionFactory) NamingException(javax.naming.NamingException) 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