Java Code Examples for io.openmessaging.producer.Producer#createMessage()

The following examples show how to use io.openmessaging.producer.Producer#createMessage() . 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: 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 2
Source File: BatchProducer.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);
    }

    producer.send(messages);
}
 
Example 3
Source File: AsyncProducer.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");
        keyValue.put(JoyQueueBuiltinKeys.TRANSACTION_TIMEOUT, 1000 * 10);

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

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

        Message message = producer.createMessage("test_topic_0", "body".getBytes());
        Future<SendResult> future = producer.sendAsync(message);

//        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: 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 5
Source File: ConsoleProducer.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
protected static void send(Producer producer, ConsoleProducerConfig config) {
    Message message = producer.createMessage(config.getTopic(), StringUtils.defaultString(config.getBody(), ToolConsts.DEFAULT_BODY).getBytes());

    if (StringUtils.isNotBlank(config.getKey())) {
        message.extensionHeader().get().setMessageKey(config.getKey());
    }

    producer.send(message);

    producer.stop();
}
 
Example 6
Source File: SpringMain.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-sample.xml");
    Producer producer = (Producer) applicationContext.getBean("producer1");

    for (int i = 0; i < 10; i++) {
        Message message = producer.createMessage("test_topic_0", "test".getBytes());
        SendResult sendResult = producer.send(message);
        logger.info("Message ID: {}", sendResult.messageId());
    }
}
 
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()));
    }