Search in sources :

Example 46 with DefaultMQProducer

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;
}
Also used : SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook)

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

Example 48 with DefaultMQProducer

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);
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) RocketMQJsonSerializer(org.apache.rocketmq.serializer.json.RocketMQJsonSerializer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 49 with DefaultMQProducer

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);
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 50 with DefaultMQProducer

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();
}
Also used : 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