Search in sources :

Example 21 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project ignite by apache.

the class RocketMQStreamerTest method produceData.

/**
 * Adds data to RocketMQ.
 *
 * @throws Exception If fails.
 */
private void produceData() throws Exception {
    initTopic(TOPIC_NAME, TEST_IP + ":" + NAME_SERVER_PORT);
    DefaultMQProducer producer = new DefaultMQProducer("testProducerGrp");
    producer.setNamesrvAddr(TEST_IP + ":" + NAME_SERVER_PORT);
    try {
        producer.start();
        for (int i = 0; i < EVT_NUM; i++) producer.send(new Message(TOPIC_NAME, "", String.valueOf(i).getBytes("UTF-8")));
    } catch (Exception e) {
        throw new Exception(e);
    } finally {
        producer.shutdown();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException)

Example 22 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-externals by apache.

the class ProducerTest method main.

public static void main(String[] args) {
    DefaultMQProducer producer = new DefaultMQProducer("p001");
    producer.setNamesrvAddr("localhost:9876");
    try {
        producer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
    for (int i = 0; i < 10000; i++) {
        Message msg = new Message("flink-source2", "", "id_" + i, ("country_X province_" + i).getBytes());
        try {
            producer.send(msg);
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("send " + i);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 23 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-externals by apache.

the class RocketMQSink method start.

@Override
public synchronized void start() {
    producer = new DefaultMQProducer(producerGroup);
    producer.setNamesrvAddr(nameServer);
    try {
        producer.start();
    } catch (MQClientException e) {
        sinkCounter.incrementConnectionFailedCount();
        log.error("RocketMQ producer start failed", e);
        throw new FlumeException("Failed to start RocketMQ producer", e);
    }
    sinkCounter.incrementConnectionCreatedCount();
    sinkCounter.start();
    super.start();
}
Also used : FlumeException(org.apache.flume.FlumeException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 24 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer 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 25 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-externals by apache.

the class RocketMQServerMock method prepareDataTo.

public void prepareDataTo(String topic, int times) throws Exception {
    // publish test message
    DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
    producer.setNamesrvAddr(nameserverAddr);
    String sendMsg = "\"Hello Rocket\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-DD hh:mm:ss");
    try {
        producer.start();
        for (int i = 0; i < times; i++) {
            producer.send(new Message(topic, sendMsg.getBytes("UTF-8")));
        }
    } catch (Exception e) {
        throw new MQClientException("Failed to publish messages", e);
    } finally {
        producer.shutdown();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Date(java.util.Date) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)90 Message (org.apache.rocketmq.common.message.Message)63 SendResult (org.apache.rocketmq.client.producer.SendResult)36 MQClientException (org.apache.rocketmq.client.exception.MQClientException)31 Test (org.junit.Test)27 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)13 SendCallback (org.apache.rocketmq.client.producer.SendCallback)11 ArrayList (java.util.ArrayList)10 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)8 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 MQProducer (org.apache.rocketmq.client.producer.MQProducer)5 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)4 CommandLine (org.apache.commons.cli.CommandLine)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2