Search in sources :

Example 16 with SendResult

use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.

the class ProducerImpl method send.

public SendResult send(CloudEvent cloudEvent) {
    this.checkProducerServiceState(rocketmqProducer.getDefaultMQProducerImpl());
    org.apache.rocketmq.common.message.Message msg = RocketMQMessageFactory.createWriter(cloudEvent.getSubject()).writeBinary(cloudEvent);
    msg = supplySysProp(msg, cloudEvent);
    String messageId = null;
    try {
        org.apache.rocketmq.client.producer.SendResult sendResultRmq = this.rocketmqProducer.send(msg);
        SendResult sendResult = new SendResult();
        sendResult.setTopic(sendResultRmq.getMessageQueue().getTopic());
        messageId = sendResultRmq.getMsgId();
        sendResult.setMessageId(messageId);
        return sendResult;
    } catch (Exception e) {
        log.error(String.format("Send message Exception, %s", msg), e);
        throw this.checkProducerException(msg.getTopic(), messageId, e);
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.eventmesh.api.SendResult) ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException)

Example 17 with SendResult

use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.

the class StandaloneProducer method publish.

public void publish(CloudEvent cloudEvent, SendCallback sendCallback) throws Exception {
    Preconditions.checkNotNull(cloudEvent);
    Preconditions.checkNotNull(sendCallback);
    try {
        SendResult sendResult = publish(cloudEvent);
        sendCallback.onSuccess(sendResult);
    } catch (Exception ex) {
        OnExceptionContext onExceptionContext = OnExceptionContext.builder().messageId(cloudEvent.getId()).topic(cloudEvent.getSubject()).exception(new ConnectorRuntimeException(ex)).build();
        sendCallback.onException(onExceptionContext);
    }
}
Also used : ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SendResult(org.apache.eventmesh.api.SendResult) ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException)

Example 18 with SendResult

use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.

the class ClientGroupWrapper method sendMsgBackToBroker.

private void sendMsgBackToBroker(CloudEvent event, String bizSeqNo) throws Exception {
    try {
        String topic = event.getSubject();
        logger.warn("send msg back to broker, bizSeqno:{}, topic:{}", bizSeqNo, topic);
        long startTime = System.currentTimeMillis();
        long taskExcuteTime = startTime;
        send(new UpStreamMsgContext(null, event, null, startTime, taskExcuteTime), new SendCallback() {

            @Override
            public void onSuccess(SendResult sendResult) {
                logger.info("group:{} consume fail, sendMessageBack success, bizSeqno:{}, " + "topic:{}", group, bizSeqNo, topic);
            }

            @Override
            public void onException(OnExceptionContext context) {
                logger.warn("group:{} consume fail, sendMessageBack fail, bizSeqno:{}," + " topic:{}", group, bizSeqNo, topic);
            }
        });
        eventMeshTcpMonitor.getTcpSummaryMetrics().getEventMesh2mqMsgNum().incrementAndGet();
    } catch (Exception e) {
        logger.warn("try send msg back to broker failed");
        throw e;
    }
}
Also used : UpStreamMsgContext(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SendResult(org.apache.eventmesh.api.SendResult) SendCallback(org.apache.eventmesh.api.SendCallback)

Example 19 with SendResult

use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.

the class SendMessageContext method retry.

@Override
public boolean retry() throws Exception {
    if (eventMeshProducer == null) {
        return false;
    }
    if (retryTimes > 0) {
        // retry once
        return false;
    }
    retryTimes++;
    eventMeshProducer.send(this, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
        }

        @Override
        public void onException(OnExceptionContext context) {
            logger.warn("", context.getException());
            eventMeshHTTPServer.metrics.getSummaryMetrics().recordSendBatchMsgFailed(1);
        }
    });
    return true;
}
Also used : OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SendResult(org.apache.eventmesh.api.SendResult) SendCallback(org.apache.eventmesh.api.SendCallback)

Example 20 with SendResult

use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.

the class MessageTransferTask method createSendCallback.

protected SendCallback createSendCallback(Command replyCmd, long taskExecuteTime, CloudEvent event) {
    final long createTime = System.currentTimeMillis();
    Package msg = new Package();
    return new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            session.getSender().getUpstreamBuff().release();
            messageLogger.info("upstreamMsg message success|user={}|callback cost={}", session.getClient(), System.currentTimeMillis() - createTime);
            if (replyCmd.equals(Command.BROADCAST_MESSAGE_TO_SERVER_ACK) || replyCmd.equals(Command.ASYNC_MESSAGE_TO_SERVER_ACK)) {
                msg.setHeader(new Header(replyCmd, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq()));
                msg.setBody(event);
                Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
            }
        }

        @Override
        public void onException(OnExceptionContext context) {
            session.getSender().getUpstreamBuff().release();
            // retry
            UpStreamMsgContext upStreamMsgContext = new UpStreamMsgContext(session, event, pkg.getHeader(), startTime, taskExecuteTime);
            upStreamMsgContext.delay(10000);
            session.getClientGroupWrapper().get().getEventMeshTcpRetryer().pushRetry(upStreamMsgContext);
            session.getSender().failMsgCount.incrementAndGet();
            messageLogger.error("upstreamMsg mq message error|user={}|callback cost={}, errMsg={}", session.getClient(), (System.currentTimeMillis() - createTime), new Exception(context.getException()));
            msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), context.getException().toString(), pkg.getHeader().getSeq()));
            msg.setBody(event);
            Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
        }
    };
}
Also used : UpStreamMsgContext(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext) Header(org.apache.eventmesh.common.protocol.tcp.Header) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) EventMeshTcpSendResult(org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult) SendResult(org.apache.eventmesh.api.SendResult) Package(org.apache.eventmesh.common.protocol.tcp.Package) SendCallback(org.apache.eventmesh.api.SendCallback)

Aggregations

SendResult (org.apache.eventmesh.api.SendResult)20 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)17 SendCallback (org.apache.eventmesh.api.SendCallback)15 CloudEvent (io.cloudevents.CloudEvent)7 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)7 ConnectorRuntimeException (org.apache.eventmesh.api.exception.ConnectorRuntimeException)5 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer)5 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext)5 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)4 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.grpc.producer.EventMeshProducer)4 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext)4 AclException (org.apache.eventmesh.api.exception.AclException)3 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)3 SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)2 ProducerManager (org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager)2 CompleteHandler (org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler)2 UpStreamMsgContext (org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext)2 CloudEventData (io.cloudevents.CloudEventData)1 CloudEventBuilder (io.cloudevents.core.builder.CloudEventBuilder)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1