Search in sources :

Example 6 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.

the class RocketMQSinkTest method testBatchEvent.

@Test
public void testBatchEvent() throws MQClientException, InterruptedException, EventDeliveryException, RemotingException, MQBrokerException, UnsupportedEncodingException {
    /*
        start sink
         */
    Context context = new Context();
    context.put(NAME_SERVER_CONFIG, nameServer);
    context.put(TAG_CONFIG, tag);
    context.put(BATCH_SIZE_CONFIG, String.valueOf(batchSize));
    RocketMQSink sink = new RocketMQSink();
    Configurables.configure(sink, context);
    MemoryChannel channel = new MemoryChannel();
    Configurables.configure(channel, context);
    sink.setChannel(channel);
    sink.start();
    /*
        mock flume source
         */
    Map<String, String> msgs = new HashMap<>();
    Transaction tx = channel.getTransaction();
    tx.begin();
    int sendNum = 0;
    for (int i = 0; i < batchSize; i++) {
        String sendMsg = "\"Hello RocketMQ\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-DD hh:mm:ss:SSSS");
        Event event = EventBuilder.withBody(sendMsg.getBytes(), null);
        channel.put(event);
        log.info("publish message : {}", sendMsg);
        String[] sendMsgKv = sendMsg.split(",");
        msgs.put(sendMsgKv[1], sendMsgKv[0]);
        sendNum++;
        Thread.sleep(10);
    }
    log.info("send message num={}", sendNum);
    tx.commit();
    tx.close();
    Sink.Status status = sink.process();
    if (status == Sink.Status.BACKOFF) {
        fail("Error");
    }
    sink.stop();
    /*
        consumer message
         */
    consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setNamesrvAddr(nameServer);
    consumer.setMessageModel(MessageModel.valueOf("BROADCASTING"));
    consumer.registerMessageQueueListener(TOPIC_DEFAULT, null);
    consumer.start();
    int receiveNum = 0;
    String receiveMsg = null;
    Set<MessageQueue> queues = consumer.fetchSubscribeMessageQueues(TOPIC_DEFAULT);
    for (MessageQueue queue : queues) {
        long offset = getMessageQueueOffset(queue);
        PullResult pullResult = consumer.pull(queue, tag, offset, batchSize);
        if (pullResult.getPullStatus() == PullStatus.FOUND) {
            for (MessageExt message : pullResult.getMsgFoundList()) {
                byte[] body = message.getBody();
                receiveMsg = new String(body, "UTF-8");
                String[] receiveMsgKv = receiveMsg.split(",");
                msgs.remove(receiveMsgKv[1]);
                log.info("receive message : {}", receiveMsg);
                receiveNum++;
            }
            long nextBeginOffset = pullResult.getNextBeginOffset();
            putMessageQueueOffset(queue, nextBeginOffset);
        }
    }
    log.info("receive message num={}", receiveNum);
    /*
        wait for processQueueTable init
         */
    Thread.sleep(1000);
    consumer.shutdown();
    assertEquals(msgs.size(), 0);
}
Also used : Context(org.apache.flume.Context) MemoryChannel(org.apache.flume.channel.MemoryChannel) HashMap(java.util.HashMap) Date(java.util.Date) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MessageExt(org.apache.rocketmq.common.message.MessageExt) Transaction(org.apache.flume.Transaction) Sink(org.apache.flume.Sink) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Event(org.apache.flume.Event) Test(org.junit.Test)

Example 7 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.

the class RocketMQSource method doProcess.

@Override
protected Status doProcess() throws EventDeliveryException {
    List<Event> events = new ArrayList<>();
    Map<MessageQueue, Long> offsets = new HashMap<>();
    Event event;
    Map<String, String> headers;
    try {
        Set<MessageQueue> queues = consumer.fetchSubscribeMessageQueues(topic);
        for (MessageQueue queue : queues) {
            long offset = getMessageQueueOffset(queue);
            PullResult pullResult = consumer.pull(queue, tag, offset, batchSize);
            if (log.isDebugEnabled()) {
                log.debug("Pull from queueId:{}, offset:{}, pullResult:{}", queue.getQueueId(), offset, pullResult);
            }
            if (pullResult.getPullStatus() == PullStatus.FOUND) {
                for (MessageExt msg : pullResult.getMsgFoundList()) {
                    byte[] body = msg.getBody();
                    headers = new HashMap<>();
                    headers.put(HEADER_TOPIC_NAME, topic);
                    headers.put(HEADER_TAG_NAME, tag);
                    if (log.isDebugEnabled()) {
                        log.debug("Processing message,body={}", new String(body, "UTF-8"));
                    }
                    event = EventBuilder.withBody(body, headers);
                    events.add(event);
                }
                offsets.put(queue, pullResult.getNextBeginOffset());
            }
        }
        if (events.size() > 0) {
            sourceCounter.incrementAppendBatchReceivedCount();
            sourceCounter.addToEventReceivedCount(events.size());
            getChannelProcessor().processEventBatch(events);
            sourceCounter.incrementAppendBatchAcceptedCount();
            sourceCounter.addToEventAcceptedCount(events.size());
            events.clear();
        }
        for (Map.Entry<MessageQueue, Long> entry : offsets.entrySet()) {
            putMessageQueueOffset(entry.getKey(), entry.getValue());
        }
    } catch (Exception e) {
        log.error("Failed to consumer message", e);
        return Status.BACKOFF;
    }
    return Status.READY;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) EventDeliveryException(org.apache.flume.EventDeliveryException) ConfigurationException(org.apache.flume.conf.ConfigurationException) FlumeException(org.apache.flume.FlumeException) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Event(org.apache.flume.Event) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.

the class MessageConverter method convert2RMQMessage.

public static Message convert2RMQMessage(JmsBaseMessage jmsMsg) throws Exception {
    Message rocketmqMsg = new MessageExt();
    // 1. Transform message body
    rocketmqMsg.setBody(MessageConverter.getContentFromJms(jmsMsg));
    // 2. Transform topic and messageType
    JmsBaseTopic destination = (JmsBaseTopic) jmsMsg.getHeaders().get(JmsBaseConstant.JMS_DESTINATION);
    String topic = destination.getMessageTopic();
    rocketmqMsg.setTopic(topic);
    String messageType = destination.getMessageType();
    Preconditions.checkState(!messageType.contains("||"), "'||' can not be in the destination when sending a message");
    rocketmqMsg.setTags(messageType);
    // 3. Transform message properties
    Properties properties = initRocketMQHeaders(jmsMsg, topic, messageType);
    for (String name : properties.stringPropertyNames()) {
        String value = properties.getProperty(name);
        if (MessageConst.PROPERTY_KEYS.equals(name)) {
            rocketmqMsg.setKeys(value);
        } else if (MessageConst.PROPERTY_TAGS.equals(name)) {
            rocketmqMsg.setTags(value);
        } else if (MessageConst.PROPERTY_DELAY_TIME_LEVEL.equals(name)) {
            rocketmqMsg.setDelayTimeLevel(Integer.parseInt(value));
        } else if (MessageConst.PROPERTY_WAIT_STORE_MSG_OK.equals(name)) {
            rocketmqMsg.setWaitStoreMsgOK(Boolean.parseBoolean(value));
        } else if (MessageConst.PROPERTY_BUYER_ID.equals(name)) {
            rocketmqMsg.setBuyerId(value);
        } else {
            rocketmqMsg.putUserProperty(name, value);
        }
    }
    return rocketmqMsg;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage) JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) Message(org.apache.rocketmq.common.message.Message) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) JmsBaseMessage(org.apache.rocketmq.jms.domain.message.JmsBaseMessage) JmsBaseTopic(org.apache.rocketmq.jms.domain.JmsBaseTopic) Properties(java.util.Properties)

Example 9 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project rocketmq-externals by apache.

the class RocketMqUtilsTest method testConsumer.

@Test
public void testConsumer() throws MQBrokerException, MQClientException, InterruptedException, UnsupportedEncodingException {
    // start up spark
    Map<String, String> optionParams = new HashMap<>();
    optionParams.put(RocketMQConfig.NAME_SERVER_ADDR, NAME_SERVER);
    SparkConf sparkConf = new SparkConf().setAppName("JavaCustomReceiver").setMaster("local[*]");
    JavaStreamingContext sc = new JavaStreamingContext(sparkConf, new Duration(1000));
    List<String> topics = new ArrayList<>();
    topics.add(TOPIC_DEFAULT);
    LocationStrategy locationStrategy = LocationStrategy.PreferConsistent();
    JavaInputDStream<MessageExt> stream = RocketMqUtils.createJavaMQPullStream(sc, UUID.randomUUID().toString(), topics, ConsumerStrategy.earliest(), false, false, false, locationStrategy, optionParams);
    final Set<MessageExt> result = Collections.synchronizedSet(new HashSet<MessageExt>());
    stream.foreachRDD(new VoidFunction<JavaRDD<MessageExt>>() {

        @Override
        public void call(JavaRDD<MessageExt> messageExtJavaRDD) throws Exception {
            result.addAll(messageExtJavaRDD.collect());
        }
    });
    sc.start();
    long startTime = System.currentTimeMillis();
    boolean matches = false;
    while (!matches && System.currentTimeMillis() - startTime < 20000) {
        matches = MESSAGE_NUM == result.size();
        Thread.sleep(50);
    }
    sc.stop();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Duration(org.apache.spark.streaming.Duration) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JavaRDD(org.apache.spark.api.java.JavaRDD) JavaStreamingContext(org.apache.spark.streaming.api.java.JavaStreamingContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) LocationStrategy(org.apache.rocketmq.spark.LocationStrategy) SparkConf(org.apache.spark.SparkConf) Test(org.junit.Test)

Example 10 with MessageExt

use of org.apache.rocketmq.common.message.MessageExt in project paascloud-master by paascloud.

the class MqConsumerStoreAspect method processMqConsumerStoreJoinPoint.

/**
 * Add exe time method object.
 *
 * @param joinPoint the join point
 *
 * @return the object
 *
 * @throws Throwable the throwable
 */
@Around(value = "mqConsumerStoreAnnotationPointcut()")
public Object processMqConsumerStoreJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
    log.info("processMqConsumerStoreJoinPoint - 线程id={}", Thread.currentThread().getId());
    Object result;
    long startTime = System.currentTimeMillis();
    Object[] args = joinPoint.getArgs();
    MqConsumerStore annotation = getAnnotation(joinPoint);
    boolean isStorePreStatus = annotation.storePreStatus();
    List<MessageExt> messageExtList;
    if (args == null || args.length == 0) {
        throw new TpcBizException(ErrorCodeEnum.TPC10050005);
    }
    if (!(args[0] instanceof List)) {
        throw new TpcBizException(ErrorCodeEnum.GL99990001);
    }
    try {
        messageExtList = (List<MessageExt>) args[0];
    } catch (Exception e) {
        log.error("processMqConsumerStoreJoinPoint={}", e.getMessage(), e);
        throw new TpcBizException(ErrorCodeEnum.GL99990001);
    }
    MqMessageData dto = this.getTpcMqMessageDto(messageExtList.get(0));
    final String messageKey = dto.getMessageKey();
    if (isStorePreStatus) {
        mqMessageService.confirmReceiveMessage(consumerGroup, dto);
    }
    String methodName = joinPoint.getSignature().getName();
    try {
        result = joinPoint.proceed();
        log.info("result={}", result);
        if (CONSUME_SUCCESS.equals(result.toString())) {
            mqMessageService.saveAndConfirmFinishMessage(consumerGroup, messageKey);
        }
    } catch (Exception e) {
        log.error("发送可靠消息, 目标方法[{}], 出现异常={}", methodName, e.getMessage(), e);
        throw e;
    } finally {
        log.info("发送可靠消息 目标方法[{}], 总耗时={}", methodName, System.currentTimeMillis() - startTime);
    }
    return result;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MqConsumerStore(com.paascloud.provider.annotation.MqConsumerStore) MqMessageData(com.paascloud.provider.model.domain.MqMessageData) List(java.util.List) TpcBizException(com.paascloud.provider.exceptions.TpcBizException) TpcBizException(com.paascloud.provider.exceptions.TpcBizException) Around(org.aspectj.lang.annotation.Around)

Aggregations

MessageExt (org.apache.rocketmq.common.message.MessageExt)23 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)7 PullResult (org.apache.rocketmq.client.consumer.PullResult)7 MQClientException (org.apache.rocketmq.client.exception.MQClientException)7 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)7 Test (org.junit.Test)7 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)6 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)6 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)4 MqConsumerStore (com.paascloud.provider.annotation.MqConsumerStore)3 Date (java.util.Date)3 Event (org.apache.flume.Event)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Map (java.util.Map)2 Context (org.apache.flume.Context)2 Sink (org.apache.flume.Sink)2