io.openmessaging.Message Java Examples

The following examples show how to use io.openmessaging.Message. 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: ProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private SendResult send(final Message message, long timeout) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    try {
        org.apache.rocketmq.client.producer.SendResult rmqResult = this.rocketmqProducer.send(rmqMessage, timeout);
        if (!rmqResult.getSendStatus().equals(SendStatus.SEND_OK)) {
            log.error(String.format("Send message to RocketMQ failed, %s", message));
            throw new OMSRuntimeException("-1", "Send message to RocketMQ broker failed.");
        }
        message.headers().put(MessageHeader.MESSAGE_ID, rmqResult.getMsgId());
        return OMSUtil.sendResultConvert(rmqResult);
    } catch (Exception e) {
        log.error(String.format("Send message to RocketMQ failed, %s", message), e);
        throw checkProducerException(rmqMessage.getTopic(), message.headers().getString(MessageHeader.MESSAGE_ID), e);
    }
}
 
Example #2
Source File: ProducerTester.java    From coding-snippets with MIT License 6 votes vote down vote up
@Override
public void run() {
    while (true) {
        try {
            String queueOrTopic;
            //queue和topic数量为1:9,都只有10种类型
            if (sendNum % 10 == 0) {
                queueOrTopic = "QUEUE_" + random.nextInt(10);
            } else {
                queueOrTopic = "TOPIC_" + random.nextInt(10);
            }
            Message message = producer.createBytesMessageToQueue(queueOrTopic, (label + "_" + offsets.get(queueOrTopic)).getBytes());
            logger.debug("queueOrTopic:{} offset:{}", queueOrTopic, label + "_" + offsets.get(queueOrTopic));
            offsets.put(queueOrTopic, offsets.get(queueOrTopic) + 1);
            producer.send(message);
            sendNum++;
            if (sendNum >= Constants.PRO_MAX) {
                break;
            }
        } catch (Exception e) {
            logger.error("Error occurred in the sending process", e);
            break;
        }
    }
}
 
Example #3
Source File: PushConsumerImplTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeMessage() {
    final byte[] testBody = new byte[] {'a', 'b'};

    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic("HELLO_QUEUE");
    consumer.attachQueue("HELLO_QUEUE", new MessageListener() {
        @Override
        public void onReceived(Message message, Context context) {
            assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId");
            assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody);
            context.ack();
        }
    });
    ((MessageListenerConcurrently) rocketmqPushConsumer
        .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null);
}
 
Example #4
Source File: BrokerBasedLog.java    From openmessaging-connect-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {

    producer.startup();

    consumer.attachQueue(queueName, (message, context) -> {

        try {

            // Need openMessaging to support start consume message from tail.
            if(Long.parseLong(message.sysHeaders().getString(Message.BuiltinKeys.BORN_TIMESTAMP)) + 10000 < System.currentTimeMillis()){
                context.ack();
                return;
            }
            log.info("Received one message: {}", message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID) + "\n");
            byte[] bytes = message.getBody(byte[].class);
            Map<K, V> map = decodeKeyValue(bytes);
            for (K key : map.keySet()) {
                dataSynchronizerCallback.onCompletion(null, key, map.get(key));
            }
            context.ack();
        }catch(Exception e){
            log.error("BrokerBasedLog process message failed.", e);
        }
    });
    consumer.startup();
}
 
Example #5
Source File: ProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public void sendOneway(final Message message) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    try {
        this.rocketmqProducer.sendOneway(rmqMessage);
    } catch (Exception ignore) { //Ignore the oneway exception.
    }
}
 
Example #6
Source File: ProducerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public void sendOneway(final Message message) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    try {
        this.rocketmqProducer.sendOneway(rmqMessage);
    } catch (Exception ignore) { //Ignore the oneway exception.
    }
}
 
Example #7
Source File: DefaultBytesMessage.java    From OpenMessageShaping with MIT License 5 votes vote down vote up
@Override
public Message putProperties(String key, long value) {
    if (properties == null)
        properties = new DefaultKeyValue();
    properties.put(key, value);
    return this;
}
 
Example #8
Source File: PullConsumerImplTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoll() {
    final byte[] testBody = new byte[] {'a', 'b'};
    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic(queueName);

    when(localMessageCache.poll()).thenReturn(consumedMsg);

    Message message = consumer.poll();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("NewMsgId");
    assertThat(((BytesMessage) message).getBody()).isEqualTo(testBody);
}
 
Example #9
Source File: ProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public void sendOneway(final Message message) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    try {
        this.rocketmqProducer.sendOneway(rmqMessage);
    } catch (Exception ignore) { //Ignore the oneway exception.
    }
}
 
Example #10
Source File: V7ProducerList.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override public void send(Message message) {
 	//处理以前没有发送完留下的对象
 	if( readyForRead.size() > 0){
 		Iterator<Entry<String, List<DefaultBytesMessageList>>> it = readyForRead.entrySet().iterator();
 		Entry<String, List<DefaultBytesMessageList>> e = null;
 		String key = null;
 		while(it.hasNext()){
 			e = it.next();
 			key =  e.getKey();
 			//能把值更改为true的才获得mapBuffer,并开始写。否则等待下次send
 			if(isReading.get(key).compareAndSet(false, true)){
 				mapBuffer = allMapBuffer.get(key);
 				sendList(e.getValue(), storeOff.get(key));
 				isReading.get(key).set(false);
 				it.remove();
 				storeOff.put(key, 0);
 				
 			}
 		}
 	}
 	/**
 	 * 当存储偏移达到writeThreshold时,写该list. off为下一个要写的数组下标
 	 * 偏移大于writeThreshold时,肯定已经在readyForRead中了。
 	 * 偏移小于于writeThreshold时,不用处理
 	 */
 	if(off == writeThreshold){
 		//能把值更改为true的才获得mapBuffer,并开始写。否则等待下次send
if(isReading.get(queueOrTopic).compareAndSet(false, true)){
	mapBuffer = allMapBuffer.get(queueOrTopic);
	sendList(list , off);
	isReading.get(queueOrTopic).set(false);
	storeOff.put(queueOrTopic, 0);
	
}else{
	//需要清理,但是获得buffer失败,偏移+1,尝试放入readyForRead
	readyForRead.put(queueOrTopic, list);
}
 	}
 }
 
Example #11
Source File: DefaultBytesMessage.java    From OpenMessageShaping with MIT License 5 votes vote down vote up
@Override
public Message putProperties(String key, int value) {
    if (properties == null)
        properties = new DefaultKeyValue();
    properties.put(key, value);
    return this;
}
 
Example #12
Source File: PullConsumerImplTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoll_WithTimeout() {
    //There is a default timeout value, @see ClientConfig#omsOperationTimeout.
    Message message = consumer.receive();
    assertThat(message).isNull();

    message = consumer.receive(OMS.newKeyValue().put(Message.BuiltinKeys.TIMEOUT, 100));
    assertThat(message).isNull();
}
 
Example #13
Source File: LocalMessageCache.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
MessageExt poll(final KeyValue properties) {
    int currentPollTimeout = clientConfig.getOperationTimeout();
    if (properties.containsKey(Message.BuiltinKeys.TIMEOUT)) {
        currentPollTimeout = properties.getInt(Message.BuiltinKeys.TIMEOUT);
    }
    return poll(currentPollTimeout);
}
 
Example #14
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 #15
Source File: DefaultBytesMessageProducer.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override public Message putProperties(String key, double value) {
        if (prosBiulder == null) prosBiulder = new StringBuilder(sem);
//        if (properties == null) properties = new DefaultKeyValue();
        prosBiulder.append(key);
        prosBiulder.append(sem);
        prosBiulder.append(value);
        prosBiulder.append(sem);
        proSize++;
        return this;
    }
 
Example #16
Source File: PullConsumerImplTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoll() {
    final byte[] testBody = new byte[] {'a', 'b'};
    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic(queueName);

    when(localMessageCache.poll()).thenReturn(consumedMsg);

    Message message = consumer.receive();
    assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId");
    assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody);
}
 
Example #17
Source File: V7Producer.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override public void send(Message message) {
 	//处理以前没有发送完留下的对象
 	if( readyForRead.size() > 0){
 		Iterator<Entry<String, List<DefaultBytesMessage>>> it = readyForRead.entrySet().iterator();
 		Entry<String, List<DefaultBytesMessage>> e = null;
 		String key = null;
 		while(it.hasNext()){
 			e = it.next();
 			key =  e.getKey();
 			//能把值更改为true的才获得mapBuffer,并开始写。否则等待下次send
 			if(isReading.get(key).compareAndSet(false, true)){
 				mapBuffer = allMapBuffer.get(key);
 				sendList(e.getValue(), storeOff.get(key));
 				isReading.get(key).set(false);
 				it.remove();
 				storeOff.put(key, 0);
 				
 			}
 		}
 	}
 	/**
 	 * 当存储偏移达到writeThreshold时,写该list. off为下一个要写的数组下标
 	 * 偏移大于writeThreshold时,肯定已经在readyForRead中了。
 	 * 偏移小于于writeThreshold时,不用处理
 	 */
 	if(off == writeThreshold){
 		//能把值更改为true的才获得mapBuffer,并开始写。否则等待下次send
if(isReading.get(queueOrTopic).compareAndSet(false, true)){
	mapBuffer = allMapBuffer.get(queueOrTopic);
	sendList(list , off);
	isReading.get(queueOrTopic).set(false);
	storeOff.put(queueOrTopic, 0);
	
}else{
	//需要清理,但是获得buffer失败,偏移+1,尝试放入readyForRead
	readyForRead.put(queueOrTopic, list);
}
 	}
 }
 
Example #18
Source File: SimplePushConsumer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final MessagingAccessPoint messagingAccessPoint = OMS
        .getMessagingAccessPoint("oms:rocketmq://localhost:9876/default:default");

    final PushConsumer consumer = messagingAccessPoint.
        createPushConsumer(OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "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.attachQueue("OMS_HELLO_TOPIC", new MessageListener() {
        @Override
        public void onReceived(Message message, Context context) {
            System.out.printf("Received one message: %s%n", message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID));
            context.ack();
        }
    });

    consumer.startup();
    System.out.printf("Consumer startup OK%n");
}
 
Example #19
Source File: V4PullConsumer.java    From coding-snippets with MIT License 5 votes vote down vote up
public Message poll() {
	// 每个线程都应该先读文件,或者阻塞住等待读文件完毕
	
	if (isReaded == false) {
		long start = System.currentTimeMillis();
		
		readFile();
		isReaded = true;
		// 将files赋为null,下个测试例才知道是新的测试例,重新初始化各个属性
		files = null;
		
		long end = System.currentTimeMillis() - start;
		System.out.println(Thread.currentThread().getName()+" 读文件耗时(ms):"+end);
	}
	// 可能某个queue/topic一个message都没有,滚动到下一个
	while (currentList == null && bucketOffset < bucketList.size() - 1) {
		++bucketOffset;
		currentList = data.get(bucketList.get(bucketOffset));
		
	}

	if (currentList == null) {
		return null;
	}

	BytesMessage message = currentList.get(messageOffset);
	if (messageOffset < currentList.size() - 1) {
		messageOffset++;
	} else {
		// bucketOffset++;
		currentList = null;
		messageOffset = 0;
	}
	return message;
}
 
Example #20
Source File: DefaultPullConsumer.java    From OpenMessageShaping with MIT License 5 votes vote down vote up
@Override
public Message poll() {
    if (naiveDataReader.hasNext()) {
        return naiveDataReader.next();
    } else {
        return null;
    }
}
 
Example #21
Source File: DefaultBytesMessage.java    From OpenMessageShaping with MIT License 5 votes vote down vote up
@Override
public Message putProperties(String key, double value) {
    if (properties == null)
        properties = new DefaultKeyValue();
    properties.put(key, value);
    return this;
}
 
Example #22
Source File: DefaultBytesMessageProducer.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override public Message putHeaders(String key, int value) {
    headersBiulder.append(seq.get(key));
    headersBiulder.append(value);
    headersBiulder.append(sem);
    headerSize++;
    return this;
}
 
Example #23
Source File: LocalMessageCache.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
MessageExt poll(final KeyValue properties) {
    int currentPollTimeout = clientConfig.getOperationTimeout();
    if (properties.containsKey(Message.BuiltinKeys.TIMEOUT)) {
        currentPollTimeout = properties.getInt(Message.BuiltinKeys.TIMEOUT);
    }
    return poll(currentPollTimeout);
}
 
Example #24
Source File: V2ProducerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override
        public void run() {
            while (true) {
                try {
                    String queueOrTopic;
                    Message message;
                    if (sendNum % 10 == 0) {
                        queueOrTopic = "QUEUE_" + random.nextInt(10);
                        message = producer.createBytesMessageToQueue(queueOrTopic, (label + "_" + offsets.get(queueOrTopic)).getBytes());
                    } else {
                        queueOrTopic = "TOPIC_" + random.nextInt(10);
                        message = producer.createBytesMessageToTopic(queueOrTopic, (label + "_" + offsets.get(queueOrTopic)).getBytes());
                    }
                    /**
                     * 消息主体 :“生产者线程名_偏移” 的字节.
                     * 对于label(线程名)和queue/topic的组合来说来说,偏移是一直累加的,0、1、2、3……
                     * 即每个线程对于每个queue/topic的消息中偏移都是一个连续的累加的。
                     */
//                    Message message = producer.createBytesMessageToQueue(queueOrTopic, (label + "_" + offsets.get(queueOrTopic)).getBytes());
                    logger.debug("queueOrTopic:{} offset:{}", queueOrTopic, label + "_" + offsets.get(queueOrTopic));
                    //每产生一个消息,该bucket对应的便偏移+1
                    offsets.put(queueOrTopic, offsets.get(queueOrTopic) + 1);
                    producer.send(message);
                    sendNum++;
                    if (sendNum >= Constants.PRO_MAX) {
                    	//自己测试时记得关闭,不然没法删除持有的文件。实测时kill进程,没有这个问题
                    	producer.shutdown();
                        break;
                    }
                } catch (Exception e) {
                    logger.error("Error occurred in the sending process", e);
                    break;
                }
            }
        }
 
Example #25
Source File: DefaultBytesMessageProducer2.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override public Message putProperties(String key, double value) {
    	putProperties(key, String.valueOf(value));
//        if (prosBiulder == null) prosBiulder = new StringBuilder(sem);
////        if (properties == null) properties = new DefaultKeyValue();
//        prosBiulder.append(key);
//        prosBiulder.append(sem);
//        prosBiulder.append(value);
//        prosBiulder.append(sem);
//        proSize++;
        return this;
    }
 
Example #26
Source File: PullConsumerImplTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoll() {
    final byte[] testBody = new byte[] {'a', 'b'};
    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic(queueName);

    when(localMessageCache.poll()).thenReturn(consumedMsg);

    Message message = consumer.receive();
    assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId");
    assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody);
}
 
Example #27
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 #28
Source File: V5Producer.java    From coding-snippets with MIT License 4 votes vote down vote up
@Override public void send(Message message) {
    	
//    	toBytesAndSend(message);   //91s,1亿。在官方111s,103s
//    	toBytes(message);   //96s,1亿。在官方99s,109s
    	toBytesAndSendMany(message); //91s,1亿
    }
 
Example #29
Source File: BytesMessageImpl.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public Message putHeaders(final String key, final String value) {
    headers.put(key, value);
    return this;
}
 
Example #30
Source File: SimpleBinarySerializer.java    From coding-snippets with MIT License 4 votes vote down vote up
@Override
public void write(ByteBuffer buffer, List<Message> messages) throws BufferOverflowException {
    for (Message message : messages) {
        write(buffer, message);
    }
}