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();
}
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();
}
}
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();
}
}
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();
}
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();
}
Aggregations