use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-externals by apache.
the class ConnectUtil method initDefaultMQProducer.
public static DefaultMQProducer initDefaultMQProducer(ConnectConfig connectConfig) {
RPCHook rpcHook = null;
if (connectConfig.getAclEnable()) {
rpcHook = new AclClientRPCHook(new SessionCredentials(connectConfig.getAccessKey(), connectConfig.getSecretKey()));
}
DefaultMQProducer producer = new DefaultMQProducer(rpcHook);
producer.setNamesrvAddr(connectConfig.getNamesrvAddr());
producer.setInstanceName(createUniqInstance(connectConfig.getNamesrvAddr()));
producer.setProducerGroup(connectConfig.getRmqProducerGroup());
producer.setSendMsgTimeout(connectConfig.getOperationTimeout());
producer.setMaxMessageSize(RuntimeConfigDefine.MAX_MESSAGE_SIZE);
producer.setLanguage(LanguageCode.JAVA);
return producer;
}
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(UUID.randomUUID().toString());
producer.setNamesrvAddr(getNameServerAddr());
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();
}
}
use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-externals by apache.
the class JsonProducer method main.
public static void main(String[] args) throws Exception {
DefaultMQProducer producer = new DefaultMQProducer("producer-group-json");
producer.setNamesrvAddr("localhost:9876");
producer.start();
// creating serializer for message body serializing
RocketMQSerializer serializer = new RocketMQJsonSerializer<User>();
for (int i = 0; i < 100; i++) {
User user = new User();
user.setName("tom");
user.setAge(i);
// creating message from user data.
Message message = Messages.newMessage("topic-json", user, serializer);
SendResult result = producer.send(message);
System.out.print(result.getSendStatus() + " " + i + "\n");
Thread.sleep(1000);
}
}
use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-externals by apache.
the class SimpleProducer method main.
public static void main(String[] args) throws Exception {
DefaultMQProducer producer = new DefaultMQProducer("producer-group-simple");
producer.setNamesrvAddr("localhost:9876");
producer.start();
for (int i = 0; i < 100; i++) {
// creating message from user data. using string serializer by default.
Message message = Messages.newMessage("topic", "say hello to rocket! " + i);
SendResult result = producer.send(message);
System.out.print(result.getSendStatus() + " " + i + "\n");
Thread.sleep(1000);
}
}
use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-externals by apache.
the class RocketMQProducer method start.
/**
* Starts the producer.
*
* @throws MQClientException
*/
public void start() throws MQClientException {
producer = new DefaultMQProducer(PRODUCER_GROUP_NAME);
producer.setInstanceName("producer-" + System.currentTimeMillis());
producer.setNamesrvAddr(namesrvAddr);
producer.start();
}
Aggregations