Search in sources :

Example 1 with JMSConsumer

use of javax.jms.JMSConsumer 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 2 with JMSConsumer

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

the class MessagingServlet method sendAndReceiveMessage.

private String sendAndReceiveMessage(Destination destination, String text) {
    Destination replyTo = context.createTemporaryQueue();
    JMSConsumer consumer = context.createConsumer(replyTo);
    context.createProducer().setJMSReplyTo(replyTo).send(destination, text);
    return consumer.receiveBody(String.class, 5000);
}
Also used : Destination(javax.jms.Destination) JMSConsumer(javax.jms.JMSConsumer)

Example 3 with JMSConsumer

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

the class ExportImportJournalTestCase method receiveMessage.

protected static void receiveMessage(Context ctx, String destinationLookup, boolean expectReceivedMessage, String expectedText) 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, 5000);
        if (expectReceivedMessage) {
            assertNotNull(text);
            assertEquals(expectedText, text);
        } else {
            assertNull("should not have received any message", text);
        }
    }
}
Also used : Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSContext(javax.jms.JMSContext)

Example 4 with JMSConsumer

use of javax.jms.JMSConsumer 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 5 with JMSConsumer

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

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)

Aggregations

JMSConsumer (javax.jms.JMSConsumer)17 JMSContext (javax.jms.JMSContext)15 Test (org.junit.Test)8 Destination (javax.jms.Destination)5 Message (javax.jms.Message)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ConnectionFactory (javax.jms.ConnectionFactory)4 JMSRuntimeException (javax.jms.JMSRuntimeException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 TemporaryQueue (javax.jms.TemporaryQueue)3 TextMessage (javax.jms.TextMessage)3 MessageListener (javax.jms.MessageListener)2 ArrayList (java.util.ArrayList)1 PostConstruct (javax.annotation.PostConstruct)1 RequestScoped (javax.enterprise.context.RequestScoped)1 JMSException (javax.jms.JMSException)1 JMSProducer (javax.jms.JMSProducer)1 InitialContext (javax.naming.InitialContext)1 ContextsService (org.apache.webbeans.spi.ContextsService)1 After (org.junit.After)1