io.openmessaging.PullConsumer Java Examples

The following examples show how to use io.openmessaging.PullConsumer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsumerTester.java    From coding-snippets with MIT License 6 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.demo.DefaultPullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    //init offsets
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #2
Source File: ConsumerTester.java    From OpenMessageShaping with MIT License 6 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.demo.DefaultPullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    //init offsets
    offsets.put(queue, new HashMap<>());
    for (String topic : topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map : offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #3
Source File: SimplePullConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory
        .getMessagingAccessPoint("openmessaging:rocketmq://IP1:9876,IP2:9876/namespace");

    final PullConsumer consumer = messagingAccessPoint.createPullConsumer("OMS_HELLO_TOPIC",
        OMS.newKeyValue().put(NonStandardKeys.CONSUMER_GROUP, "OMS_CONSUMER"));

    messagingAccessPoint.startup();
    System.out.printf("MessagingAccessPoint startup OK%n");

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            consumer.shutdown();
            messagingAccessPoint.shutdown();
        }
    }));

    consumer.startup();
    System.out.printf("Consumer startup OK%n");

    while (true) {
        Message message = consumer.poll();
        if (message != null) {
            String msgId = message.headers().getString(MessageHeader.MESSAGE_ID);
            System.out.printf("Received one message: %s%n", msgId);
            consumer.ack(msgId);
        }
    }
}
 
Example #4
Source File: V7ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.v7.V7PullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
        	//"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #5
Source File: V1ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.v1.V1PullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
        	//"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #6
Source File: ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.demo.DefaultPullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
        	//"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #7
Source File: V6ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.v6.V6PullConsumerNoStr");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
        	//"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #8
Source File: V4ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.v4.V4PullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
        	//"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #9
Source File: V2ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.v2.V2PullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
        	//"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #10
Source File: V5ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.v5.V5PullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
        	//"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #11
Source File: V3ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init(){
    try {
        Class kvClass=Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue=(KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.v2.V2PullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer==null){
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue,topics);
    } catch (Exception e) {
        logger.error("please check the package name and class:",e);
    }

    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
            //"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #12
Source File: ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void init() {
    //init consumer
    try {
        Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue");
        KeyValue keyValue = (KeyValue) kvClass.newInstance();
        keyValue.put("STORE_PATH", Constants.STORE_PATH);
        Class consumerClass = Class.forName("io.openmessaging.demo.DefaultPullConsumer");
        consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue});
        if (consumer == null) {
            throw new InstantiationException("Init Producer Failed");
        }
        consumer.attachQueue(queue, topics);
    } catch (Exception e) {
        logger.error("please check the package name and class name:", e);
    }
    /**
     * init offsets,为了检验正确性和顺序。
     * key为本线程消费的queue/topic,
     * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。
     */
    offsets.put(queue, new HashMap<>());
    for (String topic: topics) {
        offsets.put(topic, new HashMap<>());
    }
    for (Map<String, Integer> map: offsets.values()) {
        for (int i = 0; i < Constants.PRO_NUM; i++) {
        	//"PRODUCER_i",即生产者线程名
            map.put(Constants.PRO_PRE + i, 0);
        }
    }
}
 
Example #13
Source File: SimplePullConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory
        .getMessagingAccessPoint("openmessaging:rocketmq://IP1:9876,IP2:9876/namespace");

    final PullConsumer consumer = messagingAccessPoint.createPullConsumer("OMS_HELLO_TOPIC",
        OMS.newKeyValue().put(NonStandardKeys.CONSUMER_GROUP, "OMS_CONSUMER"));

    messagingAccessPoint.startup();
    System.out.printf("MessagingAccessPoint startup OK%n");

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            consumer.shutdown();
            messagingAccessPoint.shutdown();
        }
    }));

    consumer.startup();
    System.out.printf("Consumer startup OK%n");

    while (true) {
        Message message = consumer.poll();
        if (message != null) {
            String msgId = message.headers().getString(MessageHeader.MESSAGE_ID);
            System.out.printf("Received one message: %s%n", msgId);
            consumer.ack(msgId);
        }
    }
}
 
Example #14
Source File: SimplePullConsumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory
        .getMessagingAccessPoint("openmessaging:rocketmq://IP1:9876,IP2:9876/namespace");

    final PullConsumer consumer = messagingAccessPoint.createPullConsumer("OMS_HELLO_TOPIC",
        OMS.newKeyValue().put(NonStandardKeys.CONSUMER_GROUP, "OMS_CONSUMER"));

    messagingAccessPoint.startup();
    System.out.printf("MessagingAccessPoint startup OK%n");

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            consumer.shutdown();
            messagingAccessPoint.shutdown();
        }
    }));

    consumer.startup();
    System.out.printf("Consumer startup OK%n");

    while (true) {
        Message message = consumer.poll();
        if (message != null) {
            String msgId = message.headers().getString(MessageHeader.MESSAGE_ID);
            System.out.printf("Received one message: %s%n", msgId);
            consumer.ack(msgId);
        }
    }
}
 
Example #15
Source File: MessagingAccessPointImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Override
public PullConsumer createPullConsumer(String queueName, KeyValue properties) {
    return new PullConsumerImpl(queueName, OMSUtil.buildKeyValue(this.accessPointProperties, properties));
}
 
Example #16
Source File: MessagingAccessPointImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Override
public PullConsumer createPullConsumer(String queueName) {
    return new PullConsumerImpl(queueName, accessPointProperties);
}
 
Example #17
Source File: MessagingAccessPointImpl.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public PullConsumer createPullConsumer(String queueName, KeyValue properties) {
    return new PullConsumerImpl(queueName, OMSUtil.buildKeyValue(this.accessPointProperties, properties));
}
 
Example #18
Source File: MessagingAccessPointImpl.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public PullConsumer createPullConsumer(String queueName) {
    return new PullConsumerImpl(queueName, accessPointProperties);
}
 
Example #19
Source File: MessagingAccessPointImpl.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public PullConsumer createPullConsumer(String queueName, KeyValue properties) {
    return new PullConsumerImpl(queueName, OMSUtil.buildKeyValue(this.accessPointProperties, properties));
}
 
Example #20
Source File: MessagingAccessPointImpl.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public PullConsumer createPullConsumer(String queueName) {
    return new PullConsumerImpl(queueName, accessPointProperties);
}