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