io.openmessaging.BytesMessage Java Examples

The following examples show how to use io.openmessaging.BytesMessage. 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-read 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.sysHeaders().put(Message.BuiltinKeys.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.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID), e);
    }
}
 
Example #2
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 #3
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 #4
Source File: V4PullConsumer.java    From coding-snippets with MIT License 6 votes vote down vote up
public V4PullConsumer(KeyValue properties) {
	this.properties = properties;
	/**
	 * 暂未考虑同步问题。现在测试例使用反射,没有并发,所以测试问题没发现问题。
	 * files为null代表是新的测试例,为避免本次测试影响下次需要初始化各个属性。
	 */
	if (files == null) {
		String path = properties.getString("STORE_PATH");
		files = new File(path).listFiles();
		fileNum = files.length;
		index = new AtomicInteger(0);
		finshed = new AtomicInteger(0);
		latch = new CountDownLatch(fileNum);
		data = new HashMap<String, List<BytesMessage>>(170);
		listOffset = new HashMap<String, AtomicInteger>(170);
	}
}
 
Example #5
Source File: ProducerImpl.java    From rocketmq 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.sysHeaders().put(Message.BuiltinKeys.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.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID), e);
    }
}
 
Example #6
Source File: PushConsumerImplTest.java    From DDMQ 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 onMessage(final Message message, final ReceivedMessageContext context) {
            assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("NewMsgId");
            assertThat(((BytesMessage) message).getBody()).isEqualTo(testBody);
            context.ack();
        }
    });
    ((MessageListenerConcurrently) rocketmqPushConsumer
        .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null);
}
 
Example #7
Source File: PushConsumerImplTest.java    From DDMQ 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 onMessage(final Message message, final ReceivedMessageContext context) {
            assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("NewMsgId");
            assertThat(((BytesMessage) message).getBody()).isEqualTo(testBody);
            context.ack();
        }
    });
    ((MessageListenerConcurrently) rocketmqPushConsumer
        .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null);
}
 
Example #8
Source File: ProducerImpl.java    From DDMQ 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 #9
Source File: PushConsumerImplTest.java    From rocketmq 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 #10
Source File: ProducerImpl.java    From DDMQ 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 #11
Source File: OMSUtil.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static BytesMessage msgConvert(org.apache.rocketmq.common.message.MessageExt rmqMsg) {
    BytesMessage omsMsg = new BytesMessageImpl();
    omsMsg.setBody(rmqMsg.getBody());

    KeyValue headers = omsMsg.headers();
    KeyValue properties = omsMsg.properties();

    final Set<Map.Entry<String, String>> entries = rmqMsg.getProperties().entrySet();

    for (final Map.Entry<String, String> entry : entries) {
        if (isOMSHeader(entry.getKey())) {
            headers.put(entry.getKey(), entry.getValue());
        } else {
            properties.put(entry.getKey(), entry.getValue());
        }
    }

    omsMsg.putHeaders(MessageHeader.MESSAGE_ID, rmqMsg.getMsgId());
    if (!rmqMsg.getProperties().containsKey(NonStandardKeys.MESSAGE_DESTINATION) ||
        rmqMsg.getProperties().get(NonStandardKeys.MESSAGE_DESTINATION).equals("TOPIC")) {
        omsMsg.putHeaders(MessageHeader.TOPIC, rmqMsg.getTopic());
    } else {
        omsMsg.putHeaders(MessageHeader.QUEUE, rmqMsg.getTopic());
    }
    omsMsg.putHeaders(MessageHeader.SEARCH_KEY, rmqMsg.getKeys());
    omsMsg.putHeaders(MessageHeader.BORN_HOST, String.valueOf(rmqMsg.getBornHost()));
    omsMsg.putHeaders(MessageHeader.BORN_TIMESTAMP, rmqMsg.getBornTimestamp());
    omsMsg.putHeaders(MessageHeader.STORE_HOST, String.valueOf(rmqMsg.getStoreHost()));
    omsMsg.putHeaders(MessageHeader.STORE_TIMESTAMP, rmqMsg.getStoreTimestamp());
    return omsMsg;
}
 
Example #12
Source File: SequenceProducerImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend_WithCommit() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    SendResult sendResult = new SendResult();
    sendResult.setMsgId("TestMsgID");
    sendResult.setSendStatus(SendStatus.SEND_OK);
    when(rocketmqProducer.send(ArgumentMatchers.<Message>anyList())).thenReturn(sendResult);
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] {'a'});
    producer.send(message);
    producer.commit();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("TestMsgID");
}
 
Example #13
Source File: V1PullConsumer.java    From coding-snippets with MIT License 5 votes vote down vote up
public V1PullConsumer(KeyValue properties) {
    this.properties = properties;
    /**
     * 暂未考虑同步问题。现在测试例使用反射,没有并发,所以测试问题没发现问题。
     * files为null代表是新的测试例,为避免本次测试影响下次需要初始化各个属性。
     */
    if(files == null){
    	String path = properties.getString("STORE_PATH");
        files = new File(path).listFiles();
        fileNum = files.length;
        index = new AtomicInteger(0);
        finshed = new AtomicInteger(0);
        data = new HashMap<String, List<BytesMessage>>(170);
    }
}
 
Example #14
Source File: AbstractOMSProducer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Override
public BytesMessage createBytesMessage(String queue, byte[] body) {
    BytesMessage message = new BytesMessageImpl();
    message.setBody(body);
    message.sysHeaders().put(Message.BuiltinKeys.DESTINATION, queue);
    return message;
}
 
Example #15
Source File: SequenceProducerImplTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend_WithCommit() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    SendResult sendResult = new SendResult();
    sendResult.setMsgId("TestMsgID");
    sendResult.setSendStatus(SendStatus.SEND_OK);
    when(rocketmqProducer.send(ArgumentMatchers.<Message>anyList())).thenReturn(sendResult);
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] {'a'});
    producer.send(message);
    producer.commit();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("TestMsgID");
}
 
Example #16
Source File: V1PullConsumer.java    From coding-snippets with MIT License 5 votes vote down vote up
public Message poll() {
    	//每个线程都应该先读文件,或者阻塞住等待读文件完毕
    	if(isReaded == false){
    		readFile();
    		isReaded = true;
    		//将files赋为null,下个测试例才知道是新的测试例,重新初始化各个属性
    		files = null;
    	}
    	
    	//可能某个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 #17
Source File: SequenceProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public void send(final Message message) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = OMSUtil.msgConvert((BytesMessage) message);
    try {
        Validators.checkMessage(rmqMessage, this.rocketmqProducer);
    } catch (MQClientException e) {
        throw checkProducerException(rmqMessage.getTopic(), message.headers().getString(MessageHeader.MESSAGE_ID), e);
    }
    msgCacheQueue.add(message);
}
 
Example #18
Source File: OMSUtil.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public static BytesMessage msgConvert(org.apache.rocketmq.common.message.MessageExt rmqMsg) {
    BytesMessage omsMsg = new BytesMessageImpl();
    omsMsg.setBody(rmqMsg.getBody());

    KeyValue headers = omsMsg.sysHeaders();
    KeyValue properties = omsMsg.userHeaders();

    final Set<Map.Entry<String, String>> entries = rmqMsg.getProperties().entrySet();

    for (final Map.Entry<String, String> entry : entries) {
        if (isOMSHeader(entry.getKey())) {
            headers.put(entry.getKey(), entry.getValue());
        } else {
            properties.put(entry.getKey(), entry.getValue());
        }
    }

    omsMsg.putSysHeaders(BuiltinKeys.MESSAGE_ID, rmqMsg.getMsgId());

    omsMsg.putSysHeaders(BuiltinKeys.DESTINATION, rmqMsg.getTopic());

    omsMsg.putSysHeaders(BuiltinKeys.SEARCH_KEYS, rmqMsg.getKeys());
    omsMsg.putSysHeaders(BuiltinKeys.BORN_HOST, String.valueOf(rmqMsg.getBornHost()));
    omsMsg.putSysHeaders(BuiltinKeys.BORN_TIMESTAMP, rmqMsg.getBornTimestamp());
    omsMsg.putSysHeaders(BuiltinKeys.STORE_HOST, String.valueOf(rmqMsg.getStoreHost()));
    omsMsg.putSysHeaders(BuiltinKeys.STORE_TIMESTAMP, rmqMsg.getStoreTimestamp());
    return omsMsg;
}
 
Example #19
Source File: SequenceProducerImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testRollback() {
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] {'a'});
    producer.send(message);
    producer.rollback();
    producer.commit(); //Commit nothing.
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo(null);
}
 
Example #20
Source File: OMSUtil.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static BytesMessage msgConvert(org.apache.rocketmq.common.message.MessageExt rmqMsg) {
    BytesMessage omsMsg = new BytesMessageImpl();
    omsMsg.setBody(rmqMsg.getBody());

    KeyValue headers = omsMsg.sysHeaders();
    KeyValue properties = omsMsg.userHeaders();

    final Set<Map.Entry<String, String>> entries = rmqMsg.getProperties().entrySet();

    for (final Map.Entry<String, String> entry : entries) {
        if (isOMSHeader(entry.getKey())) {
            headers.put(entry.getKey(), entry.getValue());
        } else {
            properties.put(entry.getKey(), entry.getValue());
        }
    }

    omsMsg.putSysHeaders(BuiltinKeys.MESSAGE_ID, rmqMsg.getMsgId());

    omsMsg.putSysHeaders(BuiltinKeys.DESTINATION, rmqMsg.getTopic());

    omsMsg.putSysHeaders(BuiltinKeys.SEARCH_KEYS, rmqMsg.getKeys());
    omsMsg.putSysHeaders(BuiltinKeys.BORN_HOST, String.valueOf(rmqMsg.getBornHost()));
    omsMsg.putSysHeaders(BuiltinKeys.BORN_TIMESTAMP, rmqMsg.getBornTimestamp());
    omsMsg.putSysHeaders(BuiltinKeys.STORE_HOST, String.valueOf(rmqMsg.getStoreHost()));
    omsMsg.putSysHeaders(BuiltinKeys.STORE_TIMESTAMP, rmqMsg.getStoreTimestamp());
    return omsMsg;
}
 
Example #21
Source File: OMSUtil.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static BytesMessage msgConvert(org.apache.rocketmq.common.message.MessageExt rmqMsg) {
    BytesMessage omsMsg = new BytesMessageImpl();
    omsMsg.setBody(rmqMsg.getBody());

    KeyValue headers = omsMsg.headers();
    KeyValue properties = omsMsg.properties();

    final Set<Map.Entry<String, String>> entries = rmqMsg.getProperties().entrySet();

    for (final Map.Entry<String, String> entry : entries) {
        if (isOMSHeader(entry.getKey())) {
            headers.put(entry.getKey(), entry.getValue());
        } else {
            properties.put(entry.getKey(), entry.getValue());
        }
    }

    omsMsg.putHeaders(MessageHeader.MESSAGE_ID, rmqMsg.getMsgId());
    if (!rmqMsg.getProperties().containsKey(NonStandardKeys.MESSAGE_DESTINATION) ||
        rmqMsg.getProperties().get(NonStandardKeys.MESSAGE_DESTINATION).equals("TOPIC")) {
        omsMsg.putHeaders(MessageHeader.TOPIC, rmqMsg.getTopic());
    } else {
        omsMsg.putHeaders(MessageHeader.QUEUE, rmqMsg.getTopic());
    }
    omsMsg.putHeaders(MessageHeader.SEARCH_KEY, rmqMsg.getKeys());
    omsMsg.putHeaders(MessageHeader.BORN_HOST, String.valueOf(rmqMsg.getBornHost()));
    omsMsg.putHeaders(MessageHeader.BORN_TIMESTAMP, rmqMsg.getBornTimestamp());
    omsMsg.putHeaders(MessageHeader.STORE_HOST, String.valueOf(rmqMsg.getStoreHost()));
    omsMsg.putHeaders(MessageHeader.STORE_TIMESTAMP, rmqMsg.getStoreTimestamp());
    return omsMsg;
}
 
Example #22
Source File: AbstractOMSProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public BytesMessage createBytesMessageToQueue(final String queue, final byte[] body) {
    BytesMessage bytesMessage = new BytesMessageImpl();
    bytesMessage.setBody(body);
    bytesMessage.headers().put(MessageHeader.QUEUE, queue);
    return bytesMessage;
}
 
Example #23
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 #24
Source File: AbstractOMSProducer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public BytesMessage createBytesMessageToTopic(final String topic, final byte[] body) {
    BytesMessage bytesMessage = new BytesMessageImpl();
    bytesMessage.setBody(body);
    bytesMessage.headers().put(MessageHeader.TOPIC, topic);
    return bytesMessage;
}
 
Example #25
Source File: V2PullConsumer.java    From coding-snippets with MIT License 5 votes vote down vote up
public V2PullConsumer(KeyValue properties) {
	this.properties = properties;
	/**
	 * 暂未考虑同步问题。现在测试例使用反射,没有并发,所以测试问题没发现问题。
	 * files为null代表是新的测试例,为避免本次测试影响下次需要初始化各个属性。
	 */
	if (files == null) {
		String path = properties.getString("STORE_PATH");
		files = new File(path).listFiles();
		fileNum = files.length;
		index = new AtomicInteger(0);
		finshed = new AtomicInteger(0);
		data = new HashMap<String, List<BytesMessage>>(170);
	}
}
 
Example #26
Source File: ConsumerTester.java    From OpenMessageShaping with MIT License 5 votes vote down vote up
@Override
public void run() {
    while (true) {
        try {
            BytesMessage message = (BytesMessage) consumer.poll();
            if (message == null) {
                break;
            }
            String queueOrTopic;
            if (message.headers().getString(MessageHeader.QUEUE) != null) {
                queueOrTopic = message.headers().getString(MessageHeader.QUEUE);
            } else {
                queueOrTopic = message.headers().getString(MessageHeader.TOPIC);
            }
            if (queueOrTopic == null || queueOrTopic.length() == 0) {
                throw new Exception("Queue or Topic name is empty");
            }
            String body = new String(message.getBody());
            int index = body.lastIndexOf("_");
            String producer = body.substring(0, index);
            int offset = Integer.parseInt(body.substring(index + 1));
            if (offset != offsets.get(queueOrTopic).get(producer)) {
                logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        offsets.get(producer), offset, producer, queueOrTopic);
                break;
            } else {
                offsets.get(queueOrTopic).put(producer, offset + 1);
            }
            pullNum++;
        } catch (Exception e) {
            logger.error("Error occurred in the consuming process", e);
            break;
        }
    }
}
 
Example #27
Source File: V5Producer2.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override public BytesMessage createBytesMessageToTopic(String topic, byte[] body) {
//        return messageFactory.createBytesMessageToTopic(topic, body);
//    	if(!all.contains(topic)){
//    		System.out.println("发送topic:"+topic);
//    		all.add(topic);
//    	}
        return messageFactory.changeBytesMessageToTopic(topic, body);
    }
 
Example #28
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 #29
Source File: V5Producer.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override public BytesMessage createBytesMessageToTopic(String topic, byte[] body) {
//        return messageFactory.createBytesMessageToTopic(topic, body);
//    	if(!all.contains(topic)){
//    		System.out.println("发送topic:"+topic);
//    		all.add(topic);
//    	}
        return messageFactory.changeBytesMessageToTopic(topic, body);
    }
 
Example #30
Source File: V3ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
public void run(){
    while (true){
        try {
            BytesMessage message=(BytesMessage)consumer.poll();
            if (message==null){
                break;
            }
            String queueOrTopic;
            if (message.headers().getString(MessageHeader.QUEUE)!=null){
                queueOrTopic=message.headers().getString(MessageHeader.QUEUE);
            }else {
                queueOrTopic=message.headers().getString(MessageHeader.TOPIC);
            }

            if(queueOrTopic==null||queueOrTopic.length()==0){
                throw new Exception("Queue or Topic name is empty");
            }

            String body=new String(message.getBody());
            int index=body.lastIndexOf("_");
            String producer=body.substring(0,index);
            int offset = Integer.parseInt(body.substring(index+1));

            if (offset!=offsets.get(queueOrTopic).get(producer)){
                logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        offsets.get(producer), offset, producer, queueOrTopic);
                System.out.println("消费: "+pullNum);
            }else {
                offsets.get(queueOrTopic).put(producer, offset + 1);
            }
            pullNum++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}