Search in sources :

Example 1 with Consumer

use of com.alibaba.middleware.race.mom.Consumer 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 Consumer

use of com.alibaba.middleware.race.mom.Consumer 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 3 with Consumer

use of com.alibaba.middleware.race.mom.Consumer 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)

Example 4 with Consumer

use of com.alibaba.middleware.race.mom.Consumer 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());
    }
}
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 5 with Consumer

use of com.alibaba.middleware.race.mom.Consumer 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();
}
Also used : Consumer(com.alibaba.middleware.race.mom.Consumer) Message(com.alibaba.middleware.race.mom.Message) MessageListener(com.alibaba.middleware.race.mom.MessageListener) ConsumeResult(com.alibaba.middleware.race.mom.ConsumeResult)

Aggregations

ConsumeResult (com.alibaba.middleware.race.mom.ConsumeResult)6 Consumer (com.alibaba.middleware.race.mom.Consumer)6 Message (com.alibaba.middleware.race.mom.Message)6 MessageListener (com.alibaba.middleware.race.mom.MessageListener)6 Producer (com.alibaba.middleware.race.mom.Producer)5 SendResult (com.alibaba.middleware.race.mom.SendResult)5 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1