Search in sources :

Example 1 with MessagePrinter

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

the class ActiveMQClientITBase method testTopicPush.

@Test
public void testTopicPush() throws Exception {
    // Given
    final String testTopicName = "TestPushTopic";
    final ActiveMQTopic testTopic = new ActiveMQTopic(testTopicName);
    final String testMessage = "Hello World for Topic!";
    final MessagePrinter messagePrinter = new MessagePrinter();
    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 MessageListener messageListener = new MessageListener() {

        @Override
        public void onMessage(Message message) {
            try {
                messagePrinter.printMessage(message);
            } catch (Exception e) {
                exception.set(e);
            } finally {
                consumerLatch.countDown();
            }
        }
    };
    final ActiveMQSession consumer1Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    new MessageConsumerBuilder(consumer1Session, testTopic).withMessageListener(messageListener).waitTillStarted().build();
    ActiveMQSession consumer2Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    new MessageConsumerBuilder(consumer2Session, testTopic).withMessageListener(messageListener).waitTillStarted().build();
    // When
    producer.send(expectedTextMessage);
    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, testTopic, producerSession);
    verifyConsumerConsumeEvent(verifier, testTopic, consumer1Session);
    verifyConsumerConsumeEvent(verifier, testTopic, consumer2Session);
    Method printMessageMethod = MessagePrinter.class.getDeclaredMethod("printMessage", Message.class);
    for (int i = 0; i < 2; ++i) {
        verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), printMessageMethod));
    }
    verifier.verifyTraceCount(0);
}
Also used : ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageConsumerBuilder(com.navercorp.pinpoint.plugin.activemq.client.util.MessageConsumerBuilder) MessageListener(javax.jms.MessageListener) MessagePrinter(com.navercorp.test.pinpoint.plugin.activemq.MessagePrinter) 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) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 2 with MessagePrinter

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

the class ActiveMQClientITBase method testQueuePush.

@Test
public void testQueuePush() throws Exception {
    // Given
    final String testQueueName = "TestPushQueue";
    final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
    final String testMessage = "Hello World for Queue!";
    final MessagePrinter messagePrinter = new MessagePrinter();
    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 consumer
    final ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
    new MessageConsumerBuilder(consumerSession, testQueue).waitTillStarted().withMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            try {
                messagePrinter.printMessage(message);
            } catch (Exception e) {
                exception.set(e);
            } finally {
                consumerLatch.countDown();
            }
        }
    }).build();
    // When
    producer.send(expectedTextMessage);
    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(4, 100, 5000);
    verifier.verifyTraceCount(4);
    verifyProducerSendEvent(verifier, testQueue, producerSession);
    verifyConsumerConsumeEvent(verifier, testQueue, consumerSession);
    Method printMessageMethod = MessagePrinter.class.getDeclaredMethod("printMessage", Message.class);
    verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), printMessageMethod));
    verifier.verifyTraceCount(0);
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageConsumerBuilder(com.navercorp.pinpoint.plugin.activemq.client.util.MessageConsumerBuilder) MessageListener(javax.jms.MessageListener) MessagePrinter(com.navercorp.test.pinpoint.plugin.activemq.MessagePrinter) 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) 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 MessagePrinter (com.navercorp.test.pinpoint.plugin.activemq.MessagePrinter)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 Message (javax.jms.Message)2 MessageListener (javax.jms.MessageListener)2 MessageProducer (javax.jms.MessageProducer)2 TextMessage (javax.jms.TextMessage)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