Java Code Examples for io.openmessaging.OMS#newKeyValue()

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

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

    Producer producer = messagingAccessPoint.createProducer();
    producer.start();

    QueueMetaData queueMetaData = producer.getQueueMetaData("test_topic_0");
    for (QueueMetaData.Partition partition : queueMetaData.partitions()) {
        System.out.println(String.format("partition: %s, partitionHost: %s", partition.partitionId(), partition.partitonHost()));
    }

    Message message = producer.createMessage("test_topic_0", "body".getBytes());

    SendResult sendResult = producer.send(message);
    System.out.println(String.format("messageId: %s", sendResult.messageId()));
}
 
Example 3
Source File: AsyncBatchProducer.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
        KeyValue keyValue = OMS.newKeyValue();
        keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token");

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

        Producer producer = messagingAccessPoint.createProducer();
        producer.start();

        List<Message> messages = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Message message = producer.createMessage("test_topic_0", "body".getBytes());
            messages.add(message);
        }

        Future<SendResult> future = producer.sendAsync(messages);

//        future.addListener(new FutureListener<SendResult>() {
//            @Override
//            public void operationComplete(Future<SendResult> future) {
//                System.out.println(future.get().messageId());
//            }
//        });
        System.out.println(future.get().messageId());
    }
 
Example 4
Source File: KeyValueConverter.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static KeyValue convert(Map<String, String> attributes) {
    KeyValue result = OMS.newKeyValue();
    if (attributes != null && !attributes.isEmpty()) {
        for (Map.Entry<String, String> entry : attributes.entrySet()) {
            result.put(entry.getKey(), entry.getValue());
        }
    }
    return result;
}
 
Example 5
Source File: OMSUtil.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public static KeyValue buildKeyValue(KeyValue... keyValues) {
    KeyValue keyValue = OMS.newKeyValue();
    for (KeyValue properties : keyValues) {
        for (String key : properties.keySet()) {
            keyValue.put(key, properties.getString(key));
        }
    }
    return keyValue;
}
 
Example 6
Source File: OMSUtil.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public static KeyValue buildKeyValue(KeyValue... keyValues) {
    KeyValue keyValue = OMS.newKeyValue();
    for (KeyValue properties : keyValues) {
        for (String key : properties.keySet()) {
            keyValue.put(key, properties.getString(key));
        }
    }
    return keyValue;
}
 
Example 7
Source File: SimpleProducer.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        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);

        // 使用MessagingAccessPoint创建producer
        Producer producer = messagingAccessPoint.createProducer();
        producer.start();

        // 使用producer.createMessage方法创建message
        Message message = producer.createMessage(topic, "Message body".getBytes());

        // 设置messageKey,非必填
        // 如果需要相对顺序消息,也可以使用messageKey作为key指定分区
//        message.extensionHeader().get().setMessageKey("test_key");

        // 自定义发送的partition
        // 最好根据元数据自定义分配,不要写死partitions
//        List<QueueMetaData.Partition> partitions = producer.getQueueMetaData("test_topic_0").partitions();
//        QueueMetaData.Partition partition = partitions.get((int) SystemClock.now() % partitions.size());
//        message.extensionHeader().get().setPartition(partition.partitionId());

        // 生产消息,不抛异常就算成功,sendResult里的messageId暂时没有意义
        SendResult sendResult = producer.send(message);

        // 打印生产结果
        System.out.println(String.format("messageId: %s", sendResult.messageId()));
    }
 
Example 8
Source File: OMSUtil.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static KeyValue buildKeyValue(KeyValue... keyValues) {
    KeyValue keyValue = OMS.newKeyValue();
    for (KeyValue properties : keyValues) {
        for (String key : properties.keySet()) {
            keyValue.put(key, properties.getString(key));
        }
    }
    return keyValue;
}
 
Example 9
Source File: ExtensionTransactionProducer.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);

        ExtensionProducer extensionProducer = (ExtensionProducer) messagingAccessPoint.createProducer();
        extensionProducer.start();

        ExtensionTransactionalResult transactionalResult = extensionProducer.prepare();

        for (int i = 0; i < 10; i++) {
            // 可以发多条事务消息
            Message message = extensionProducer.createMessage("test_topic_0", "body".getBytes());

            // 添加事务id,设置过事务id的才会被补偿,补偿时会带上这个事务id,非必填
            // 建议根据业务使用有意义的事务id
            message.extensionHeader().get().setTransactionId("test_transactionId");

            SendResult sendResult = transactionalResult.send(message);
            System.out.println(sendResult.messageId());
        }

        // 提交事务
        transactionalResult.commit();

        // 回滚事务
//        transactionalResult.rollback();
    }
 
Example 10
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 11
Source File: OMSUtil.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static KeyValue buildKeyValue(KeyValue... keyValues) {
    KeyValue keyValue = OMS.newKeyValue();
    for (KeyValue properties : keyValues) {
        for (String key : properties.keySet()) {
            keyValue.put(key, properties.getString(key));
        }
    }
    return keyValue;
}
 
Example 12
Source File: BytesMessageImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
public BytesMessageImpl() {
    this.headers = OMS.newKeyValue();
    this.properties = OMS.newKeyValue();
}
 
Example 13
Source File: BytesMessageImpl.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public BytesMessageImpl() {
    this.headers = OMS.newKeyValue();
    this.properties = OMS.newKeyValue();
}
 
Example 14
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 15
Source File: PushConsumerImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> rmqMsgList,
    ConsumeConcurrentlyContext contextRMQ) {
    MessageExt rmqMsg = rmqMsgList.get(0);
    BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg);

    MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic());

    if (listener == null) {
        throw new OMSRuntimeException("-1",
            String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic()));
    }

    final KeyValue contextProperties = OMS.newKeyValue();
    final CountDownLatch sync = new CountDownLatch(1);

    contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name());

    MessageListener.Context context = new MessageListener.Context() {
        @Override
        public KeyValue attributes() {
            return contextProperties;
        }

        @Override
        public void ack() {
            sync.countDown();
            contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS,
                ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name());
        }
    };
    long begin = System.currentTimeMillis();
    listener.onReceived(omsMsg, context);
    long costs = System.currentTimeMillis() - begin;
    long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000;
    try {
        sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }

    return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS));
}
 
Example 16
Source File: BytesMessageImpl.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
public BytesMessageImpl() {
    this.sysHeaders = OMS.newKeyValue();
    this.userHeaders = OMS.newKeyValue();
}
 
Example 17
Source File: BytesMessageImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public BytesMessageImpl() {
    this.sysHeaders = OMS.newKeyValue();
    this.userHeaders = OMS.newKeyValue();
}
 
Example 18
Source File: BytesMessageImpl.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
public BytesMessageImpl() {
    this.sysHeaders = OMS.newKeyValue();
    this.userHeaders = OMS.newKeyValue();
}
 
Example 19
Source File: PushConsumerImpl.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
@Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> rmqMsgList,
            ConsumeConcurrentlyContext contextRMQ) {
            MessageExt rmqMsg = rmqMsgList.get(0);
            BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg);

            MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic());

            if (listener == null) {
                throw new OMSRuntimeException("-1",
                    String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic()));
            }

            final KeyValue contextProperties = OMS.newKeyValue();
//            让一段代码执行完毕后再往下执行
            final CountDownLatch sync = new CountDownLatch(1);

            contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name());

            MessageListener.Context context = new MessageListener.Context() {
                @Override
                public KeyValue attributes() {
                    return contextProperties;
                }

                @Override
                public void ack() {
                    sync.countDown();
                    contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS,
                        ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name());
                }
            };
            long begin = System.currentTimeMillis();
            listener.onReceived(omsMsg, context);
            long costs = System.currentTimeMillis() - begin;
            long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000;
            try {
                sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS);
            } catch (InterruptedException ignore) {
            }

            return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS));
        }
 
Example 20
Source File: BytesMessageImpl.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public BytesMessageImpl() {
    this.headers = OMS.newKeyValue();
    this.properties = OMS.newKeyValue();
}