use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.
the class MomFuntionTest method testFilter.
private static void testFilter() {
int code = random.nextInt(100000);
final ConsumeResult consumeResult = new ConsumeResult();
consumeResult.setStatus(ConsumeStatus.SUCCESS);
final String topic = TOPIC + code;
final String k = AREA + code;
final String v = "hz_" + 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, k + "=" + v, 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());
}
if (System.currentTimeMillis() - message.getBornTime() > 1000) {
testResult.setSuccess(false);
testResult.setInfo("msg " + message.getMsgId() + " delay " + (System.currentTimeMillis() - message.getBornTime()) + " ms");
}
if (message.getProperty(k) == null || !message.getProperty(k).equals(v)) {
testResult.setSuccess(false);
testResult.setInfo("msg " + message.getMsgId() + " expect k" + k + " value is " + v + ", but actual value is " + message.getProperty(k));
}
recvMsg.add(message.getMsgId());
return consumeResult;
}
});
consumer.start();
Producer producer = (Producer) Class.forName("com.alibaba.middleware.race.mom.DefaultProducer").newInstance();
producer.setGroupId(PID + code);
producer.setTopic(topic);
producer.start();
Message msg = new Message();
msg.setBody(BODY.getBytes(charset));
msg.setProperty(k, v);
SendResult result = producer.sendMessage(msg);
if (result.getStatus() != SendStatus.SUCCESS) {
testResult.setSuccess(false);
testResult.setInfo(result.toString());
return;
}
sendMsg.add(result.getMsgId());
msg = new Message();
msg.setBody(BODY.getBytes(charset));
result = producer.sendMessage(msg);
if (result.getStatus() != SendStatus.SUCCESS) {
testResult.setSuccess(false);
testResult.setInfo(result.toString());
return;
}
Thread.sleep(5000);
if (!testResult.isSuccess()) {
return;
}
checkMsg(sendMsg, recvMsg);
return;
} catch (Exception e) {
testResult.setSuccess(false);
testResult.setInfo(e.getMessage());
}
}
use of com.alibaba.middleware.race.mom.Message 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.Message in project alibaba-mom by younfor.
the class ConusmerTest method main.
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Consumer consumer = (Consumer) Class.forName("com.alibaba.middleware.race.mom.DefaultConsumer").newInstance();
// 设置消费者id,groupid相同的消费者,broker会视为同一个消费者集群,每条消息只会投递给集群中的一台机器
consumer.setGroupId("CG-test");
// 发起订阅操作,broker只能投递 topic为T-test,并且area属性为us的消息给消费者
consumer.subscribe("T-test", "area=us", /*如果改属性为null或者空串,那么表示接收这个topic下的所有消息*/
new MessageListener() {
@Override
public ConsumeResult onMessage(Message message) {
assert "T-test".equals(message.getTopic()) && "us".equals(message.getProperty("area"));
System.out.println("consume success:" + message.getMsgId());
ConsumeResult result = new ConsumeResult();
// 设置消费结果,如果成功,那么broker不再投递
result.setStatus(ConsumeStatus.SUCCESS);
// result.setInfo("fail detail or reason");
return result;
}
});
consumer.start();
}
use of com.alibaba.middleware.race.mom.Message 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());
}
}
use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.
the class MessageManager method recieveMsg.
// public static Map<String/* TopicAndFilter+msgOffset */,MessageInfo> messageCacheMap;
/**
* 每个集群有独立的消费进度
* 消息再broker收到后,按照TopicAndFilter 消息分类,每种TopicAndFilter消息有独立的offsetProducer
* 如果要以queueId分多文件,那么再分一层独立的offsetProducer
*/
// private MsgStore mstore = /* new MsgStoreImp();// */new MsgStoreImp_MappedBuffer();
// private FSTConfiguration fst = FSTConfiguration.getDefaultConfiguration();
// private Map<String/* topic@group */,BlockingQueue<String /*id*/>/*reSendMsgQueue*/>topicAndgroup2reSendMsgQueue=new HashMap<>();
// public MessageManager(ConcurrentHashMap<String/* TopicAndFilter+msgOffset */,Message> recoverMsgMap){
// if(recoverMsgMap!=null){
// messageCacheMap=recoverMsgMap;
// }else{
// messageCacheMap=new ConcurrentHashMap<>();
// }
// }
/**
* 打包消息处理
* @param msgSend
* @param producer
*/
public static void recieveMsg(MessageSend msgSend, ChannelHandlerContext producer) {
long start = System.currentTimeMillis();
/*
* 存储处理
*/
msgSend.setProducer(producer.channel());
TopicAndFilter topicAndFilter = new TopicAndFilter(msgSend.getTopic(), msgSend.getProperties());
String[] queueIdAndOffsetArray = ProducerGroup.storeMsg(topicAndFilter, msgSend).split(" ");
int queueIndex = Integer.parseInt(queueIdAndOffsetArray[0]);
int offset = Integer.parseInt(queueIdAndOffsetArray[1]);
logger.debug("currentCanUseOffset " + offset);
/*
* 消息进入待发送队列,等待存储完成()
*/
int i = 0;
for (byte[] body : msgSend.getBodys()) {
Message msg = new Message(msgSend.getTopic(), body, msgSend.getProperties(), msgSend.getBornTime());
msg.setMsgId(msgSend.getSendIds().get(i));
MessageInfo msgInfo = new MessageInfo(msg, producer.channel());
msgInfo.setQueueId(queueIndex);
msgInfo.setOffset(offset++);
msgInfo.setMsgInfoId(topicAndFilter.toString() + queueIndex + "" + String.valueOf(msgInfo.getOffset()));
/*
* 发送处理,加入发送队列,等待存储完成
*/
ConsumerGroup.sendMsg(topicAndFilter, msgInfo);
i++;
}
// logger.error("消息born 到生产者recv处理完毕 cost:"+(System.currentTimeMillis()-msgSend.getBornTime()));
// logger.error("消息recv生产者recv处理完毕 cost:"+(System.currentTimeMillis()-start));
}
Aggregations