use of org.apache.rocketmq.client.producer.SendResult 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.SendResult in project incubator-skywalking by apache.
the class OnSuccessInterceptor method beforeMethod.
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
SendCallBackEnhanceInfo enhanceInfo = (SendCallBackEnhanceInfo) objInst.getSkyWalkingDynamicField();
AbstractSpan activeSpan = ContextManager.createLocalSpan(CALLBACK_OPERATION_NAME_PREFIX + enhanceInfo.getTopicId() + "/Producer/Callback");
activeSpan.setComponent(ComponentsDefine.ROCKET_MQ);
SendStatus sendStatus = ((SendResult) allArguments[0]).getSendStatus();
if (sendStatus != SendStatus.SEND_OK) {
activeSpan.errorOccurred();
Tags.STATUS_CODE.set(activeSpan, sendStatus.name());
}
ContextManager.continued(enhanceInfo.getContextSnapshot());
}
use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-externals by apache.
the class RocketMQRedisProducer method send.
/**
* @param event redis event.
* @return true if send success.
* @throws MQClientException MQClientException
* @throws RemotingException RemotingException
* @throws InterruptedException InterruptedException
* @throws MQBrokerException MQBrokerException
*/
public boolean send(Event event) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
Message msg = new Message(this.topic, serializer.write(event));
SendResult rs = this.producer.send(msg);
return rs.getSendStatus() == SendStatus.SEND_OK;
}
use of org.apache.rocketmq.client.producer.SendResult 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();
}
use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-externals by apache.
the class RocketMQSourceTest method testEvent.
@Test
public void testEvent() throws EventDeliveryException, MQBrokerException, MQClientException, InterruptedException, UnsupportedEncodingException, RemotingException {
// publish message before flume-source start
DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
producer.setNamesrvAddr(nameServer);
String sendMsg = "\"Hello Flume\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss");
producer.start();
Message msg = new Message(TOPIC_DEFAULT, tag, sendMsg.getBytes("UTF-8"));
SendResult sendResult = producer.send(msg);
// start source
Context context = new Context();
context.put(NAME_SERVER_CONFIG, nameServer);
context.put(TAG_CONFIG, tag);
Channel channel = new MemoryChannel();
Configurables.configure(channel, context);
List<Channel> channels = new ArrayList<>();
channels.add(channel);
ChannelSelector channelSelector = new ReplicatingChannelSelector();
channelSelector.setChannels(channels);
ChannelProcessor channelProcessor = new ChannelProcessor(channelSelector);
RocketMQSource source = new RocketMQSource();
source.setChannelProcessor(channelProcessor);
Configurables.configure(source, context);
source.start();
// wait for rebalanceImpl start
Thread.sleep(2000);
sendMsg = "\"Hello Flume\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss");
msg = new Message(TOPIC_DEFAULT, tag, sendMsg.getBytes("UTF-8"));
sendResult = producer.send(msg);
log.info("publish message : {}, sendResult:{}", sendMsg, sendResult);
PollableSource.Status status = source.process();
if (status == PollableSource.Status.BACKOFF) {
fail("Error");
}
/*
wait for processQueueTable init
*/
Thread.sleep(1000);
producer.shutdown();
source.stop();
/*
mock flume sink
*/
Transaction transaction = channel.getTransaction();
transaction.begin();
Event event = channel.take();
if (event == null) {
transaction.commit();
fail("Error");
}
byte[] body = event.getBody();
String receiveMsg = new String(body, "UTF-8");
log.info("receive message : {}", receiveMsg);
assertEquals(sendMsg, receiveMsg);
}
Aggregations