Search in sources :

Example 1 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer in project ignite by apache.

the class RocketMQStreamer method start.

/**
 * Starts streamer.
 *
 * @throws IgniteException If failed.
 */
public void start() {
    if (!stopped)
        throw new IgniteException("Attempted to start an already started RocketMQ streamer");
    // validate parameters.
    A.notNull(getStreamer(), "streamer");
    A.notNull(getIgnite(), "ignite");
    A.notNull(topic, "topic");
    A.notNull(consumerGrp, "consumer group");
    A.notNullOrEmpty(nameSrvAddr, "nameserver address");
    A.ensure(null != getMultipleTupleExtractor(), "Multiple tuple extractor must be configured");
    log = getIgnite().log();
    consumer = new DefaultMQPushConsumer(consumerGrp);
    consumer.setNamesrvAddr(nameSrvAddr);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
    try {
        consumer.subscribe(topic, "*");
    } catch (MQClientException e) {
        throw new IgniteException("Failed to subscribe to " + topic, e);
    }
    consumer.registerMessageListener(this);
    try {
        consumer.start();
    } catch (MQClientException e) {
        throw new IgniteException("Failed to start the streamer", e);
    }
    stopped = false;
}
Also used : IgniteException(org.apache.ignite.IgniteException) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer in project rocketmq-externals by apache.

the class RocketMQConsoleTestBase method startTestMQConsumer.

protected void startTestMQConsumer() {
    // test online consumer
    consumer = new DefaultMQPushConsumer(TEST_CONSUMER_GROUP);
    consumerConfigInfo.setBrokerNameList(Lists.newArrayList(TestConstant.TEST_BROKER_NAME));
    SubscriptionGroupConfig subscriptionGroupConfig = new SubscriptionGroupConfig();
    subscriptionGroupConfig.setGroupName(TEST_CONSUMER_GROUP);
    subscriptionGroupConfig.setRetryQueueNums(RETRY_QUEUE_NUMS);
    consumerConfigInfo.setSubscriptionGroupConfig(subscriptionGroupConfig);
    consumerService.createAndUpdateSubscriptionGroupConfig(consumerConfigInfo);
    consumer.setNamesrvAddr(TestConstant.NAME_SERVER_ADDRESS);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    try {
        consumer.subscribe(TEST_CONSOLE_TOPIC, "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                consoleTestBaseLog.info("op=consumeMessage message={}", JsonUtil.obj2String(msgs));
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)

Example 3 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer in project rocketmq-externals by apache.

the class ConsumerTest method main.

public static void main(String[] args) {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("g00003");
    consumer.setNamesrvAddr("localhost:9876");
    try {
        consumer.subscribe("flink-sink2", "*");
    } catch (MQClientException e) {
        e.printStackTrace();
    }
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            for (MessageExt msg : msgs) {
                System.out.println(msg.getKeys() + ":" + new String(msg.getBody()));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    try {
        consumer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 4 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer in project rocketmq-externals by apache.

the class JmsClientIT method testConfigInURI.

@Test
public void testConfigInURI() throws Exception {
    JmsBaseConnectionFactory connectionFactory = new JmsBaseConnectionFactory(new URI(String.format("rocketmq://xxx?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s", CommonConstant.PRODUCERID, producerId, CommonConstant.CONSUMERID, consumerId, CommonConstant.NAMESERVER, nameServer, CommonConstant.CONSUME_THREAD_NUMS, consumeThreadNums, CommonConstant.SEND_TIMEOUT_MILLIS, 10 * 1000, CommonConstant.INSTANCE_NAME, "JMS_TEST")));
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    connection.start();
    try {
        Destination destination = session.createTopic(topic + ":" + messageType);
        session.createConsumer(destination);
        session.createProducer(destination);
        DefaultMQPushConsumer rmqPushConsumer = (DefaultMQPushConsumer) getRMQPushConsumerExt(consumerId).getConsumer();
        Assert.assertNotNull(rmqPushConsumer);
        Assert.assertEquals(consumerId, rmqPushConsumer.getConsumerGroup());
        Assert.assertEquals("JMS_TEST", rmqPushConsumer.getInstanceName());
        Assert.assertEquals(consumeThreadNums, rmqPushConsumer.getConsumeThreadMax());
        Assert.assertEquals(consumeThreadNums, rmqPushConsumer.getConsumeThreadMin());
        Assert.assertEquals(nameServer, rmqPushConsumer.getNamesrvAddr());
        DefaultMQProducer mqProducer = (DefaultMQProducer) JmsTestUtil.getMQProducer(producerId);
        Assert.assertNotNull(mqProducer);
        Assert.assertEquals(producerId, mqProducer.getProducerGroup());
        Assert.assertEquals("JMS_TEST", mqProducer.getInstanceName());
        Assert.assertEquals(10 * 1000, mqProducer.getSendMsgTimeout());
        Assert.assertEquals(nameServer, mqProducer.getNamesrvAddr());
        Thread.sleep(2000);
    } finally {
        connection.close();
    }
}
Also used : Destination(javax.jms.Destination) JmsBaseConnectionFactory(org.apache.rocketmq.jms.domain.JmsBaseConnectionFactory) Connection(javax.jms.Connection) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) URI(java.net.URI) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 5 with DefaultMQPushConsumer

use of org.apache.rocketmq.client.consumer.DefaultMQPushConsumer in project rocketmq-externals by apache.

the class DefaultRocketMQListenerContainer method initRocketMQPushConsumer.

private void initRocketMQPushConsumer() throws MQClientException {
    Assert.notNull(rocketMQListener, "Property 'rocketMQListener' is required");
    Assert.notNull(consumerGroup, "Property 'consumerGroup' is required");
    Assert.notNull(nameServer, "Property 'nameServer' is required");
    Assert.notNull(topic, "Property 'topic' is required");
    consumer = new DefaultMQPushConsumer(consumerGroup);
    consumer.setNamesrvAddr(nameServer);
    consumer.setConsumeThreadMax(consumeThreadMax);
    if (consumeThreadMax < consumer.getConsumeThreadMin()) {
        consumer.setConsumeThreadMin(consumeThreadMax);
    }
    consumer.setMessageModel(messageModel);
    switch(selectorType) {
        case TAG:
            consumer.subscribe(topic, selectorExpress);
            break;
        case SQL92:
            consumer.subscribe(topic, MessageSelector.bySql(selectorExpress));
            break;
        default:
            throw new IllegalArgumentException("Property 'selectorType' was wrong.");
    }
    switch(consumeMode) {
        case ORDERLY:
            consumer.setMessageListener(new DefaultMessageListenerOrderly());
            break;
        case CONCURRENTLY:
            consumer.setMessageListener(new DefaultMessageListenerConcurrently());
            break;
        default:
            throw new IllegalArgumentException("Property 'consumeMode' was wrong.");
    }
    // provide an entryway to custom setting RocketMQ consumer
    if (rocketMQListener instanceof RocketMQPushConsumerLifecycleListener) {
        ((RocketMQPushConsumerLifecycleListener) rocketMQListener).prepareStart(consumer);
    }
}
Also used : DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)

Aggregations

DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)11 MessageExt (org.apache.rocketmq.common.message.MessageExt)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 MQClientException (org.apache.rocketmq.client.exception.MQClientException)5 ConsumeOrderlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext)2 ConsumeOrderlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus)2 MessageListenerOrderly (org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly)2 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 URI (java.net.URI)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Connection (javax.jms.Connection)1 Destination (javax.jms.Destination)1 Session (javax.jms.Session)1 IgniteException (org.apache.ignite.IgniteException)1 SendResult (org.apache.rocketmq.client.producer.SendResult)1 Message (org.apache.rocketmq.common.message.Message)1 SubscriptionGroupConfig (org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)1 JmsBaseConnectionFactory (org.apache.rocketmq.jms.domain.JmsBaseConnectionFactory)1