Search in sources :

Example 1 with SendMessageHook

use of org.apache.rocketmq.client.hook.SendMessageHook in project rocketmq by apache.

the class DefaultMQProducerTest method testSendMessageSync_SuccessWithHook.

@Test
public void testSendMessageSync_SuccessWithHook() throws Throwable {
    when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(createTopicRoute());
    final Throwable[] assertionErrors = new Throwable[1];
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    producer.getDefaultMQProducerImpl().registerSendMessageHook(new SendMessageHook() {

        @Override
        public String hookName() {
            return "TestHook";
        }

        @Override
        public void sendMessageBefore(final SendMessageContext context) {
            assertionErrors[0] = assertInOtherThread(new Runnable() {

                @Override
                public void run() {
                    assertThat(context.getMessage()).isEqualTo(message);
                    assertThat(context.getProducer()).isEqualTo(producer);
                    assertThat(context.getCommunicationMode()).isEqualTo(CommunicationMode.SYNC);
                    assertThat(context.getSendResult()).isNull();
                }
            });
            countDownLatch.countDown();
        }

        @Override
        public void sendMessageAfter(final SendMessageContext context) {
            assertionErrors[0] = assertInOtherThread(new Runnable() {

                @Override
                public void run() {
                    assertThat(context.getMessage()).isEqualTo(message);
                    assertThat(context.getProducer()).isEqualTo(producer.getDefaultMQProducerImpl());
                    assertThat(context.getCommunicationMode()).isEqualTo(CommunicationMode.SYNC);
                    assertThat(context.getSendResult()).isNotNull();
                }
            });
            countDownLatch.countDown();
        }
    });
    SendResult sendResult = producer.send(message);
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
    assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
    assertThat(sendResult.getQueueOffset()).isEqualTo(456L);
    countDownLatch.await();
    if (assertionErrors[0] != null) {
        throw assertionErrors[0];
    }
}
Also used : SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) SendMessageHook(org.apache.rocketmq.client.hook.SendMessageHook) Test(org.junit.Test)

Example 2 with SendMessageHook

use of org.apache.rocketmq.client.hook.SendMessageHook in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQProducerTest method testSendMessageSync_SuccessWithHook.

@Test
public void testSendMessageSync_SuccessWithHook() throws Throwable {
    when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(createTopicRoute());
    final Throwable[] assertionErrors = new Throwable[1];
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    producer.getDefaultMQProducerImpl().registerSendMessageHook(new SendMessageHook() {

        @Override
        public String hookName() {
            return "TestHook";
        }

        @Override
        public void sendMessageBefore(final SendMessageContext context) {
            assertionErrors[0] = assertInOtherThread(new Runnable() {

                @Override
                public void run() {
                    assertThat(context.getMessage()).isEqualTo(message);
                    assertThat(context.getProducer()).isEqualTo(producer);
                    assertThat(context.getCommunicationMode()).isEqualTo(CommunicationMode.SYNC);
                    assertThat(context.getSendResult()).isNull();
                }
            });
            countDownLatch.countDown();
        }

        @Override
        public void sendMessageAfter(final SendMessageContext context) {
            assertionErrors[0] = assertInOtherThread(new Runnable() {

                @Override
                public void run() {
                    assertThat(context.getMessage()).isEqualTo(message);
                    assertThat(context.getProducer()).isEqualTo(producer.getDefaultMQProducerImpl());
                    assertThat(context.getCommunicationMode()).isEqualTo(CommunicationMode.SYNC);
                    assertThat(context.getSendResult()).isNotNull();
                }
            });
            countDownLatch.countDown();
        }
    });
    SendResult sendResult = producer.send(message);
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
    assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
    assertThat(sendResult.getQueueOffset()).isEqualTo(456L);
    countDownLatch.await();
    if (assertionErrors[0] != null) {
        throw assertionErrors[0];
    }
}
Also used : SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) SendMessageHook(org.apache.rocketmq.client.hook.SendMessageHook) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)2 SendMessageHook (org.apache.rocketmq.client.hook.SendMessageHook)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2