Search in sources :

Example 1 with UpStreamMsgContext

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext 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 2 with UpStreamMsgContext

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext 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

SendCallback (org.apache.eventmesh.api.SendCallback)2 SendResult (org.apache.eventmesh.api.SendResult)2 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)2 UpStreamMsgContext (org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext)2 Header (org.apache.eventmesh.common.protocol.tcp.Header)1 Package (org.apache.eventmesh.common.protocol.tcp.Package)1 EventMeshTcpSendResult (org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult)1