Search in sources :

Example 1 with MessageReceiver

use of com.navercorp.test.pinpoint.plugin.activemq.MessageReceiver in project pinpoint by naver.

the class ActiveMQClientITBase method testTopicPull.

@Test
public void testTopicPull() throws Exception {
    // Given
    final String testTopicName = "TestPullTopic";
    final ActiveMQTopic testTopic = new ActiveMQTopic(testTopicName);
    final String testMessage = "Hello World for Topic!";
    final CountDownLatch consumerLatch = new CountDownLatch(2);
    final AtomicReference<Exception> exception = new AtomicReference<>();
    // create producer
    final ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    final MessageProducer producer = new MessageProducerBuilder(producerSession, testTopic).waitTillStarted().build();
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // create 2 consumers
    final ActiveMQSession consumer1Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    final MessageConsumer consumer1 = new MessageConsumerBuilder(consumer1Session, testTopic).waitTillStarted().build();
    final MessageReceiver messageReceiver1 = new MessageReceiver(consumer1);
    final Thread consumer1Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                messageReceiver1.receiveMessage(1000L);
            } catch (Exception e) {
                exception.set(e);
            } finally {
                consumerLatch.countDown();
            }
        }
    });
    final ActiveMQSession consumer2Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    final MessageConsumer consumer2 = new MessageConsumerBuilder(consumer2Session, testTopic).waitTillStarted().build();
    final MessageReceiver messageReceiver2 = new MessageReceiver(consumer2);
    final Thread consumer2Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                messageReceiver2.receiveMessage(1000L);
            } catch (Exception e) {
                exception.set(e);
            } finally {
                consumerLatch.countDown();
            }
        }
    });
    // When
    producer.send(expectedTextMessage);
    consumer1Thread.start();
    consumer2Thread.start();
    consumerLatch.await(1L, TimeUnit.SECONDS);
    // Then
    assertNoConsumerError(exception.get());
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    verifier.awaitTraceCount(13, 100, 5000);
    verifier.verifyTraceCount(13);
    verifyProducerSendEvent(verifier, testTopic, producerSession);
    verifyConsumerConsumeEvent(verifier, testTopic, consumer1Session);
    verifyConsumerConsumeEvent(verifier, testTopic, consumer2Session);
    // Separate transaction for the consumer's request to receive the message
    Method receiveMessageMethod = MessageReceiver.class.getDeclaredMethod("receiveMessage", long.class);
    Method receiveMethod = ActiveMQMessageConsumer.class.getDeclaredMethod("receive", long.class);
    Method printMessageMethod = MessagePrinter.class.getDeclaredMethod("printMessage", Message.class);
    for (int i = 0; i < 2; ++i) {
        verifier.verifyDiscreteTrace(root(ServiceType.STAND_ALONE.getName(), "Entry Point Process", null, null, null, null), event(ServiceType.INTERNAL_METHOD.getName(), receiveMessageMethod), event(ACTIVEMQ_CLIENT_INTERNAL, receiveMethod, annotation("activemq.message", getMessageAsString(expectedTextMessage))), event(ServiceType.INTERNAL_METHOD.getName(), printMessageMethod));
    }
    verifier.verifyTraceCount(0);
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) MessageConsumerBuilder(com.navercorp.pinpoint.plugin.activemq.client.util.MessageConsumerBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) JMSException(javax.jms.JMSException) MessageProducerBuilder(com.navercorp.pinpoint.plugin.activemq.client.util.MessageProducerBuilder) ActiveMQSession(org.apache.activemq.ActiveMQSession) MessageReceiver(com.navercorp.test.pinpoint.plugin.activemq.MessageReceiver) PollingMessageReceiver(com.navercorp.test.pinpoint.plugin.activemq.PollingMessageReceiver) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 2 with MessageReceiver

use of com.navercorp.test.pinpoint.plugin.activemq.MessageReceiver in project pinpoint by naver.

the class ActiveMQClientITBase method testQueuePull.

@Test
public void testQueuePull() throws Exception {
    // Given
    final String testQueueName = "TestPullQueue";
    final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
    final String testMessage = "Hello World for Queue!";
    final CountDownLatch consumerLatch = new CountDownLatch(1);
    final AtomicReference<Exception> exception = new AtomicReference<>();
    // create producer
    final ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
    final MessageProducer producer = new MessageProducerBuilder(producerSession, testQueue).waitTillStarted().build();
    final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
    // create and start message receiver
    final ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    final MessageConsumer consumer = new MessageConsumerBuilder(consumerSession, testQueue).waitTillStarted().build();
    final MessageReceiver messageReceiver = new MessageReceiver(consumer);
    final Thread consumerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                messageReceiver.receiveMessage(1000L);
            } catch (Exception e) {
                exception.set(e);
            } finally {
                consumerLatch.countDown();
            }
        }
    });
    // When
    producer.send(expectedTextMessage);
    consumerThread.start();
    consumerLatch.await(1L, TimeUnit.SECONDS);
    // Then
    assertNoConsumerError(exception.get());
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    // Wait till all traces are recorded (consumer traces are recorded from another thread)
    verifier.awaitTraceCount(7, 100, 5000);
    verifier.verifyTraceCount(7);
    verifyProducerSendEvent(verifier, testQueue, producerSession);
    verifyConsumerConsumeEvent(verifier, testQueue, consumerSession);
    // Separate transaction for the consumer's request to receive the message
    verifier.verifyTrace(root(ServiceType.STAND_ALONE.getName(), "Entry Point Process", null, null, null, null));
    Method receiveMessageMethod = MessageReceiver.class.getDeclaredMethod("receiveMessage", long.class);
    verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), receiveMessageMethod));
    Method receiveMethod = ActiveMQMessageConsumer.class.getDeclaredMethod("receive", long.class);
    verifier.verifyTrace(event(ACTIVEMQ_CLIENT_INTERNAL, receiveMethod, annotation("activemq.message", getMessageAsString(expectedTextMessage))));
    Method printMessageMethod = MessagePrinter.class.getDeclaredMethod("printMessage", Message.class);
    verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), printMessageMethod));
    verifier.verifyTraceCount(0);
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) MessageConsumerBuilder(com.navercorp.pinpoint.plugin.activemq.client.util.MessageConsumerBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) JMSException(javax.jms.JMSException) MessageProducerBuilder(com.navercorp.pinpoint.plugin.activemq.client.util.MessageProducerBuilder) ActiveMQSession(org.apache.activemq.ActiveMQSession) MessageReceiver(com.navercorp.test.pinpoint.plugin.activemq.MessageReceiver) PollingMessageReceiver(com.navercorp.test.pinpoint.plugin.activemq.PollingMessageReceiver) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Aggregations

PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)2 MessageConsumerBuilder (com.navercorp.pinpoint.plugin.activemq.client.util.MessageConsumerBuilder)2 MessageProducerBuilder (com.navercorp.pinpoint.plugin.activemq.client.util.MessageProducerBuilder)2 MessageReceiver (com.navercorp.test.pinpoint.plugin.activemq.MessageReceiver)2 PollingMessageReceiver (com.navercorp.test.pinpoint.plugin.activemq.PollingMessageReceiver)2 Method (java.lang.reflect.Method)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 JMSException (javax.jms.JMSException)2 MessageConsumer (javax.jms.MessageConsumer)2 MessageProducer (javax.jms.MessageProducer)2 TextMessage (javax.jms.TextMessage)2 ActiveMQMessageConsumer (org.apache.activemq.ActiveMQMessageConsumer)2 ActiveMQSession (org.apache.activemq.ActiveMQSession)2 Test (org.junit.Test)2 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)1 ActiveMQTopic (org.apache.activemq.command.ActiveMQTopic)1