use of org.apache.rocketmq.client.producer.MessageQueueSelector 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);
}
use of org.apache.rocketmq.client.producer.MessageQueueSelector 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();
}
}
use of org.apache.rocketmq.client.producer.MessageQueueSelector in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class AsyncSendExceptionIT method testSendSelectorNull.
@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());
}
use of org.apache.rocketmq.client.producer.MessageQueueSelector in project spring-boot-starter-samples by vindell.
the class OrderedProducer method main.
public static void main(String[] args) throws Exception {
// Instantiate with a producer group name.
MQProducer producer = new DefaultMQProducer("example_group_name");
// Launch the instance.
producer.start();
String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
for (int i = 0; i < 100; i++) {
int orderId = i % 10;
// Create a message instance, specifying topic, tag and message body.
Message msg = new Message("TopicTestjjj", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
Integer id = (Integer) arg;
int index = id % mqs.size();
return mqs.get(index);
}
}, orderId);
System.out.printf("%s%n", sendResult);
}
// server shutdown
producer.shutdown();
}
use of org.apache.rocketmq.client.producer.MessageQueueSelector in project rocketmq by apache.
the class Producer method main.
public static void main(String[] args) throws UnsupportedEncodingException {
try {
MQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
producer.start();
String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
for (int i = 0; i < 100; i++) {
int orderId = i % 10;
Message msg = new Message("TopicTestjjj", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
Integer id = (Integer) arg;
int index = id % mqs.size();
return mqs.get(index);
}
}, orderId);
System.out.printf("%s%n", sendResult);
}
producer.shutdown();
} catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
e.printStackTrace();
}
}
Aggregations