Search in sources :

Example 51 with SendResult

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

Example 52 with SendResult

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());
}
Also used : SendResult(org.apache.rocketmq.client.producer.SendResult) SendStatus(org.apache.rocketmq.client.producer.SendStatus) SendCallBackEnhanceInfo(org.apache.skywalking.apm.plugin.rocketMQ.v4.define.SendCallBackEnhanceInfo) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 53 with SendResult

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

Example 54 with SendResult

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

Example 55 with SendResult

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);
}
Also used : Context(org.apache.flume.Context) MemoryChannel(org.apache.flume.channel.MemoryChannel) Message(org.apache.rocketmq.common.message.Message) MemoryChannel(org.apache.flume.channel.MemoryChannel) Channel(org.apache.flume.Channel) ArrayList(java.util.ArrayList) ChannelProcessor(org.apache.flume.channel.ChannelProcessor) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Date(java.util.Date) PollableSource(org.apache.flume.PollableSource) ReplicatingChannelSelector(org.apache.flume.channel.ReplicatingChannelSelector) Transaction(org.apache.flume.Transaction) SendResult(org.apache.rocketmq.client.producer.SendResult) Event(org.apache.flume.Event) ReplicatingChannelSelector(org.apache.flume.channel.ReplicatingChannelSelector) ChannelSelector(org.apache.flume.ChannelSelector) Test(org.junit.Test)

Aggregations

SendResult (org.apache.rocketmq.client.producer.SendResult)95 Message (org.apache.rocketmq.common.message.Message)64 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)42 MQClientException (org.apache.rocketmq.client.exception.MQClientException)39 Test (org.junit.Test)32 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)15 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)15 SendCallback (org.apache.rocketmq.client.producer.SendCallback)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)10 ArrayList (java.util.ArrayList)8 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)8 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)8 IOException (java.io.IOException)6 TransactionSendResult (org.apache.rocketmq.client.producer.TransactionSendResult)6 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)6 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)5 BytesMessage (io.openmessaging.BytesMessage)4