Search in sources :

Example 1 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class OneWaySendExceptionIT method testSelectorThrowsException.

@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSelectorThrowsException() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    producer.sendOneway(msg, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            String str = null;
            return list.get(str.length());
        }
    }, null);
}
Also used : MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) Message(org.apache.rocketmq.common.message.Message) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Test(org.junit.Test)

Example 2 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class OneWaySendExceptionIT method testSendMQNull.

@Test(expected = java.lang.NullPointerException.class)
public void testSendMQNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueue messageQueue = null;
    producer.sendOneway(msg, messageQueue);
}
Also used : Message(org.apache.rocketmq.common.message.Message) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Test(org.junit.Test)

Example 3 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class CheckMsgSendRTCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQProducer producer = new DefaultMQProducer(rpcHook);
    producer.setProducerGroup(Long.toString(System.currentTimeMillis()));
    try {
        producer.start();
        long start = 0;
        long end = 0;
        long timeElapsed = 0;
        boolean sendSuccess = false;
        String topic = commandLine.getOptionValue('t').trim();
        long amount = !commandLine.hasOption('a') ? 100 : Long.parseLong(commandLine.getOptionValue('a').trim());
        long msgSize = !commandLine.hasOption('s') ? 128 : Long.parseLong(commandLine.getOptionValue('s').trim());
        Message msg = new Message(topic, getStringBySize(msgSize).getBytes(MixAll.DEFAULT_CHARSET));
        System.out.printf("%-32s  %-4s  %-20s    %s%n", "#Broker Name", "#QID", "#Send Result", "#RT");
        for (int i = 0; i < amount; i++) {
            start = System.currentTimeMillis();
            try {
                producer.send(msg, new MessageQueueSelector() {

                    @Override
                    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                        int queueIndex = (Integer) arg % mqs.size();
                        MessageQueue queue = mqs.get(queueIndex);
                        brokerName = queue.getBrokerName();
                        queueId = queue.getQueueId();
                        return queue;
                    }
                }, i);
                sendSuccess = true;
                end = System.currentTimeMillis();
            } catch (Exception e) {
                sendSuccess = false;
                end = System.currentTimeMillis();
            }
            if (i != 0) {
                timeElapsed += end - start;
            }
            System.out.printf("%-32s  %-4s  %-20s    %s%n", brokerName, queueId, sendSuccess, end - start);
        }
        double rt = (double) timeElapsed / (amount - 1);
        System.out.printf("Avg RT: %s%n", String.format("%.2f", rt));
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        producer.shutdown();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue)

Example 4 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageAsync_Success.

@Test
public void testSendMessageAsync_Success() throws RemotingException, InterruptedException, MQBrokerException {
    doNothing().when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult).isNull();
    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(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()));
    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);
}
Also used : InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 5 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class TestProducer method main.

public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();
    for (int i = 0; i < 1; i++) try {
        {
            Message msg = new Message("TopicTest1", "TagA", "key113", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
            QueryResult queryMessage = producer.queryMessage("TopicTest1", "key113", 10, 0, System.currentTimeMillis());
            for (MessageExt m : queryMessage.getMessageList()) {
                System.out.printf("%s%n", m);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    producer.shutdown();
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) QueryResult(org.apache.rocketmq.client.QueryResult) Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)100 Message (org.apache.rocketmq.common.message.Message)68 SendResult (org.apache.rocketmq.client.producer.SendResult)41 MQClientException (org.apache.rocketmq.client.exception.MQClientException)31 Test (org.junit.Test)28 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)13 ArrayList (java.util.ArrayList)12 SendCallback (org.apache.rocketmq.client.producer.SendCallback)11 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)8 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 MQProducer (org.apache.rocketmq.client.producer.MQProducer)6 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Date (java.util.Date)3 CommandLine (org.apache.commons.cli.CommandLine)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2