Search in sources :

Example 1 with Message

use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.

the class MomStressTest 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).equals(v)) {
                    testResult.setSuccess(false);
                    testResult.setInfo("msg " + message.getMsgId() + " expect k" + k + "  value is " + v + ", but actual value is " + message.getProperty(k));
                }
                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;
        }
        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;
        }
    } catch (Exception e) {
        testResult.setSuccess(false);
        testResult.setInfo(e.getMessage());
    }
}
Also used : Consumer(com.alibaba.middleware.race.mom.Consumer) Message(com.alibaba.middleware.race.mom.Message) Producer(com.alibaba.middleware.race.mom.Producer) SendResult(com.alibaba.middleware.race.mom.SendResult) MessageListener(com.alibaba.middleware.race.mom.MessageListener) ConsumeResult(com.alibaba.middleware.race.mom.ConsumeResult)

Example 2 with Message

use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.

the class ProducerTest method main.

public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    System.setProperty("SIP", "127.0.0.1");
    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");
    SendResult result = producer.sendMessage(message);
    if (result.getStatus().equals(SendStatus.SUCCESS)) {
        System.out.println("send success:" + result.getMsgId());
    }
}
Also used : Producer(com.alibaba.middleware.race.mom.Producer) Message(com.alibaba.middleware.race.mom.Message) SendResult(com.alibaba.middleware.race.mom.SendResult)

Example 3 with Message

use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.

the class PubSubTest method main.

public static void main(String[] args) throws InterruptedException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    System.setProperty("SIP", "127.0.0.1");
    // 这些会记录所有的发送记录和消费记录,用于对账
    String toAs = FileUtil.read("toA");
    String toBs = FileUtil.read("toB");
    String toCs = FileUtil.read("toC");
    String amaps = FileUtil.read("amap");
    String bmaps = FileUtil.read("bmap");
    String cmaps = FileUtil.read("cmap");
    if (toAs != null) {
        toA = JSONArray.parseArray(toAs, String.class);
    }
    if (toBs != null) {
        toB = JSONArray.parseArray(toBs, String.class);
    }
    if (toCs != null) {
        toC = JSONArray.parseArray(toCs, String.class);
    }
    if (amaps != null) {
        amap = JSON.parseObject(amaps, ConcurrentHashMap.class);
    }
    if (bmaps != null) {
        bmap = JSON.parseObject(bmaps, ConcurrentHashMap.class);
    }
    if (cmaps != null) {
        cmap = JSON.parseObject(cmaps, ConcurrentHashMap.class);
    }
    Consumer consumerA = (Consumer) Class.forName("com.alibaba.middleware.race.mom.DefaultConsumer").newInstance();
    consumerA.setGroupId(CID + "A");
    // /A 消费所有消息
    consumerA.subscribe(TOPIC, "", new MessageListener() {

        @Override
        public ConsumeResult onMessage(Message message) {
            ConsumeResult result = new ConsumeResult();
            if (!isRunning) {
                return result;
            }
            if (!TOPIC.equals(message.getTopic())) {
                System.out.println("wrong topic-" + message.getTopic());
            }
            result.setStatus(ConsumeStatus.SUCCESS);
            List<Boolean> value = new ArrayList<Boolean>();
            List<Boolean> oldValue = amap.putIfAbsent(message.getProperty(BIZID), value);
            if (oldValue != null) {
                oldValue.add(Boolean.TRUE);
            } else {
                value.add(Boolean.TRUE);
            }
            return result;
        }
    });
    consumerA.start();
    Consumer consumerB = (Consumer) Class.forName("com.alibaba.middleware.race.mom.DefaultConsumer").newInstance();
    consumerB.setGroupId(CID + "B");
    // /B 消费AREA+seed+"="+seed
    consumerB.subscribe(TOPIC, AREA + "=" + seed, new MessageListener() {

        @Override
        public ConsumeResult onMessage(Message message) {
            ConsumeResult result = new ConsumeResult();
            if (!isRunning) {
                return result;
            }
            if (!TOPIC.equals(message.getTopic())) {
                System.out.println("wrong topic-" + message.getTopic());
            }
            if (!seed.equals(message.getProperty(AREA))) {
                System.out.println("wrong area-" + message.getProperty(AREA));
            }
            result.setStatus(ConsumeStatus.SUCCESS);
            List<Boolean> value = new ArrayList<Boolean>();
            List<Boolean> oldValue = bmap.putIfAbsent(message.getProperty(BIZID), value);
            if (oldValue != null) {
                oldValue.add(Boolean.TRUE);
            } else {
                value.add(Boolean.TRUE);
            }
            return result;
        }
    });
    consumerB.start();
    Consumer consumerC = (Consumer) Class.forName("com.alibaba.middleware.race.mom.DefaultConsumer").newInstance();
    consumerC.setGroupId(CID + "C");
    // /C 消费CATID+seed+"="+seed
    consumerC.subscribe(TOPIC, CATID + "=" + seed, new MessageListener() {

        @Override
        public ConsumeResult onMessage(Message message) {
            ConsumeResult result = new ConsumeResult();
            if (!isRunning) {
                return result;
            }
            // 重启broker来观察,是否能否自动重连
            System.out.println(message.getProperty(BIZID));
            if (!TOPIC.equals(message.getTopic())) {
                System.out.println("wrong topic-" + message.getTopic());
            }
            if (!seed.equals(message.getProperty(CATID))) {
                System.out.println("wrong CATID-" + message.getProperty(CATID));
            }
            // 此处注释模拟消费失败
            result.setStatus(ConsumeStatus.FAIL);
            // result.setStatus(ConsumeStatus.SUCCESS);
            List<Boolean> value = new ArrayList<Boolean>();
            List<Boolean> oldValue = cmap.putIfAbsent(message.getProperty(BIZID), value);
            if (oldValue != null) {
                oldValue.add(result.getStatus() == ConsumeStatus.SUCCESS ? true : false);
            } else {
                value.add(result.getStatus() == ConsumeStatus.SUCCESS ? true : false);
            }
            return result;
        }
    });
    // 本行可以注释,来模拟c不在线
    consumerC.start();
    Producer producer = (Producer) Class.forName("com.alibaba.middleware.race.mom.DefaultProducer").newInstance();
    producer.setGroupId(PID);
    producer.setTopic(TOPIC);
    producer.start();
    // 此处控制发送量
    for (int i = 0; i < 10; i++) {
        Message message = new Message();
        message.setBody((BODY + i).getBytes(charset));
        message.setTopic(TOPIC);
        String bizId = UniqId.getInstance().getUniqIDHashString();
        message.setProperty(BIZID, bizId);
        toA.add(bizId);
        if (i % 2 == 0) {
            message.setProperty(AREA, seed);
            toB.add(bizId);
        }
        if (i % 8 == 0) {
            message.setProperty(CATID, seed);
            toC.add(bizId);
        }
        SendResult result = producer.sendMessage(message);
        if (result.getStatus() == SendStatus.FAIL) {
            System.out.println("fail:" + bizId);
        }
    }
    // 改变大小,来模拟客户端在线,broker重启,客户端重连
    Thread.sleep(5000L);
    isRunning = false;
    Thread.sleep(2000L);
    FileUtil.write("toA", JSON.toJSONString(toA));
    FileUtil.write("toB", JSON.toJSONString(toB));
    FileUtil.write("toC", JSON.toJSONString(toC));
    FileUtil.write("amap", JSON.toJSONString(amap));
    FileUtil.write("bmap", JSON.toJSONString(bmap));
    FileUtil.write("cmap", JSON.toJSONString(cmap));
    System.out.println(toA.size() + "--A--" + amap.size());
    System.out.println(toB.size() + "--B--" + bmap.size());
    System.out.println(toC.size() + "--C--" + cmap.size());
    for (String string : toA) {
        if (!amap.containsKey(string)) {
            System.out.println("a miss " + string);
        } else if (amap.get(string).size() > 1) {
            List<Boolean> ackList = amap.get(string);
            for (int i = 0; i < ackList.size(); i++) {
                if (ackList.get(i).booleanValue() && i < ackList.size() - 1) {
                    System.out.println("a reapeat  " + string);
                }
            }
        }
    }
    for (String string : toB) {
        if (!bmap.containsKey(string)) {
            System.out.println("b miss " + string);
        } else if (bmap.get(string).size() > 1) {
            List<Boolean> ackList = bmap.get(string);
            for (int i = 0; i < ackList.size(); i++) {
                if (ackList.get(i).booleanValue() && i < ackList.size() - 1) {
                    System.out.println("b reapeat  " + string);
                }
            }
        }
    }
    for (String string : toC) {
        if (!cmap.containsKey(string)) {
            System.out.println("c miss " + string);
        } else if (cmap.get(string).size() > 1) {
            List<Boolean> ackList = cmap.get(string);
            for (int i = 0; i < ackList.size(); i++) {
                if (ackList.get(i).booleanValue() && i < ackList.size() - 1) {
                    System.out.println("c reapeat  " + string);
                }
            }
        }
    }
    System.out.println("finish ");
    System.exit(0);
}
Also used : Message(com.alibaba.middleware.race.mom.Message) MessageListener(com.alibaba.middleware.race.mom.MessageListener) Consumer(com.alibaba.middleware.race.mom.Consumer) Producer(com.alibaba.middleware.race.mom.Producer) SendResult(com.alibaba.middleware.race.mom.SendResult) ArrayList(java.util.ArrayList) List(java.util.List) ConsumeResult(com.alibaba.middleware.race.mom.ConsumeResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with Message

use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.

the class MsgFileUtil method main.

public static void main(String[] args) throws Exception {
    Message msg = new Message();
    // msg.setMsgId("msgID");
    msg.setBody("dddddfgf".getBytes());
    msg.setBornTime(System.currentTimeMillis());
    long startTime = System.currentTimeMillis();
    new MsgFileUtil().setMsg("topic", "queueId", null, msg);
    long endTime = System.currentTimeMillis();
    System.out.println("统计 存一条数据 时间 endTime-startTime= " + (endTime - startTime));
    startTime = System.currentTimeMillis();
    List<Message> msgList = new MsgFileUtil().getMsg("topic", "queueId", null, 0L);
    endTime = System.currentTimeMillis();
    System.out.println("统计 取一条数据 时间 endTime-startTime= " + (endTime - startTime));
    // test
    System.out.println("main " + msgList.get(0).toString());
}
Also used : Message(com.alibaba.middleware.race.mom.Message)

Example 5 with Message

use of com.alibaba.middleware.race.mom.Message in project alibaba-mom by younfor.

the class MomFuntionTest method testBasic.

private static void testBasic() {
    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());
                }
                if (System.currentTimeMillis() - message.getBornTime() > 1000) {
                    testResult.setSuccess(false);
                    testResult.setInfo("msg " + message.getMsgId() + " delay " + (System.currentTimeMillis() - message.getBornTime()) + " ms");
                }
                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("area", "hz" + code);
        SendResult result = producer.sendMessage(msg);
        if (result.getStatus() != SendStatus.SUCCESS) {
            testResult.setSuccess(false);
            testResult.setInfo(result.toString());
            return;
        }
        sendMsg.add(result.getMsgId());
        Thread.sleep(5000);
        if (!testResult.isSuccess()) {
            System.out.println(testResult);
            return;
        }
        checkMsg(sendMsg, recvMsg);
        producer.stop();
        consumer.stop();
    } catch (Exception e) {
        testResult.setSuccess(false);
        testResult.setInfo(e.getMessage());
    }
}
Also used : Consumer(com.alibaba.middleware.race.mom.Consumer) Message(com.alibaba.middleware.race.mom.Message) Producer(com.alibaba.middleware.race.mom.Producer) SendResult(com.alibaba.middleware.race.mom.SendResult) MessageListener(com.alibaba.middleware.race.mom.MessageListener) ConsumeResult(com.alibaba.middleware.race.mom.ConsumeResult)

Aggregations

Message (com.alibaba.middleware.race.mom.Message)12 Producer (com.alibaba.middleware.race.mom.Producer)7 SendResult (com.alibaba.middleware.race.mom.SendResult)7 ConsumeResult (com.alibaba.middleware.race.mom.ConsumeResult)6 Consumer (com.alibaba.middleware.race.mom.Consumer)6 MessageListener (com.alibaba.middleware.race.mom.MessageListener)6 TopicAndFilter (com.alibaba.middleware.race.mom.util.TopicAndFilter)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 SendCallback (com.alibaba.middleware.race.mom.SendCallback)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1