org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl Java Examples

The following examples show how to use org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultMQProducer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor specifying namespace, producer group, RPC hook, enabled msgTrace flag and customized trace topic
 * name.
 *
 * @param namespace Namespace for this MQ Producer instance.
 * @param producerGroup Producer group, see the name-sake field.
 * @param rpcHook RPC hook to execute per each remoting command execution.
 * @param enableMsgTrace Switch flag instance for message trace.
 * @param customizedTraceTopic The name value of message trace topic.If you don't config,you can use the default
 * trace topic name.
 */
public DefaultMQProducer(final String namespace, final String producerGroup, RPCHook rpcHook,
    boolean enableMsgTrace, final String customizedTraceTopic) {
    this.namespace = namespace;
    this.producerGroup = producerGroup;
    defaultMQProducerImpl = new DefaultMQProducerImpl(this, rpcHook);
    //if client open the message trace feature
    if (enableMsgTrace) {
        try {
            AsyncTraceDispatcher dispatcher = new AsyncTraceDispatcher(producerGroup, TraceDispatcher.Type.PRODUCE, customizedTraceTopic, rpcHook);
            dispatcher.setHostProducer(this.getDefaultMQProducerImpl());
            traceDispatcher = dispatcher;
            this.getDefaultMQProducerImpl().registerSendMessageHook(
                new SendMessageTraceHookImpl(traceDispatcher));
        } catch (Throwable e) {
            log.error("system mqtrace hook init failed ,maybe can't send msg trace data");
        }
    }
}
 
Example #2
Source File: DefaultMQProducer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor specifying producer group.
 *
 * @param producerGroup Producer group, see the name-sake field.
 * @param rpcHook RPC hook to execute per each remoting command execution.
 * @param enableMsgTrace Switch flag instance for message trace.
 * @param customizedTraceTopic The name value of message trace topic.If you don't config,you can use the default
 * trace topic name.
 */
public DefaultMQProducer(final String producerGroup, RPCHook rpcHook, boolean enableMsgTrace,
    final String customizedTraceTopic) {
    this.producerGroup = producerGroup;
    defaultMQProducerImpl = new DefaultMQProducerImpl(this, rpcHook);
    //if client open the message trace feature
    if (enableMsgTrace) {
        try {
            AsyncTraceDispatcher dispatcher = new AsyncTraceDispatcher(producerGroup, TraceDispatcher.Type.PRODUCE, customizedTraceTopic, rpcHook);
            dispatcher.setHostProducer(this.defaultMQProducerImpl);
            traceDispatcher = dispatcher;
            this.defaultMQProducerImpl.registerSendMessageHook(
                new SendMessageTraceHookImpl(traceDispatcher));
        } catch (Throwable e) {
            log.error("system mqtrace hook init failed ,maybe can't send msg trace data");
        }
    }
}
 
Example #3
Source File: DefaultMQProducerTest.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws Exception {
    String producerGroupTemp = producerGroupPrefix + System.currentTimeMillis();
    producer = new DefaultMQProducer(producerGroupTemp);
    producer.setNamesrvAddr("127.0.0.1:9876");
    message = new Message(topic, new byte[] {'a'});
    zeroMsg = new Message(topic, new byte[] {});

    producer.start();

    Field field = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(producer.getDefaultMQProducerImpl(), mQClientFactory);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);

    producer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTemp, producer.getDefaultMQProducerImpl());

    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class))).thenCallRealMethod();
    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendCallback.class), nullable(TopicPublishInfo.class), nullable(MQClientInstance.class), anyInt(), nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class)))
        .thenReturn(createSendResult(SendStatus.SEND_OK));
}
 
Example #4
Source File: DefaultMQProducerTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws Exception {
    String producerGroupTemp = producerGroupPrefix + System.currentTimeMillis();
    producer = new DefaultMQProducer(producerGroupTemp);
    producer.setNamesrvAddr("127.0.0.1:9876");
    message = new Message(topic, new byte[] {'a'});
    zeroMsg = new Message(topic, new byte[] {});

    producer.start();

    Field field = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(producer.getDefaultMQProducerImpl(), mQClientFactory);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);

    producer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTemp, producer.getDefaultMQProducerImpl());

    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class))).thenCallRealMethod();
    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendCallback.class), nullable(TopicPublishInfo.class), nullable(MQClientInstance.class), anyInt(), nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class)))
        .thenReturn(createSendResult(SendStatus.SEND_OK));
}
 
Example #5
Source File: DefaultMQProducerTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws Exception {
    String producerGroupTemp = producerGroupPrefix + System.currentTimeMillis();
    producer = new DefaultMQProducer(producerGroupTemp);
    producer.setNamesrvAddr("127.0.0.1:9876");
    message = new Message(topic, new byte[] {'a'});
    zeroMsg = new Message(topic, new byte[] {});

    producer.start();

    Field field = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(producer.getDefaultMQProducerImpl(), mQClientFactory);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);

    producer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTemp, producer.getDefaultMQProducerImpl());

    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class))).thenCallRealMethod();
    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendCallback.class), nullable(TopicPublishInfo.class), nullable(MQClientInstance.class), anyInt(), nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class)))
        .thenReturn(createSendResult(SendStatus.SEND_OK));
}
 
Example #6
Source File: DefaultMQProducerTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws Exception {
    String producerGroupTemp = producerGroupPrefix + System.currentTimeMillis();
    producer = new DefaultMQProducer(producerGroupTemp);
    producer.setNamesrvAddr("127.0.0.1:9876");
    message = new Message(topic, new byte[] {'a'});
    zeroMsg = new Message(topic, new byte[] {});

    producer.start();

    Field field = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(producer.getDefaultMQProducerImpl(), mQClientFactory);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);

    producer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTemp, producer.getDefaultMQProducerImpl());

    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class))).thenCallRealMethod();
    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendCallback.class), nullable(TopicPublishInfo.class), nullable(MQClientInstance.class), anyInt(), nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class)))
        .thenReturn(createSendResult(SendStatus.SEND_OK));
}
 
Example #7
Source File: MQClientInstanceTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProducer() {
    boolean flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();

    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isFalse();

    mqClientInstance.unregisterProducer(group);
    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();
}
 
Example #8
Source File: MQClientInstanceTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProducer() {
    boolean flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();

    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isFalse();

    mqClientInstance.unregisterProducer(group);
    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();
}
 
Example #9
Source File: DefaultMQProducerTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws Exception {
    String producerGroupTemp = producerGroupPrefix + System.currentTimeMillis();
    producer = new DefaultMQProducer(producerGroupTemp);
    producer.setNamesrvAddr("127.0.0.1:9876");
    producer.setCompressMsgBodyOverHowmuch(16);
    message = new Message(topic, new byte[] {'a'});
    zeroMsg = new Message(topic, new byte[] {});
    bigMessage = new Message(topic, "This is a very huge message!".getBytes());

    producer.start();

    Field field = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(producer.getDefaultMQProducerImpl(), mQClientFactory);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);

    producer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTemp, producer.getDefaultMQProducerImpl());

    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class))).thenCallRealMethod();
    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendCallback.class), nullable(TopicPublishInfo.class), nullable(MQClientInstance.class), anyInt(), nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class)))
        .thenReturn(createSendResult(SendStatus.SEND_OK));
}
 
Example #10
Source File: MQClientInstance.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 注册Producer
 * @param group group
 * @param producer producer
 * @return l
 */
public boolean registerProducer(final String group, final DefaultMQProducerImpl producer) {
    if (null == group || null == producer) {
        return false;
    }

    MQProducerInner prev = this.producerTable.putIfAbsent(group, producer);
    if (prev != null) {
        log.warn("the producer group[{}] exist already.", group);
        return false;
    }

    return true;
}
 
Example #11
Source File: DeFiBusClientAPIImplTest.java    From DeFiBus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendMessagetypeOfnull_Success() throws RemotingException, InterruptedException, MQBrokerException {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            InvokeCallback callback = mock.getArgument(3);
            RemotingCommand request = mock.getArgument(1);
            ResponseFuture responseFuture = new ResponseFuture(null, request.getOpaque(), 3 * 1000, null, null);
            responseFuture.setResponseCommand(createSuccessResponse(request));
            callback.operationComplete(responseFuture);
            return null;
        }
    }).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendMessageContext sendMessageContext = new SendMessageContext();
    sendMessageContext.setProducer(new DefaultMQProducerImpl(new DefaultMQProducer()));
    deFiBusClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC,
        new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
                assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
                assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
                assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
            }

            @Override
            public void onException(Throwable e) {
            }
        },
        null, null, 0, sendMessageContext, defaultMQProducerImpl);
}
 
Example #12
Source File: DeFiBusClientAPIImplTest.java    From DeFiBus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendMessageTypeOfReply_Success() throws Exception {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            InvokeCallback callback = mock.getArgument(3);
            RemotingCommand request = mock.getArgument(1);
            ResponseFuture responseFuture = new ResponseFuture(null, request.getOpaque(), 3 * 1000, null, null);
            responseFuture.setResponseCommand(createSuccessResponse(request));
            callback.operationComplete(responseFuture);
            return null;
        }
    }).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendMessageContext sendMessageContext = new SendMessageContext();
    sendMessageContext.setProducer(new DefaultMQProducerImpl(new DefaultMQProducer()));
    msg.getProperties().put("msgType", "reply");
    deFiBusClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC,
        new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
                assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
                assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
                assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
            }

            @Override
            public void onException(Throwable e) {
            }
        }, null, null, 0, sendMessageContext, defaultMQProducerImpl);
}
 
Example #13
Source File: RmqClusterProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public int getPartitionsSize(String topic) {
    DefaultMQProducerImpl producer = pickRocketMQProducer().getDefaultMQProducerImpl();
    if (producer.isPublishTopicNeedUpdate(topic)) {
        if (!producer.getmQClientFactory().updateTopicRouteInfoFromNameServer(topic)) {
            throw new RuntimeException("Fetching the size of rmq partition failed!");
        }
    }
    return producer.getTopicPublishInfoTable().get(topic).getMessageQueueList().size();
}
 
Example #14
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public SendResult sendMessage(
    final String addr,
    final String brokerName,
    final Message msg,
    final SendMessageRequestHeader requestHeader,
    final long timeoutMillis,
    final CommunicationMode communicationMode,
    final SendMessageContext context,
    final DefaultMQProducerImpl producer
) throws RemotingException, MQBrokerException, InterruptedException {
    return sendMessage(addr, brokerName, msg, requestHeader, timeoutMillis, communicationMode, null, null, null, 0, context, producer);
}
 
Example #15
Source File: DefaultMQProducerTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws Exception {
    String producerGroupTemp = producerGroupPrefix + System.currentTimeMillis();
    producer = new DefaultMQProducer(producerGroupTemp);
    producer.setNamesrvAddr("127.0.0.1:9876");
    producer.setCompressMsgBodyOverHowmuch(16);
    message = new Message(topic, new byte[] {'a'});
    zeroMsg = new Message(topic, new byte[] {});
    bigMessage = new Message(topic, "This is a very huge message!".getBytes());

    producer.start();

    Field field = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(producer.getDefaultMQProducerImpl(), mQClientFactory);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);

    producer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTemp, producer.getDefaultMQProducerImpl());

    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class))).thenCallRealMethod();
    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendCallback.class), nullable(TopicPublishInfo.class), nullable(MQClientInstance.class), anyInt(), nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class)))
        .thenReturn(createSendResult(SendStatus.SEND_OK));
}
 
Example #16
Source File: MQClientInstanceTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProducer() {
    boolean flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();

    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isFalse();

    mqClientInstance.unregisterProducer(group);
    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();
}
 
Example #17
Source File: MQClientInstance.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public boolean registerProducer(final String group, final DefaultMQProducerImpl producer) {
        if (null == group || null == producer) {
            return false;
        }

//        这里是用concurrentHashMap在本地内存中维护注册信息
        MQProducerInner prev = this.producerTable.putIfAbsent(group, producer);
        if (prev != null) {
            log.warn("the producer group[{}] exist already.", group);
            return false;
        }

        return true;
    }
 
Example #18
Source File: MQClientInstance.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * 注册Producer
 * 若之前创建过,则返回失败;否则,成功。
 *
 * @param group    分组
 * @param producer producer
 * @return 是否成功。
 */
public boolean registerProducer(final String group, final DefaultMQProducerImpl producer) {
    if (null == group || null == producer) {
        return false;
    }

    MQProducerInner prev = this.producerTable.putIfAbsent(group, producer);
    if (prev != null) {
        log.warn("the producer group[{}] exist already.", group);
        return false;
    }

    return true;
}
 
Example #19
Source File: MQClientInstanceTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProducer() {
    boolean flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();

    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isFalse();

    mqClientInstance.unregisterProducer(group);
    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();
}
 
Example #20
Source File: MQClientInstance.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public boolean registerProducer(final String group, final DefaultMQProducerImpl producer) {
    if (null == group || null == producer) {
        return false;
    }

    MQProducerInner prev = this.producerTable.putIfAbsent(group, producer);
    if (prev != null) {
        log.warn("the producer group[{}] exist already.", group);
        return false;
    }

    return true;
}
 
Example #21
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public SendResult sendMessage(
    final String addr,
    final String brokerName,
    final Message msg,
    final SendMessageRequestHeader requestHeader,
    final long timeoutMillis,
    final CommunicationMode communicationMode,
    final SendMessageContext context,
    final DefaultMQProducerImpl producer
) throws RemotingException, MQBrokerException, InterruptedException {
    return sendMessage(addr, brokerName, msg, requestHeader, timeoutMillis, communicationMode, null, null, null, 0, context, producer);
}
 
Example #22
Source File: MQClientAPIImplTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendMessageTypeofReply() throws Exception {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            InvokeCallback callback = mock.getArgument(3);
            RemotingCommand request = mock.getArgument(1);
            ResponseFuture responseFuture = new ResponseFuture(null, request.getOpaque(), 3 * 1000, null, null);
            responseFuture.setResponseCommand(createSuccessResponse(request));
            callback.operationComplete(responseFuture);
            return null;
        }
    }).when(remotingClient).invokeAsync(Matchers.anyString(), Matchers.any(RemotingCommand.class), Matchers.anyLong(), Matchers.any(InvokeCallback.class));
    SendMessageContext sendMessageContext = new SendMessageContext();
    sendMessageContext.setProducer(new DefaultMQProducerImpl(new DefaultMQProducer()));
    msg.getProperties().put("MSG_TYPE", "reply");
    mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC,
        new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
                assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
                assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
                assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
            }

            @Override
            public void onException(Throwable e) {
            }
        }, null, null, 0, sendMessageContext, defaultMQProducerImpl);
}
 
Example #23
Source File: MQClientInstanceTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProducer() {
    boolean flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();

    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isFalse();

    mqClientInstance.unregisterProducer(group);
    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();
}
 
Example #24
Source File: RmqClusterProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public int getPartitionsSize(String topic) {
    DefaultMQProducerImpl producer = pickRocketMQProducer().getDefaultMQProducerImpl();
    if (producer.isPublishTopicNeedUpdate(topic)) {
        if (!producer.getmQClientFactory().updateTopicRouteInfoFromNameServer(topic)) {
            throw new RuntimeException("Fetching the size of rmq partition failed!");
        }
    }
    return producer.getTopicPublishInfoTable().get(topic).getMessageQueueList().size();
}
 
Example #25
Source File: MQClientInstance.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public boolean registerProducer(final String group, final DefaultMQProducerImpl producer) {
    if (null == group || null == producer) {
        return false;
    }

    MQProducerInner prev = this.producerTable.putIfAbsent(group, producer);
    if (prev != null) {
        log.warn("the producer group[{}] exist already.", group);
        return false;
    }

    return true;
}
 
Example #26
Source File: MQClientAPIImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public SendResult sendMessage(//
                              final String addr, // 1
                              final String brokerName, // 2
                              final Message msg, // 3
                              final SendMessageRequestHeader requestHeader, // 4
                              final long timeoutMillis, // 5
                              final CommunicationMode communicationMode, // 6
                              final SendMessageContext context, // 7
                              final DefaultMQProducerImpl producer // 8
) throws RemotingException, MQBrokerException, InterruptedException {
    return sendMessage(addr, brokerName, msg, requestHeader, timeoutMillis, communicationMode, null, null, null, 0, context, producer);
}
 
Example #27
Source File: DefaultMQProducerWithTraceTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws Exception {

    customTraceTopicproducer = new DefaultMQProducer(producerGroupTemp, false, customerTraceTopic);
    normalProducer = new DefaultMQProducer(producerGroupTemp, false, "");
    producer = new DefaultMQProducer(producerGroupTemp, true, "");
    producer.setNamesrvAddr("127.0.0.1:9876");
    normalProducer.setNamesrvAddr("127.0.0.1:9877");
    customTraceTopicproducer.setNamesrvAddr("127.0.0.1:9878");
    message = new Message(topic, new byte[] {'a', 'b', 'c'});
    asyncTraceDispatcher = (AsyncTraceDispatcher) producer.getTraceDispatcher();
    asyncTraceDispatcher.setTraceTopicName(customerTraceTopic);
    asyncTraceDispatcher.getHostProducer();
    asyncTraceDispatcher.getHostConsumer();
    traceProducer = asyncTraceDispatcher.getTraceProducer();

    producer.start();

    Field field = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(producer.getDefaultMQProducerImpl(), mQClientFactory);

    Field fieldTrace = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    fieldTrace.setAccessible(true);
    fieldTrace.set(traceProducer.getDefaultMQProducerImpl(), mQClientFactory);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);

    producer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTemp, producer.getDefaultMQProducerImpl());

    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class))).thenCallRealMethod();
    when(mQClientAPIImpl.sendMessage(anyString(), anyString(), any(Message.class), any(SendMessageRequestHeader.class), anyLong(), any(CommunicationMode.class),
        nullable(SendCallback.class), nullable(TopicPublishInfo.class), nullable(MQClientInstance.class), anyInt(), nullable(SendMessageContext.class), any(DefaultMQProducerImpl.class)))
        .thenReturn(createSendResult(SendStatus.SEND_OK));

}
 
Example #28
Source File: MQClientInstanceTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProducer() {
    boolean flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();

    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isFalse();

    mqClientInstance.unregisterProducer(group);
    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();
}
 
Example #29
Source File: MQClientInstance.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public boolean registerProducer(final String group, final DefaultMQProducerImpl producer) {
    if (null == group || null == producer) {
        return false;
    }

    MQProducerInner prev = this.producerTable.putIfAbsent(group, producer);
    if (prev != null) {
        log.warn("the producer group[{}] exist already.", group);
        return false;
    }

    return true;
}
 
Example #30
Source File: MQClientInstanceTest.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterProducer() {
    boolean flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();

    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isFalse();

    mqClientInstance.unregisterProducer(group);
    flag = mqClientInstance.registerProducer(group, mock(DefaultMQProducerImpl.class));
    assertThat(flag).isTrue();
}