Search in sources :

Example 46 with JMSContext

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

the class AbstractMessagingHATestCase method receiveNoMessage.

protected static void receiveNoMessage(Context ctx, String destinationLookup) throws NamingException {
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
    assertNotNull(cf);
    Destination destination = (Destination) ctx.lookup(destinationLookup);
    assertNotNull(destination);
    try (JMSContext context = cf.createContext("guest", "guest");
        JMSConsumer consumer = context.createConsumer(destination)) {
        String text = consumer.receiveBody(String.class, TimeoutUtil.adjust(5000));
        assertNull(text);
    }
}
Also used : Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSContext(javax.jms.JMSContext)

Example 47 with JMSContext

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

the class CorruptedLogTestCase method testCorruptedJournal.

/**
 * Set the journal file size to 100k.
 * Send 100 messages of 20k.
 * Set the journal file size to 200k.
 * Restart the server.
 * Consume the messages.
 * Should have journal files in the attic.
 *
 * @throws javax.naming.NamingException
 * @throws java.io.IOException
 */
@Test
public void testCorruptedJournal() throws NamingException, IOException {
    counter++;
    InitialContext remoteContext = createJNDIContext();
    ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
    Queue queue = (Queue) remoteContext.lookup("queue/corrupted");
    try (JMSContext context = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
        JMSProducer producer = context.createProducer();
        for (int i = 0; i < 100; i++) {
            producer.send(queue, context.createTextMessage(RandomStringUtils.randomAlphabetic(20000)));
        }
    }
    setJournalSize(200L);
    remoteContext.close();
    managementClient.close();
    container.stop(DEFAULT_FULL_JBOSSAS);
    container.start(DEFAULT_FULL_JBOSSAS);
    managementClient = createManagementClient();
    remoteContext = createJNDIContext();
    cf = (ConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
    queue = (Queue) remoteContext.lookup("queue/corrupted");
    try (JMSContext context = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
        JMSConsumer consumer = context.createConsumer(queue);
        for (int i = 0; i < 100; i++) {
            TextMessage message = (TextMessage) consumer.receive(TimeoutUtil.adjust(500));
            Assert.assertNotNull(message);
        }
    }
    Path attic = getAtticPath();
    Assert.assertTrue("Couldn't find " + attic.toAbsolutePath(), Files.exists(attic));
    Assert.assertTrue(Files.isDirectory(attic));
    try (Stream<Path> stream = Files.list(attic)) {
        long nbAtticFiles = stream.collect(Collectors.counting());
        Assert.assertTrue(nbAtticFiles > 0L);
    }
}
Also used : Path(java.nio.file.Path) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 48 with JMSContext

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

the class CorruptedLogTestCase method testCorruptedJournalNotSaved.

/**
 * Set the journal max attic files to 0 (aka don't store corrupted journal files).
 * Set the journal file size to 100k.
 * Send 100 messages of 20k.
 * Set the journal file size to 200k.
 * Restart the server.
 * Consume the messages.
 * Shouldn't have journal files in the attic.
 *
 * @throws javax.naming.NamingException
 * @throws java.io.IOException
 */
@Test
public void testCorruptedJournalNotSaved() throws NamingException, IOException {
    Path attic = getAtticPath();
    counter++;
    JMSOperations jmsOperations = JMSOperationsProvider.getInstance(managementClient.getControllerClient());
    managementClient.getControllerClient().execute(Operations.createWriteAttributeOperation(jmsOperations.getServerAddress(), "journal-max-attic-files", 0));
    jmsOperations.close();
    managementClient.close();
    container.stop(DEFAULT_FULL_JBOSSAS);
    PathUtil.deleteRecursively(attic);
    container.start(DEFAULT_FULL_JBOSSAS);
    Assert.assertFalse("Couldn't find " + attic.toAbsolutePath(), Files.exists(attic));
    managementClient = createManagementClient();
    InitialContext remoteContext = createJNDIContext();
    ConnectionFactory cf = (ConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
    Queue queue = (Queue) remoteContext.lookup("queue/corrupted");
    try (JMSContext context = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
        JMSProducer producer = context.createProducer();
        for (int i = 0; i < 100; i++) {
            producer.send(queue, context.createTextMessage(RandomStringUtils.randomAlphabetic(20000)));
        }
    }
    setJournalSize(200L);
    remoteContext.close();
    managementClient.close();
    container.stop(DEFAULT_FULL_JBOSSAS);
    container.start(DEFAULT_FULL_JBOSSAS);
    managementClient = createManagementClient();
    remoteContext = createJNDIContext();
    cf = (ConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
    queue = (Queue) remoteContext.lookup("queue/corrupted");
    try (JMSContext context = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
        JMSConsumer consumer = context.createConsumer(queue);
        for (int i = 0; i < 100; i++) {
            TextMessage message = (TextMessage) consumer.receive(TimeoutUtil.adjust(500));
            Assert.assertNotNull(message);
        }
    }
    if (Files.exists(attic)) {
        Assert.assertTrue(Files.isDirectory(attic));
        try (Stream<Path> stream = Files.list(attic)) {
            long nbAtticFiles = stream.collect(Collectors.counting());
            Assert.assertEquals("We shouldn't have any file in the attic", 0L, nbAtticFiles);
        }
    }
}
Also used : Path(java.nio.file.Path) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSOperations(org.jboss.as.test.integration.common.jms.JMSOperations) JMSProducer(javax.jms.JMSProducer) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 49 with JMSContext

use of javax.jms.JMSContext in project tomee by apache.

the class JMS2AMQTest method receiveGetBody.

@Test
public void receiveGetBody() throws InterruptedException {
    final String text = TEXT + "2";
    final AtomicReference<Throwable> error = new AtomicReference<>();
    final CountDownLatch ready = new CountDownLatch(1);
    final CountDownLatch over = new CountDownLatch(1);
    new Thread() {

        @Override
        public void run() {
            {
                setName(JMS2AMQTest.class.getName() + ".receiveGetBody#receiver");
            }
            try (final JMSContext context = cf.createContext()) {
                try (final JMSConsumer consumer = context.createConsumer(destination2)) {
                    ready.countDown();
                    final Message receive = consumer.receive(TimeUnit.MINUTES.toMillis(1));
                    assertEquals(text, receive.getBody(String.class));
                }
            } catch (final Throwable ex) {
                error.set(ex);
            } finally {
                over.countDown();
            }
        }
    }.start();
    ready.await(1, TimeUnit.MINUTES);
    // just to ensure we called receive already
    sleep(150);
    // now send the message
    try (final JMSContext context = cf.createContext()) {
        context.createProducer().send(destination2, text);
    } catch (final JMSRuntimeException ex) {
        fail(ex.getMessage());
    }
    over.await(1, TimeUnit.MINUTES);
    // ensure we got the message and no exception
    final Throwable exception = error.get();
    if (exception != null) {
        exception.printStackTrace();
    }
    assertNull(exception == null ? "ok" : exception.getMessage(), exception);
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 50 with JMSContext

use of javax.jms.JMSContext in project tomee by apache.

the class JMS2AMQTest method cdi.

@Test
public void cdi() throws InterruptedException {
    final String text = TEXT + "3";
    final AtomicReference<Throwable> error = new AtomicReference<>();
    final CountDownLatch ready = new CountDownLatch(1);
    final CountDownLatch over = new CountDownLatch(1);
    new Thread() {

        {
            setName(JMS2AMQTest.class.getName() + ".cdi#receiver");
        }

        @Override
        public void run() {
            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
            // spec defines it for request scope an transaction scope
            contextsService.startContext(RequestScoped.class, null);
            try {
                ready.countDown();
                assertEquals(text, context.createConsumer(destination3).receiveBody(String.class, TimeUnit.MINUTES.toMillis(1)));
                // ensure we dont do a NPE if there is nothing to read
                assertNull(context.createConsumer(destination3).receiveBody(String.class, 100));
            } catch (final Throwable t) {
                error.set(t);
            } finally {
                contextsService.endContext(RequestScoped.class, null);
                over.countDown();
            }
        }
    }.start();
    ready.await(1, TimeUnit.MINUTES);
    // just to ensure we called receive already
    sleep(150);
    // now send the message
    try (final JMSContext context = cf.createContext()) {
        context.createProducer().send(destination3, text);
    } catch (final JMSRuntimeException ex) {
        fail(ex.getMessage());
    }
    over.await(1, TimeUnit.MINUTES);
    // ensure we got the message and no exception
    final Throwable exception = error.get();
    if (exception != null) {
        exception.printStackTrace();
    }
    assertNull(exception == null ? "ok" : exception.getMessage(), exception);
}
Also used : ContextsService(org.apache.webbeans.spi.ContextsService) AtomicReference(java.util.concurrent.atomic.AtomicReference) RequestScoped(javax.enterprise.context.RequestScoped) CountDownLatch(java.util.concurrent.CountDownLatch) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) 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