Search in sources :

Example 96 with MessageListener

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

the class TempQueueMemoryTest method testLoadRequestReply.

public void testLoadRequestReply() throws Exception {
    for (int i = 0; i < numConsumers; i++) {
        serverSession.createConsumer(serverDestination).setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message msg) {
                try {
                    Destination replyTo = msg.getJMSReplyTo();
                    MessageProducer producer = serverSession.createProducer(replyTo);
                    producer.send(replyTo, msg);
                    if (serverTransactional) {
                        serverSession.commit();
                    }
                    producer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    class Producer extends Thread {

        private final int numToSend;

        public Producer(int numToSend) {
            this.numToSend = numToSend;
        }

        @Override
        public void run() {
            try {
                Session session = clientConnection.createSession(clientTransactional, clientTransactional ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(serverDestination);
                for (int i = 0; i < numToSend; i++) {
                    TemporaryQueue replyTo = session.createTemporaryQueue();
                    MessageConsumer consumer = session.createConsumer(replyTo);
                    Message msg = session.createMessage();
                    msg.setJMSReplyTo(replyTo);
                    producer.send(msg);
                    if (clientTransactional) {
                        session.commit();
                    }
                    consumer.receive();
                    if (clientTransactional) {
                        session.commit();
                    }
                    consumer.close();
                    if (deleteTempQueue) {
                        replyTo.delete();
                    } else {
                    // temp queue will be cleaned up on clientConnection.close
                    }
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
    Vector<Thread> threads = new Vector<>(numProducers);
    for (int i = 0; i < numProducers; i++) {
        threads.add(new Producer(messagesToSend / numProducers));
    }
    startAndJoinThreads(threads);
    clientSession.close();
    serverSession.close();
    clientConnection.close();
    serverConnection.close();
    AdvisoryBroker ab = (AdvisoryBroker) broker.getBroker().getAdaptor(AdvisoryBroker.class);
    // The server destination will be left
    assertTrue(ab.getAdvisoryDestinations().size() == 1);
    assertTrue("should be zero but is " + ab.getAdvisoryConsumers().size(), ab.getAdvisoryConsumers().size() == 0);
    assertTrue("should be zero but is " + ab.getAdvisoryProducers().size(), ab.getAdvisoryProducers().size() == 0);
    RegionBroker rb = (RegionBroker) broker.getBroker().getAdaptor(RegionBroker.class);
    assertTrue(rb.getDestinationMap().size() >= 6);
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) RegionBroker(org.apache.activemq.broker.region.RegionBroker) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Vector(java.util.Vector) Session(javax.jms.Session)

Example 97 with MessageListener

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

the class JMSTest method testMessageListener.

@Test
public void testMessageListener() throws Exception {
    conn = createConnection();
    Session sessionConsumer = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer cons = sessionConsumer.createConsumer(queue1);
    final AtomicReference<Message> message = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    cons.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(final Message m) {
            message.set(m);
            latch.countDown();
        }
    });
    conn.start();
    Session sessionProducer = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer prod = sessionProducer.createProducer(queue1);
    prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage m = sessionProducer.createTextMessage("one");
    prod.send(m);
    boolean gotMessage = latch.await(5000, MILLISECONDS);
    ProxyAssertSupport.assertTrue(gotMessage);
    TextMessage rm = (TextMessage) message.get();
    ProxyAssertSupport.assertEquals("one", rm.getText());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 98 with MessageListener

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

the class MiscellaneousTest method testClosingConsumerFromMessageListenerAutoAck.

/**
 * Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-542
 */
@Test
public void testClosingConsumerFromMessageListenerAutoAck() throws Exception {
    Connection c = null;
    try {
        c = createConnection();
        Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer prod = s.createProducer(queue1);
        Message m = s.createMessage();
        prod.send(m);
    } finally {
        if (c != null) {
            c.close();
        }
    }
    final Result result = new Result();
    Connection conn = createConnection();
    Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final MessageConsumer cons = s.createConsumer(queue1);
    cons.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(final Message m) {
            // close the connection on the same thread that processed the message
            try {
                cons.close();
                result.setSuccess();
            } catch (Exception e) {
                result.setFailure(e);
            }
        }
    });
    conn.start();
    result.waitForResult();
    ProxyAssertSupport.assertTrue(result.isSuccess());
    ProxyAssertSupport.assertNull(result.getFailure());
    // it's auto _ack so message *should not* be acked (auto ack acks after successfully completion of onMessage
    Thread.sleep(1000);
    assertRemainingMessages(1);
    conn.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 99 with MessageListener

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

the class RequestReplyTempDestRemovalAdvisoryRaceTest method noConsumerAdvisory.

private void noConsumerAdvisory() throws JMSException {
    for (BrokerItem item : brokers.values()) {
        ActiveMQConnectionFactory brokerAFactory = new ActiveMQConnectionFactory(item.broker.getTransportConnectorByScheme("tcp").getName() + "?jms.watchTopicAdvisories=false");
        Connection connection = brokerAFactory.createConnection();
        connection.start();
        connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(AdvisorySupport.getNoTopicConsumersAdvisoryTopic(new ActiveMQTempTopic(">"))).setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                sendsWithNoConsumers.incrementAndGet();
            }
        });
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQTempTopic(org.apache.activemq.command.ActiveMQTempTopic) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener)

Example 100 with MessageListener

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

the class RequestReplyTempDestRemovalAdvisoryRaceTest method forwardFailureAdvisory.

private void forwardFailureAdvisory() throws JMSException {
    for (BrokerItem item : brokers.values()) {
        ActiveMQConnectionFactory brokerAFactory = new ActiveMQConnectionFactory(item.broker.getTransportConnectorByScheme("tcp").getName() + "?jms.watchTopicAdvisories=false");
        Connection connection = brokerAFactory.createConnection();
        connection.start();
        connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(AdvisorySupport.getNetworkBridgeForwardFailureAdvisoryTopic()).setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                forwardFailures.incrementAndGet();
            }
        });
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener)

Aggregations

MessageListener (javax.jms.MessageListener)146 Message (javax.jms.Message)128 MessageConsumer (javax.jms.MessageConsumer)103 Session (javax.jms.Session)97 TextMessage (javax.jms.TextMessage)95 CountDownLatch (java.util.concurrent.CountDownLatch)72 Test (org.junit.Test)71 Connection (javax.jms.Connection)60 MessageProducer (javax.jms.MessageProducer)58 JMSException (javax.jms.JMSException)57 Destination (javax.jms.Destination)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)26 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 ConnectionFactory (javax.jms.ConnectionFactory)16 ScheduledMessage (org.apache.activemq.ScheduledMessage)15 ArrayList (java.util.ArrayList)14 Topic (javax.jms.Topic)13 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)13