Java Code Examples for org.apache.rocketmq.client.producer.MessageQueueSelector

The following examples show how to use org.apache.rocketmq.client.producer.MessageQueueSelector. These examples are extracted from open source projects. 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
@Override
public void send(MessageProxy messageProxy) throws MQClientException, InterruptedException, RemotingException {
    SendCallback sendCallback = messageProxy.getSendCallback() == null ? new DefaultSendCallback() : messageProxy
            .getSendCallback();
    if (messageProxy.getMessage() == null) {
        throw new NullPointerException("the message is null");
    }
    if (this.isOrderlyMessage()) {
        MessageQueueSelector selector = messageProxy.getMessageQueueSelector();
        if (selector == null) {
            throw new NullPointerException("the sequential message must be configured with MessageQueueSelector.");
        }
        this.defaultMQProducer.send(messageProxy.getMessage(), selector, messageProxy.getSelectorArg(),
                sendCallback);
    } else {
        this.defaultMQProducer.send(messageProxy.getMessage(), sendCallback);
    }
}
 
Example 2
Source Project: rocketmq   Source File: DefaultMQProducerImpl.java    License: Apache License 2.0 6 votes vote down vote up
public void request(final Message msg, final MessageQueueSelector selector, final Object arg,
    final RequestCallback requestCallback, final long timeout)
    throws RemotingException, InterruptedException, MQClientException, MQBrokerException {
    long beginTimestamp = System.currentTimeMillis();
    prepareSendRequest(msg, timeout);
    final String correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);

    final RequestResponseFuture requestResponseFuture = new RequestResponseFuture(correlationId, timeout, requestCallback);
    RequestFutureTable.getRequestFutureTable().put(correlationId, requestResponseFuture);

    long cost = System.currentTimeMillis() - beginTimestamp;
    this.sendSelectImpl(msg, selector, arg, CommunicationMode.ASYNC, new SendCallback() {
        @Override
        public void onSuccess(SendResult sendResult) {
            requestResponseFuture.setSendRequestOk(true);
        }

        @Override
        public void onException(Throwable e) {
            requestResponseFuture.setCause(e);
            requestFail(correlationId);
        }
    }, timeout - cost);

}
 
Example 3
Source Project: DDMQ   Source File: BatchMQProducer.java    License: Apache License 2.0 6 votes vote down vote up
public void send(CarreraRequest request, MessageQueueSelector messageQueueSelector)
    throws RemotingException, MQClientException, InterruptedException, MQBrokerException {
    TopicPublishInfo topicInfo = clusterProducer.getRocketMQProducerByIndex(0).getDefaultMQProducerImpl().getTopicPublishInfoTable().get(request.getTopic());
    if (topicInfo == null || !topicInfo.ok()) { //new topic
        sendSingleMessage(request);
        return;
    }

    MessageQueue mq = messageQueueSelector.select(topicInfo.getMessageQueueList(), null, request);
    request.setMessageQueue(mq);

    requestQMap.computeIfAbsent(mq.getBrokerName(), _name -> {
        Deque<CarreraRequest> q = new ConcurrentLinkedDeque<>();
        addRequestQueue(q, _name, config.getMaxEncodeWorkerForEachBroker());
        return q;
    }).add(request);
}
 
Example 4
Source Project: rocketmq   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void sendOneWay(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.sendOneway(metaqMsg, selector, arg);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
Source Project: DDMQ   Source File: DefaultMQProducerImpl.java    License: Apache License 2.0 5 votes vote down vote up
public void send(Message msg, MessageQueueSelector selector, Object arg, SendCallback sendCallback, long timeout)
    throws MQClientException, RemotingException, InterruptedException {
    try {
        this.sendSelectImpl(msg, selector, arg, CommunicationMode.ASYNC, sendCallback, timeout);
    } catch (MQBrokerException e) {
        throw new MQClientException("unknownn exception", e);
    }
}
 
Example 6
Source Project: DDMQ   Source File: DefaultMQProducerImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * SELECT ONEWAY -------------------------------------------------------
 */
public void sendOneway(Message msg, MessageQueueSelector selector, Object arg)
    throws MQClientException, RemotingException, InterruptedException {
    try {
        this.sendSelectImpl(msg, selector, arg, CommunicationMode.ONEWAY, null, this.defaultMQProducer.getSendMsgTimeout());
    } catch (MQBrokerException e) {
        throw new MQClientException("unknown exception", e);
    }
}
 
Example 7
Source Project: DDMQ   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void asyncSend(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.send(metaqMsg, selector, arg, sendCallback);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Source Project: DDMQ   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void asyncSend(int msgSize, MessageQueueSelector selector) {
    this.msgSize = msgSize;
    for (int i = 0; i < msgSize; i++) {
        Message msg = new Message(topic, RandomUtil.getStringByUUID().getBytes());
        this.asyncSend(msg, selector, i);
    }
}
 
Example 9
Source Project: DDMQ   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void sendOneWay(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.sendOneway(metaqMsg, selector, arg);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
public void asyncSend(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.send(metaqMsg, selector, arg, sendCallback);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source Project: DDMQ   Source File: OneWaySendExceptionIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSendSelectorNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueueSelector selector = null;
    producer.sendOneway(msg, selector, 100);
}
 
Example 12
Source Project: DDMQ   Source File: OneWaySendExceptionIT.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 13
public void sendOneWay(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.sendOneway(metaqMsg, selector, arg);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 14
Source Project: rocketmq   Source File: DefaultMQProducerImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * SELECT ONEWAY -------------------------------------------------------
 */
public void sendOneway(Message msg, MessageQueueSelector selector, Object arg)
    throws MQClientException, RemotingException, InterruptedException {
    try {
        this.sendSelectImpl(msg, selector, arg, CommunicationMode.ONEWAY, null, this.defaultMQProducer.getSendMsgTimeout());
    } catch (MQBrokerException e) {
        throw new MQClientException("unknown exception", e);
    }
}
 
Example 15
Source Project: rocketmq   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void asyncSend(int msgSize, MessageQueueSelector selector) {
    this.msgSize = msgSize;
    for (int i = 0; i < msgSize; i++) {
        Message msg = new Message(topic, RandomUtil.getStringByUUID().getBytes());
        this.asyncSend(msg, selector, i);
    }
}
 
Example 16
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSendSelectorNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueueSelector selector = null;
    producer.send(msg, selector, 100, SendCallBackFactory.getSendCallBack());
}
 
Example 17
Source Project: rocketmq-4.3.0   Source File: DefaultMQProducerImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
     * SELECT ONEWAY -------------------------------------------------------
     */
//
    public void sendOneway(Message msg, MessageQueueSelector selector, Object arg)
        throws MQClientException, RemotingException, InterruptedException {
        try {
            this.sendSelectImpl(msg, selector, arg, CommunicationMode.ONEWAY, null, this.defaultMQProducer.getSendMsgTimeout());
        } catch (MQBrokerException e) {
            throw new MQClientException("unknown exception", e);
        }
    }
 
Example 18
Source Project: rocketmq-4.3.0   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void asyncSend(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.send(metaqMsg, selector, arg, sendCallback);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 19
Source Project: rocketmq-4.3.0   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void asyncSend(int msgSize, MessageQueueSelector selector) {
    this.msgSize = msgSize;
    for (int i = 0; i < msgSize; i++) {
        Message msg = new Message(topic, RandomUtil.getStringByUUID().getBytes());
        this.asyncSend(msg, selector, i);
    }
}
 
Example 20
Source Project: rocketmq-4.3.0   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void sendOneWay(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.sendOneway(metaqMsg, selector, arg);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 21
Source Project: rocketmq   Source File: OneWaySendExceptionIT.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 22
Source Project: rocketmq-4.3.0   Source File: OneWaySendExceptionIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSendSelectorNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueueSelector selector = null;
    producer.sendOneway(msg, selector, 100);
}
 
Example 23
Source Project: rocketmq-4.3.0   Source File: AsyncSendExceptionIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSendSelectorNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueueSelector selector = null;
    producer.send(msg, selector, 100, SendCallBackFactory.getSendCallBack());
}
 
Example 24
Source Project: rocketmq   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void asyncSend(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.send(metaqMsg, selector, arg, sendCallback);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 25
Source Project: rocketmq-read   Source File: DefaultMQProducerImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 带队列选择器的发送
 * @param msg msg
 * @param selector selector
 * @param arg arg
 * @param communicationMode 发送模型
 * @param sendCallback callback
 * @param timeout timeout
 * @return ;
 * @throws MQClientException ;
 * @throws RemotingException ;
 * @throws MQBrokerException ;
 * @throws InterruptedException ;
 */
private SendResult sendSelectImpl(
    Message msg,
    MessageQueueSelector selector,
    Object arg,
    final CommunicationMode communicationMode,
    final SendCallback sendCallback, final long timeout
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {

    long beginStartTime = System.currentTimeMillis();
    this.makeSureStateOK();
    Validators.checkMessage(msg, this.defaultMQProducer);

    TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
    if (topicPublishInfo != null && topicPublishInfo.ok()) {
        MessageQueue mq = null;
        try {
            mq = selector.select(topicPublishInfo.getMessageQueueList(), msg, arg);
        } catch (Throwable e) {
            throw new MQClientException("select message queue throwed exception.", e);
        }

        long costTime = System.currentTimeMillis() - beginStartTime;
        if (timeout < costTime) {
            throw new RemotingTooMuchRequestException("sendSelectImpl call timeout");
        }
        if (mq != null) {
            return this.sendKernelImpl(msg, mq, communicationMode, sendCallback, null, timeout - costTime);
        } else {
            throw new MQClientException("select message queue return null.", null);
        }
    }

    throw new MQClientException("No route info for this topic, " + msg.getTopic(), null);
}
 
Example 26
Source Project: rocketmq-read   Source File: DefaultMQProducerImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * SELECT ONEWAY -------------------------------------------------------
 * oneWay。单队列选择器
 */
public void sendOneway(Message msg, MessageQueueSelector selector, Object arg)
    throws MQClientException, RemotingException, InterruptedException {
    try {
        this.sendSelectImpl(msg, selector, arg, CommunicationMode.ONEWAY, null, this.defaultMQProducer.getSendMsgTimeout());
    } catch (MQBrokerException e) {
        throw new MQClientException("unknown exception", e);
    }
}
 
Example 27
Source Project: rocketmq-read   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void asyncSend(Object msg, MessageQueueSelector selector, Object arg) {
    Message metaqMsg = (Message) msg;
    try {
        producer.send(metaqMsg, selector, arg, sendCallback);
        msgBodys.addData(new String(metaqMsg.getBody()));
        originMsgs.addData(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 28
Source Project: rocketmq-read   Source File: RMQAsyncSendProducer.java    License: Apache License 2.0 5 votes vote down vote up
public void asyncSend(int msgSize, MessageQueueSelector selector) {
    this.msgSize = msgSize;
    for (int i = 0; i < msgSize; i++) {
        Message msg = new Message(topic, RandomUtil.getStringByUUID().getBytes());
        this.asyncSend(msg, selector, i);
    }
}
 
Example 29
@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.send(msg, new MessageQueueSelector() {
        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            String str = null;
            return list.get(str.length());
        }
    }, null, SendCallBackFactory.getSendCallBack());
}
 
Example 30
Source Project: rocketmq-read   Source File: OneWaySendExceptionIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSendSelectorNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueueSelector selector = null;
    producer.sendOneway(msg, selector, 100);
}