Search in sources :

Example 51 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 52 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 53 with DefaultMQProducer

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

the class RocketMQSourceTest method writeData.

/**
 * @param inMsg
 * @return
 * @throws MQClientException
 * @throws UnsupportedEncodingException
 * @throws RemotingException
 * @throws InterruptedException
 * @throws MQBrokerException
 */
private String writeData(String inMsg) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException, MQBrokerException {
    final DefaultMQProducer producer = new DefaultMQProducer(PRODUCER_GROUP_NAME);
    producer.setNamesrvAddr(NAMESERVER);
    try {
        producer.start();
        final Message msg = new Message(ROCKETMQ_TOPIC, null, inMsg.getBytes("UTF-8"));
        final SendResult sendResult = producer.send(msg);
        logger.info("publish message : {}, sendResult:{}", msg, sendResult);
        return sendResult.getMsgId();
    } finally {
        producer.shutdown();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 54 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project spring-boot-starter-samples by vindell.

the class BatchProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
    producer.setRetryTimesWhenSendAsyncFailed(0);
    // Launch the instance.
    producer.start();
    String topic = "BatchTest";
    List<Message> messages = new ArrayList<Message>();
    messages.add(new Message(topic, "TagA", "OrderID001", "Hello world 0".getBytes()));
    messages.add(new Message(topic, "TagA", "OrderID002", "Hello world 1".getBytes()));
    messages.add(new Message(topic, "TagA", "OrderID003", "Hello world 2".getBytes()));
    try {
        producer.send(messages);
    } catch (Exception e) {
        e.printStackTrace();
    // handle the error
    }
    // then you could split the large list into small ones:
    ListSplitter splitter = new ListSplitter(messages);
    while (splitter.hasNext()) {
        try {
            List<Message> listItem = splitter.next();
            producer.send(listItem);
        } catch (Exception e) {
            e.printStackTrace();
        // handle the error
        }
    }
    // Shut down once the producer instance is not longer in use.
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) ArrayList(java.util.ArrayList) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 55 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project spring-boot-starter-samples by vindell.

the class ScheduledMessageProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate a producer to send scheduled messages
    DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
    // Launch producer
    producer.start();
    int totalMessagesToSend = 100;
    for (int i = 0; i < totalMessagesToSend; i++) {
        Message message = new Message("TestTopic", ("Hello scheduled message " + i).getBytes());
        // This message will be delivered to consumer 10 seconds later.
        message.setDelayTimeLevel(3);
        // Send the message
        producer.send(message);
    }
    // Shutdown producer after use.
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)100 Message (org.apache.rocketmq.common.message.Message)68 SendResult (org.apache.rocketmq.client.producer.SendResult)41 MQClientException (org.apache.rocketmq.client.exception.MQClientException)31 Test (org.junit.Test)28 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)13 ArrayList (java.util.ArrayList)12 SendCallback (org.apache.rocketmq.client.producer.SendCallback)11 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 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Date (java.util.Date)3 CommandLine (org.apache.commons.cli.CommandLine)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2