use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class Producer method main.
public static void main(String[] args) throws MQClientException, InterruptedException {
CommandLine commandLine = buildCommandline(args);
if (commandLine != null) {
String group = commandLine.getOptionValue('g');
String topic = commandLine.getOptionValue('t');
String tags = commandLine.getOptionValue('a');
String keys = commandLine.getOptionValue('k');
String msgCount = commandLine.getOptionValue('c');
DefaultMQProducer producer = new DefaultMQProducer(group);
producer.setInstanceName(Long.toString(System.currentTimeMillis()));
producer.start();
for (int i = 0; i < Integer.parseInt(msgCount); i++) {
try {
Message msg = new Message(topic, tags, keys, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg);
System.out.printf("%-8d %s%n", i, sendResult);
} catch (Exception e) {
e.printStackTrace();
Thread.sleep(1000);
}
}
producer.shutdown();
}
}
use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class Producer method main.
public static void main(String[] args) throws UnsupportedEncodingException {
try {
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
// 指定NameServer地址
producer.setNamesrvAddr("127.0.0.1:9876");
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();
}
}
use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class AsyncSendExceptionIT method testSendCallBackNull.
@Test
public void testSendCallBackNull() throws Exception {
Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
SendCallback sendCallback = null;
producer.send(msg, sendCallback);
}
use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class AsyncSendExceptionIT 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.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());
}
use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.
the class AsyncSendExceptionIT method testQueueIdSmallZero.
@Test
public void testQueueIdSmallZero() throws Exception {
int queueId = -100;
sendFail = true;
MessageQueue mq = new MessageQueue(topic, broker1Name, queueId);
Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
producer.send(msg, mq, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
sendFail = false;
}
@Override
public void onException(Throwable throwable) {
sendFail = true;
}
});
int checkNum = 50;
while (sendFail && checkNum > 0) {
checkNum--;
TestUtils.waitForMoment(100);
}
producer.shutdown();
assertThat(sendFail).isEqualTo(false);
}
Aggregations