Search in sources :

Example 21 with JMSContext

use of javax.jms.JMSContext in project javaee7-samples by javaee-samples.

the class SubscriptionCreator method createSubscription.

/**
 * We create the subscription at soonest possible time after deployment so we
 * wouldn't miss any message
 */
@PostConstruct
void createSubscription() {
    try (JMSContext jms = factory.createContext()) {
        // <1> This is factory with clientId specified
        // <2> creates durable subscription on the topic
        JMSConsumer consumer = jms.createDurableConsumer(topic, Resources.SUBSCRIPTION);
        consumer.close();
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSContext(javax.jms.JMSContext) PostConstruct(javax.annotation.PostConstruct)

Example 22 with JMSContext

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

the class DefaultTimeoutMDB method onMessage.

@Override
public void onMessage(Message message) {
    try {
        log.tracef("onMessage received message: %s '%s'", message, ((TextMessage) message).getText());
        final Destination replyTo = message.getJMSReplyTo();
        if (replyTo == null) {
            throw new RuntimeException("ReplyTo info in message was not specified" + " and bean does not know where to reply to");
        }
        // should timeout txn - this timeout waiting has to be greater than 1 s
        // (see transaction timeout default setup task)
        TxTestUtil.waitForTimeout(tm);
        TxTestUtil.enlistTestXAResource(tm.getTransaction(), checker);
        try (JMSContext context = factory.createContext()) {
            context.createProducer().setJMSCorrelationID(message.getJMSMessageID()).send(replyTo, REPLY_PREFIX + ((TextMessage) message).getText());
        }
    } catch (Exception e) {
        throw new RuntimeException("onMessage method execution failed", e);
    }
}
Also used : Destination(javax.jms.Destination) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

Example 23 with JMSContext

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

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 24 with JMSContext

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

the class ExternalJMSDestinationManagementTestCase method testCreatedDestinations.

@Test
public void testCreatedDestinations() throws Exception {
    ActiveMQJMSConnectionFactory cf = (ActiveMQJMSConnectionFactory) remoteContext.lookup("jms/TestConnectionFactory");
    Assert.assertNotNull(cf);
    Destination myExternalQueue = (Destination) remoteContext.lookup("jms/queue/" + QUEUE_NAME);
    Assert.assertNotNull(myExternalQueue);
    Destination myLegacyExternalQueue = (Destination) remoteContext.lookup("jms/queue/" + LEGACY_QUEUE_NAME);
    Assert.assertNotNull(myLegacyExternalQueue);
    Destination myExternalTopic = (Destination) remoteContext.lookup("jms/topic/" + TOPIC_NAME);
    Assert.assertNotNull(myExternalTopic);
    Destination myLegacyExternalTopic = (Destination) remoteContext.lookup("jms/topic/" + LEGACY_TOPIC_NAME);
    Assert.assertNotNull(myLegacyExternalTopic);
    try (JMSContext jmsContext = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
        checkJMSDestination(jmsContext, myLegacyExternalQueue);
        checkJMSDestination(jmsContext, myLegacyExternalTopic);
    }
    cf = (ActiveMQJMSConnectionFactory) remoteContext.lookup("jms/TestConnectionFactory");
    cf.setEnable1xPrefixes(false);
    try (JMSContext jmsContext = cf.createContext("guest", "guest", JMSContext.AUTO_ACKNOWLEDGE)) {
        checkJMSDestination(jmsContext, myExternalQueue);
        checkJMSDestination(jmsContext, myExternalTopic);
    }
    JMSOperations ops = JMSOperationsProvider.getInstance(managementClient.getControllerClient());
    if (!ops.isRemoteBroker()) {
        ModelNode op = Operations.createOperation("read-children-names", ops.getServerAddress());
        op.get("child-type").set("runtime-queue");
        List<ModelNode> runtimeQueues = Operations.readResult(managementClient.getControllerClient().execute(op)).asList();
        Set<String> queues = runtimeQueues.stream().map(ModelNode::asString).collect(Collectors.toSet());
        Assert.assertEquals(initialQueues.size() + 4, queues.size());
        Assert.assertTrue("We should have myExternalQueue queue", queues.contains(QUEUE_NAME));
        Assert.assertTrue("We should have myExternalQueue queue", queues.contains("jms.queue." + LEGACY_QUEUE_NAME));
        queues.removeAll(initialQueues);
        queues.remove(QUEUE_NAME);
        queues.remove("jms.queue." + LEGACY_QUEUE_NAME);
        checkRuntimeQueue(ops, QUEUE_NAME, "ANYCAST", QUEUE_NAME);
        checkRuntimeQueue(ops, "jms.queue." + LEGACY_QUEUE_NAME, "ANYCAST", "jms.queue." + LEGACY_QUEUE_NAME);
        for (String topicId : queues) {
            checkRuntimeTopic(ops, topicId);
        }
    }
}
Also used : Destination(javax.jms.Destination) ActiveMQJMSConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory) JMSOperations(org.jboss.as.test.integration.common.jms.JMSOperations) ModelNode(org.jboss.dmr.ModelNode) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 25 with JMSContext

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

the class ExportImportJournalTestCase method sendMessage.

protected static void sendMessage(Context ctx, String destinationLookup, String text) throws NamingException, JMSException {
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
    assertNotNull(cf);
    Destination destination = (Destination) ctx.lookup(destinationLookup);
    assertNotNull(destination);
    try (JMSContext context = cf.createContext("guest", "guest")) {
        TextMessage message = context.createTextMessage(text);
        message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
        context.createProducer().send(destination, message);
    }
}
Also used : Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

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