Java Code Examples for io.openmessaging.MessagingAccessPoint#createConsumer()

The following examples show how to use io.openmessaging.MessagingAccessPoint#createConsumer() . 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: BatchConsumer.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    KeyValue keyValue = OMS.newKeyValue();
    keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token");

    MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue);

    Consumer consumer = messagingAccessPoint.createConsumer();

    consumer.bindQueue("test_topic_0", new BatchMessageListener() {
        @Override
        public void onReceived(List<Message> messages, Context context) {
            for (Message message : messages) {
                System.out.println(String.format("onReceived, message: %s", message));
            }

            // 代表这一批消息的ack
            context.ack();
        }
    });

    consumer.start();
    System.in.read();
}
 
Example 2
Source File: ConsoleConsumer.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
protected static Consumer buildConsumer(ConsoleConsumerConfig config) {
    KeyValue keyValue = OMS.newKeyValue();
    keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, config.getToken());
    keyValue.put(JoyQueueNameServerBuiltinKeys.NAMESPACE, StringUtils.defaultIfBlank(config.getNamespace(), ToolConsts.DEFAULT_NAMESPACE));

    for (Map.Entry<String, String> entry : config.getParams().entrySet()) {
        keyValue.put(entry.getKey(), entry.getValue());
    }

    MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint(
            String.format("oms:%s://%s@%s/%s",
                    ToolConsts.DRIVER, config.getApp(), config.getBootstrap(), StringUtils.defaultIfBlank(config.getRegion(), ToolConsts.DEFAULT_REGION)), keyValue);
    return messagingAccessPoint.createConsumer();
}
 
Example 3
Source File: TopicMsgFilterServiceImpl.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private ExtensionConsumer initConsumer(TopicMsgFilter msgFilter) {
    final String url = String.format(urlFormat, msgFilter.getApp(), msgFilter.getBrokerAddr());
    KeyValue keyValue = OMS.newKeyValue();
    keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, msgFilter.getToken());
    keyValue.put(JoyQueueBuiltinKeys.IO_THREADS, 1);
    keyValue.put(JoyQueueBuiltinKeys.BATCH_SIZE, 100);
    MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint(url, keyValue);
    ExtensionConsumer consumer = (ExtensionConsumer) messagingAccessPoint.createConsumer();
    consumer.bindQueue(msgFilter.getTopic());
    return consumer;
}
 
Example 4
Source File: ReceiveConsumer.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    KeyValue keyValue = OMS.newKeyValue();
    keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token");

    MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue);

    Consumer consumer = messagingAccessPoint.createConsumer();
    consumer.start();

    // 绑定主题,将要消费的主题
    consumer.bindQueue("test_topic_0");

    while (true) {
        System.out.println("doReceive");
        // 拉取单条消息
        // 参数是超时时间,只是网络请求的超时
        Message message = consumer.receive(1000 * 10);

        // 没有拉取到,继续循环
        if (message == null) {
            continue;
        }

        // 拉取到消息,打印日志并ack
        System.out.println(String.format("receive, message: %s", message));
        consumer.ack(message.getMessageReceipt());

        Thread.currentThread().sleep(1000 * 1);
    }
}
 
Example 5
Source File: BatchReceiveConsumer.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    KeyValue keyValue = OMS.newKeyValue();
    keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token");

    MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue);

    Consumer consumer = messagingAccessPoint.createConsumer();
    consumer.start();

    // 绑定主题,将要消费的主题
    consumer.bindQueue("test_topic_0");

    // 这里只是简单例子,根据具体情况进行调度处理
    // 参数是超时时间,只是网络请求的超时
    while (true) {
        System.out.println("doReceive");

        // 批量拉取消息
        List<Message> messages = consumer.batchReceive(1000 * 10);

        if (CollectionUtils.isEmpty(messages)) {
            continue;
        }

        for (Message message : messages) {
            // 拉取到消息,打印日志并ack
            System.out.println(String.format("receive, message: %s", messages));
            consumer.ack(message.getMessageReceipt());
        }

        Thread.currentThread().sleep(1000 * 1);
    }
}
 
Example 6
Source File: SimpleConsumer.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final String app = "test_app";
    final String token = "some token";
    final String dataCenter = "default";
    final String brokerAddr = "127.0.0.1:50088";
    final String topic = "test_topic_0";
    // oms:joyqueue://[email protected]:50088/default
    final String url = "oms:joyqueue://" + app +  "@" + brokerAddr + "/" + dataCenter;

    KeyValue keyValue = OMS.newKeyValue();
    keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, token);

    MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint(url, keyValue);

    // 创建consumer实例
    Consumer consumer = messagingAccessPoint.createConsumer();

    // 绑定需要消费的topic和对应的listener
    consumer.bindQueue(topic, new MessageListener() {
        @Override
        public void onReceived(Message message, Context context) {
            System.out.println(String.format("onReceived, message: %s", message));

            // 确认消息消费成功,如果没有确认或抛出异常会进入重试队列
            context.ack();
        }
    });

    consumer.start();
    System.in.read();
}
 
Example 7
Source File: PartitionIndexReceiveConsumer.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        KeyValue keyValue = OMS.newKeyValue();
        keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token");

        MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue);

        // 首先需要对consumer强制转型
        ExtensionConsumer extensionConsumer = (ExtensionConsumer) messagingAccessPoint.createConsumer();
        extensionConsumer.start();

        // 绑定主题,将要消费的主题
        extensionConsumer.bindQueue("test_topic_0");

        // 获取元数据
        QueueMetaData queueMetaData = extensionConsumer.getQueueMetaData("test_topic_0");
        Map<Integer, Long> indexMapper = Maps.newHashMap();

        // 这里只是简单例子,根据具体情况进行调度处理
        while (true) {
            // 循环所有partition,并拉取相应消息
            for (QueueMetaData.Partition partition : queueMetaData.partitions()) {
                // 初始化index信息
                Long index = indexMapper.get(partition.partitionId());
                if (index == null) {
                    index = 0L;
                    indexMapper.put(partition.partitionId(), index);
                }

                System.out.println("doReceive");

                // 拉取分区单条消息
                // 参数是超时时间,只是网络请求的超时
                Message message = extensionConsumer.receive((short) partition.partitionId(), index, 1000 * 10);
                // 批量拉取的方式相同,不单独列出
//                List<Message> messages = extensionConsumer.batchReceive((short) partition.partitionId(), index, 1000 * 10);

                // 没有拉取到,继续循环
                if (message == null) {
                    continue;
                }

                // 拉取到消息,打印日志并ack
                System.out.println(String.format("receive, message: %s", message));
                extensionConsumer.ack(message.getMessageReceipt());

                // 更新index到下一个消息处
                index = message.extensionHeader().get().getOffset() + 1;
                indexMapper.put(partition.partitionId(), index);
            }

            Thread.currentThread().sleep(1000 * 1);
        }
    }
 
Example 8
Source File: PartitionReceiveConsumer.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
        KeyValue keyValue = OMS.newKeyValue();
        keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token");

        MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue);

        // 首先需要对consumer强制转型
        ExtensionConsumer extensionConsumer = (ExtensionConsumer) messagingAccessPoint.createConsumer();
        extensionConsumer.start();

        // 绑定主题,将要消费的主题
        extensionConsumer.bindQueue("test_topic_0");

        // 获取元数据
        QueueMetaData queueMetaData = extensionConsumer.getQueueMetaData("test_topic_0");

        // 这里只是简单例子,根据具体情况进行调度处理
        while (true) {
            // 循环所有partition,并拉取相应消息
            for (QueueMetaData.Partition partition : queueMetaData.partitions()) {
                System.out.println("doReceive");

                // 拉取分区单条消息
                // 参数是超时时间,只是网络请求的超时
                Message message = extensionConsumer.receive((short) partition.partitionId(), 1000 * 10);

                // 批量拉取的方式相同,不单独列出
//                List<Message> messages = extensionConsumer.batchReceive((short) partition.partitionId(), 1000 * 10);

                // 没有拉取到,继续循环
                if (message == null) {
                    continue;
                }

                // 拉取到消息,打印日志并ack
                System.out.println(String.format("receive, message: %s", message));
                extensionConsumer.ack(message.getMessageReceipt());
            }

            Thread.currentThread().sleep(1000 * 1);
        }
    }