Java Code Examples for io.openmessaging.BytesMessage#getBody()

The following examples show how to use io.openmessaging.BytesMessage#getBody() . 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: 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();
        }
    }
}
 
Example 2
Source File: ConsumerTester.java    From coding-snippets 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);
            final int expectedOffset = offsets.get(queueOrTopic).get(producer);
            int offset = Integer.parseInt(body.substring(index + 1));
            if (offset != expectedOffset) {
                logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        expectedOffset, 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 3
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 4
Source File: V7ConsumerTester.java    From coding-snippets with MIT License 4 votes vote down vote up
@Override
        public void run() {
        	
            while (true) {
                try {
                	long start = System.currentTimeMillis();
                    BytesMessage message = (BytesMessage) consumer.poll();
                    time += System.currentTimeMillis() - start;
                    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
                    String body = new String(message.getBody());
                    int index = body.lastIndexOf("_");
                    //生产者线程名
                    String producer = body.substring(0, index);
                    //该线程的该消息对应的偏移
                    int offset = Integer.parseInt(body.substring(index + 1));
                    
                    /**
                     * 检测正确性和顺序。
                     * 每个获得的消息,根据queueOrTopic,再通过producer(生产者线程名),
                     * 就找到偏移(即等于之前的编译+1,因为发送时是累加的),一致则这个消息接受正确。
                     */
                    if (offset != offsets.get(queueOrTopic).get(producer)) {
                        logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        		offsets.get(queueOrTopic).get(producer), offset, producer, queueOrTopic);
                        System.out.println("消费: "+pullNum);
                        break;
                    } else {
//                        logger.info("Offset  equal expected:{} actual:{} producer:{} queueOrTopic:{}",
//                        		offsets.get(queueOrTopic).get(producer), offset, producer, queueOrTopic);
                        offsets.get(queueOrTopic).put(producer, offset + 1);
                    }
                    pullNum++;
//                    System.out.println(Thread.currentThread().getName()+": "+pullNum);
                } catch (Exception e) {
                    logger.error("Error occurred in the consuming process", e);
                    break;
                }
            }
        }
 
Example 5
Source File: V1ConsumerTester.java    From coding-snippets with MIT License 4 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
            String body = new String(message.getBody());
            int index = body.lastIndexOf("_");
            //生产者线程名
            String producer = body.substring(0, index);
            //该线程的该消息对应的偏移
            int offset = Integer.parseInt(body.substring(index + 1));
            
            /**
             * 检测正确性和顺序。
             * 每个获得的消息,根据queueOrTopic,再通过producer(生产者线程名),
             * 就找到偏移(即等于之前的编译+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);
                break;
            } else {
                offsets.get(queueOrTopic).put(producer, offset + 1);
            }
            pullNum++;
        } catch (Exception e) {
            logger.error("Error occurred in the consuming process", e);
            break;
        }
    }
}
 
Example 6
Source File: ConsumerTester.java    From coding-snippets with MIT License 4 votes vote down vote up
@Override
        public void run() {
        	
            while (true) {
                try {
                	long start = System.currentTimeMillis();
                    BytesMessage message = (BytesMessage) consumer.poll();
                    time += System.currentTimeMillis() - start;
                    if (message == null) {
//                    	System.out.println(Thread.currentThread().getName()+" pullNum: "+pullNum);
                        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
                    String body = new String(message.getBody());
                    int index = body.lastIndexOf("_");
                    //生产者线程名
                    String producer = body.substring(0, index);
                    //该线程的该消息对应的偏移
                    int offset = Integer.parseInt(body.substring(index + 1));
                    
                    /**
                     * 检测正确性和顺序。
                     * 每个获得的消息,根据queueOrTopic,再通过producer(生产者线程名),
                     * 就找到偏移(即等于之前的编译+1,因为发送时是累加的),一致则这个消息接受正确。
                     */
                    if (offset != offsets.get(queueOrTopic).get(producer)) {
                        logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        		offsets.get(queueOrTopic).get(producer), offset, producer, queueOrTopic);
                        System.out.println("消费: "+pullNum);
                        break;
                    } else {
//                        logger.info("Offset  equal expected:{} actual:{} producer:{} queueOrTopic:{}",
//                        		offsets.get(queueOrTopic).get(producer), offset, producer, queueOrTopic);
                        offsets.get(queueOrTopic).put(producer, offset + 1);
                    }
                    pullNum++;
//                    System.out.println(Thread.currentThread().getName()+": "+pullNum);
                } catch (Exception e) {
                    logger.error("Error occurred in the consuming process", e);
                    break;
                }
            }
        }
 
Example 7
Source File: V6ConsumerTester.java    From coding-snippets with MIT License 4 votes vote down vote up
@Override
        public void run() {
        	
            while (true) {
                try {
                	long start = System.currentTimeMillis();
                    BytesMessage message = (BytesMessage) consumer.poll();
                    time += System.currentTimeMillis() - start;
                    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
                    String body = new String(message.getBody());
                    int index = body.lastIndexOf("_");
                    //生产者线程名
                    String producer = body.substring(0, index);
                    //该线程的该消息对应的偏移
                    int offset = Integer.parseInt(body.substring(index + 1));
                    
                    /**
                     * 检测正确性和顺序。
                     * 每个获得的消息,根据queueOrTopic,再通过producer(生产者线程名),
                     * 就找到偏移(即等于之前的编译+1,因为发送时是累加的),一致则这个消息接受正确。
                     */
                    if (offset != offsets.get(queueOrTopic).get(producer)) {
                        logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        		offsets.get(queueOrTopic).get(producer), offset, producer, queueOrTopic);
                        System.out.println("消费: "+pullNum);
                        break;
                    } else {
//                        logger.info("Offset  equal expected:{} actual:{} producer:{} queueOrTopic:{}",
//                        		offsets.get(queueOrTopic).get(producer), offset, producer, queueOrTopic);
                        offsets.get(queueOrTopic).put(producer, offset + 1);
                    }
                    pullNum++;
//                    System.out.println(Thread.currentThread().getName()+": "+pullNum);
                } catch (Exception e) {
                    logger.error("Error occurred in the consuming process", e);
                    break;
                }
            }
        }
 
Example 8
Source File: V4ConsumerTester.java    From coding-snippets with MIT License 4 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
            String body = new String(message.getBody());
            int index = body.lastIndexOf("_");
            //生产者线程名
            String producer = body.substring(0, index);
            //该线程的该消息对应的偏移
            int offset = Integer.parseInt(body.substring(index + 1));
            
            /**
             * 检测正确性和顺序。
             * 每个获得的消息,根据queueOrTopic,再通过producer(生产者线程名),
             * 就找到偏移(即等于之前的编译+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);
                break;
            } else {
                offsets.get(queueOrTopic).put(producer, offset + 1);
            }
            pullNum++;
        } catch (Exception e) {
            logger.error("Error occurred in the consuming process", e);
            break;
        }
    }
}
 
Example 9
Source File: V2ConsumerTester.java    From coding-snippets with MIT License 4 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
            String body = new String(message.getBody());
            int index = body.lastIndexOf("_");
            //生产者线程名
            String producer = body.substring(0, index);
            //该线程的该消息对应的偏移
            int offset = Integer.parseInt(body.substring(index + 1));
            
            /**
             * 检测正确性和顺序。
             * 每个获得的消息,根据queueOrTopic,再通过producer(生产者线程名),
             * 就找到偏移(即等于之前的编译+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);
                break;
            } else {
                offsets.get(queueOrTopic).put(producer, offset + 1);
            }
            pullNum++;
        } catch (Exception e) {
            logger.error("Error occurred in the consuming process", e);
            break;
        }
    }
}
 
Example 10
Source File: V5ConsumerTester.java    From coding-snippets with MIT License 4 votes vote down vote up
@Override
        public void run() {
        	
            while (true) {
                try {
                	long start = System.currentTimeMillis();
                    BytesMessage message = (BytesMessage) consumer.poll();
                    time += System.currentTimeMillis() - start;
                    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
                    String body = new String(message.getBody());
                    int index = body.lastIndexOf("_");
                    //生产者线程名
                    String producer = body.substring(0, index);
                    //该线程的该消息对应的偏移
                    int offset = Integer.parseInt(body.substring(index + 1));
                    
                    /**
                     * 检测正确性和顺序。
                     * 每个获得的消息,根据queueOrTopic,再通过producer(生产者线程名),
                     * 就找到偏移(即等于之前的编译+1,因为发送时是累加的),一致则这个消息接受正确。
                     */
                    if (offset != offsets.get(queueOrTopic).get(producer)) {
                        logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        		offsets.get(queueOrTopic).get(producer), offset, producer, queueOrTopic);
                        System.out.println("消费: "+pullNum);
                        break;
                    } else {
//                        logger.info("Offset  equal expected:{} actual:{} producer:{} queueOrTopic:{}",
//                        		offsets.get(queueOrTopic).get(producer), offset, producer, queueOrTopic);
                        offsets.get(queueOrTopic).put(producer, offset + 1);
                    }
                    pullNum++;
//                    System.out.println(Thread.currentThread().getName()+": "+pullNum);
                } catch (Exception e) {
                    logger.error("Error occurred in the consuming process", e);
                    break;
                }
            }
        }
 
Example 11
Source File: ConsumerTester.java    From coding-snippets with MIT License 4 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
            String body = new String(message.getBody());
            int index = body.lastIndexOf("_");
            //生产者线程名
            String producer = body.substring(0, index);
            //该线程的该消息对应的偏移
            int offset = Integer.parseInt(body.substring(index + 1));
            
            /**
             * 检测正确性和顺序。
             * 每个获得的消息,根据queueOrTopic,再通过producer(生产者线程名),
             * 就找到偏移(即等于之前的编译+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;
        }
    }
}