Search in sources :

Example 66 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project java-example by 1479005017.

the class AliyunSender method main.

public static void main(String[] args) throws Exception {
    /**
     * Alions
     */
    AlionsRPCHook rpcHook = new AlionsRPCHook();
    rpcHook.setAccessKeyId(accessKeyId);
    rpcHook.setAccessKeySecret(accessKeySecret);
    rpcHook.setOnsChannel(onsChannel);
    /**
     * 初始化
     */
    DefaultMQProducer producer = new DefaultMQProducer(rpcHook);
    producer.setNamesrvAddr(HttpTinyClient.fetchNamesrvAddress(nameServer));
    producer.setProducerGroup(producerGroup);
    producer.setVipChannelEnabled(vipChannelEnabled);
    /**
     * 启动
     */
    producer.start();
    /**
     * 发送信息
     */
    Message msg = new Message("conanli-test", "conanli-test-commit", "Hello RocketMQ".getBytes(Charset.forName("UTF-8")));
    SendResult sendResult = producer.send(msg);
    System.out.printf("%s Send Message: %s, and Result: %s %n", Thread.currentThread().getName(), msg, sendResult);
    /**
     * 关闭
     */
    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 67 with DefaultMQProducer

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

the class RocketMQProducer method start.

public void start() throws MQClientException {
    producer = new DefaultMQProducer("BINLOG_PRODUCER_GROUP");
    producer.setNamesrvAddr(config.mqNamesrvAddr);
    producer.start();
}
Also used : DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 68 with DefaultMQProducer

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

the class ProducerInstance method getInstance.

public MQProducer getInstance(String nameServerAddress, String group) throws MQClientException {
    if (StringUtils.isBlank(group)) {
        group = DEFAULT_GROUP;
    }
    String genKey = genKey(nameServerAddress, group);
    MQProducer p = getProducerInstance().producerMap.get(genKey);
    if (p != null) {
        return p;
    }
    DefaultMQProducer defaultMQProducer = new DefaultMQProducer(group);
    defaultMQProducer.setNamesrvAddr(nameServerAddress);
    MQProducer beforeProducer = null;
    beforeProducer = getProducerInstance().producerMap.putIfAbsent(genKey, defaultMQProducer);
    if (beforeProducer != null) {
        return beforeProducer;
    }
    defaultMQProducer.start();
    return defaultMQProducer;
}
Also used : MQProducer(org.apache.rocketmq.client.producer.MQProducer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 69 with DefaultMQProducer

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

the class AbstractTestCase method mockLoggerAppender.

@Before
public void mockLoggerAppender() throws Exception {
    DefaultMQProducer defaultMQProducer = spy(new DefaultMQProducer("loggerAppender"));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Message message = (Message) invocationOnMock.getArgument(0);
            messages.add(message);
            return null;
        }
    }).when(defaultMQProducer).sendOneway(any(Message.class));
    ProducerInstance spy = mock(ProducerInstance.class);
    Field instance = ProducerInstance.class.getDeclaredField("instance");
    instance.setAccessible(true);
    instance.set(ProducerInstance.class, spy);
    doReturn(defaultMQProducer).when(spy).getInstance(anyString(), anyString());
}
Also used : Field(java.lang.reflect.Field) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) ProducerInstance(org.apache.rocketmq.logappender.common.ProducerInstance) Before(org.junit.Before)

Example 70 with DefaultMQProducer

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

the class SyncProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    DefaultMQProducer producer = new DefaultMQProducer(Constants.TEST_GROUP_NAME);
    // Launch the instance.
    producer.start();
    for (int i = 0; i < 1000; i++) {
        // Create a message instance, specifying topic, tag and message body.
        Message msg = new Message(Constants.TEST_TOPIC_NAME, "TagA", ("Hello RocketMQ From Sentinel " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
        // Call send message to deliver message to one of brokers.
        SendResult sendResult = producer.send(msg);
        System.out.printf("%s%n", sendResult);
    }
    // Shut down once the producer instance is not longer in use.
    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)

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