use of com.alibaba.middleware.race.mom.SendResult in project alibaba-mom by younfor.
the class ProducerAsyncTest method main.
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Producer producer = (Producer) Class.forName("com.alibaba.middleware.race.mom.DefaultProducer").newInstance();
producer.setGroupId("PG-test");
producer.setTopic("T-test");
producer.start();
Message message = new Message();
message.setBody("Hello MOM".getBytes());
message.setProperty("area", "us");
// 调用此方法,当前线程不阻塞
producer.asyncSendMessage(message, new SendCallback() {
@Override
public void onResult(SendResult result) {
if (result.getStatus().equals(SendStatus.SUCCESS)) {
System.out.println("send success:" + result.getMsgId());
}
}
});
}
use of com.alibaba.middleware.race.mom.SendResult in project alibaba-mom by younfor.
the class MomStressTest method testBasic.
private static void testBasic() {
final int code = random.nextInt(100000);
final ConsumeResult consumeResult = new ConsumeResult();
consumeResult.setStatus(ConsumeStatus.SUCCESS);
final String topic = TOPIC + code;
try {
String ip = System.getProperty("SIP");
Consumer consumer = (Consumer) Class.forName("com.alibaba.middleware.race.mom.DefaultConsumer").newInstance();
consumer.setGroupId(CID + code);
consumer.subscribe(topic, "", new MessageListener() {
@Override
public ConsumeResult onMessage(Message message) {
if (!message.getTopic().equals(topic)) {
testResult.setSuccess(false);
testResult.setInfo("expect topic:" + topic + ", actual topic:" + message.getTopic());
}
long delay = System.currentTimeMillis() - message.getBornTime();
/* if (delay>1000) {
testResult.setSuccess(false);
testResult.setInfo("msg "+message.getMsgId()+" delay "+(System.currentTimeMillis()-message.getBornTime())+" ms");
}*/
totalDelay.addAndGet(delay);
recvCount.incrementAndGet();
return consumeResult;
}
});
consumer.start();
final Producer producer = (Producer) Class.forName("com.alibaba.middleware.race.mom.DefaultProducer").newInstance();
producer.setGroupId(PID + code);
producer.setTopic(topic);
producer.start();
long start = System.currentTimeMillis();
for (int i = 0; i < c; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
Message msg = new Message();
msg.setBody(BODY.getBytes(charset));
msg.setProperty("area", "hz" + code);
final long startRt = System.currentTimeMillis();
SendResult result = producer.sendMessage(msg);
if (result.getStatus() == SendStatus.SUCCESS) {
sendCount.incrementAndGet();
totalRT.addAndGet(System.currentTimeMillis() - startRt);
}
/*producer.asyncSendMessage(msg, new SendCallback() {
@Override
public void onResult(SendResult result) {
if (result.getStatus()==SendStatus.SUCCESS) {
sendCount.incrementAndGet();
totalRT.addAndGet(System.currentTimeMillis()-startRt);
}
}
});*/
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
Thread.sleep(30000);
if (!testResult.isSuccess()) {
return;
}
long totalTime = System.currentTimeMillis() - start;
long sendTps = sendCount.get() * 1000 / totalTime;
long recvTps = recvCount.get() * 1000 / totalTime;
long rt = totalRT.get() / sendCount.get();
long delay = totalDelay.get() / recvCount.get();
testResult.setInfo("send tps:" + sendTps + ", recv tps:" + recvTps + ", send rt:" + rt + ", avg delay:" + delay);
} catch (Exception e) {
testResult.setSuccess(false);
testResult.setInfo(e.getMessage());
}
}
Aggregations