Search in sources :

Example 1 with ErrorResponse

use of pers.cy.iris.commons.network.protocol.response.ErrorResponse in project iris by chicc999.

the class DefaultDispatcherHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Command command) throws Exception {
    if (command == null) {
        return;
    }
    logger.debug("dispatcher command : {} {}", command.getHeader().getHeaderType().toString(), command.getHeader().getTypeString());
    HeaderType headerType = command.getHeader().getHeaderType();
    switch(headerType) {
        case REQUEST:
            {
                // 如果是请求命令, 做请求处理
                try {
                    processRequest(ctx, command);
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                    //如果请求需要响应
                    if (command.getHeader().getAcknowledge() != Acknowledge.ACK_NO) {
                        //写出响应,如果出现异常则调用exceptionCaught打印异常关闭连接
                        ctx.writeAndFlush(new ErrorResponse(-1, e.toString(), command.getRequestId())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
                    }
                }
                break;
            }
        case RESPONSE:
            {
                // 如果是响应命令, 做响应处理
                processResponse(ctx, command);
                break;
            }
    }
}
Also used : HeaderType(pers.cy.iris.commons.network.protocol.HeaderType) UnknowCommandException(pers.cy.iris.commons.exception.UnknowCommandException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ServiceTooBusyException(pers.cy.iris.commons.exception.ServiceTooBusyException) ErrorResponse(pers.cy.iris.commons.network.protocol.response.ErrorResponse)

Example 2 with ErrorResponse

use of pers.cy.iris.commons.network.protocol.response.ErrorResponse in project iris by chicc999.

the class HandlerTask method run.

@Override
public void run() {
    // 处理请求命令
    Header header = request.getHeader();
    Command response = null;
    try {
        response = handler.process(ctx, request);
    } catch (Throwable e) {
        //如果请求需要答复
        if (request.getHeader().getAcknowledge() != Acknowledge.ACK_NO) {
            //写出响应,如果出现异常则调用exceptionCaught打印异常关闭连接
            ctx.writeAndFlush(new ErrorResponse(-1, e.getMessage(), request.getRequestId())).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        }
    }
    response.getHeader().setRequestId(request.getRequestId());
    ChannelFutureListener listenner = response.getListenner() == null ? ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE : response.getListenner();
    //服务端不处理commandCallback,直接在DispatcherHandler处理对应type.客户端则在发送时指定回调函数.
    ctx.writeAndFlush(response).addListener(listenner);
}
Also used : Header(pers.cy.iris.commons.network.protocol.Header) Command(pers.cy.iris.commons.network.protocol.Command) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ErrorResponse(pers.cy.iris.commons.network.protocol.response.ErrorResponse)

Example 3 with ErrorResponse

use of pers.cy.iris.commons.network.protocol.response.ErrorResponse in project iris by chicc999.

the class PutMessageHandler method process.

@Override
public Command process(ChannelHandlerContext ctx, Command command) throws Exception {
    PutMessage putMessage = (PutMessage) command;
    System.out.println("收到putMessage请求," + new String(putMessage.getMessages()[0].getBody()));
    Message[] messages = putMessage.getMessages();
    for (Message message : messages) {
        store.putMessage((StoreMessage) message);
    }
    return new ErrorResponse(200, "", command.getRequestId());
}
Also used : Message(pers.cy.iris.commons.model.message.Message) PutMessage(pers.cy.iris.commons.network.protocol.request.PutMessage) StoreMessage(pers.cy.iris.commons.model.message.StoreMessage) PutMessage(pers.cy.iris.commons.network.protocol.request.PutMessage) ErrorResponse(pers.cy.iris.commons.network.protocol.response.ErrorResponse)

Aggregations

ErrorResponse (pers.cy.iris.commons.network.protocol.response.ErrorResponse)3 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ServiceTooBusyException (pers.cy.iris.commons.exception.ServiceTooBusyException)1 UnknowCommandException (pers.cy.iris.commons.exception.UnknowCommandException)1 Message (pers.cy.iris.commons.model.message.Message)1 StoreMessage (pers.cy.iris.commons.model.message.StoreMessage)1 Command (pers.cy.iris.commons.network.protocol.Command)1 Header (pers.cy.iris.commons.network.protocol.Header)1 HeaderType (pers.cy.iris.commons.network.protocol.HeaderType)1 PutMessage (pers.cy.iris.commons.network.protocol.request.PutMessage)1