Search in sources :

Example 1 with Header

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

the class DefaultDispatcherHandler method processResponse.

private void processResponse(final ChannelHandlerContext ctx, final Command command) {
    Header header = command.getHeader();
    // 超时被删除了
    final ResponseFuture responseFuture = futures.remove(header.getRequestId());
    if (responseFuture == null) {
        logger.info("ack type:" + header.getTypeString() + " requestId:" + header.getRequestId() + " but responseFuture is null");
        return;
    }
    responseFuture.setResponse(command);
    //回调交给线程池,避免回调任务阻塞IO线程
    try {
        serviceExecutor.submit(new Runnable() {

            @Override
            public void run() {
                responseFuture.done();
            }
        });
    } catch (RejectedExecutionException e) {
        //队列已满,拒绝执行回调
        responseFuture.cancel(new ServiceTooBusyException("请求成功,但没有足够的资源执行回调", e));
    }
}
Also used : ServiceTooBusyException(pers.cy.iris.commons.exception.ServiceTooBusyException) Header(pers.cy.iris.commons.network.protocol.Header) ResponseFuture(pers.cy.iris.commons.network.ResponseFuture) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with Header

use of pers.cy.iris.commons.network.protocol.Header 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 Header

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

the class DefaultDispatcherHandler method processRequest.

private void processRequest(ChannelHandlerContext ctx, Command command) throws Exception {
    int type = command.getHeader().getType();
    if (Command.HEARTBEAT == type) {
        //双向心跳,无需处理
        return;
    }
    Header header = command.getHeader();
    CommandHandler handler = handlerFactory.getHandler(header.getType());
    if (handler == null) {
        throw new UnknowCommandException("处理" + header.getTypeString() + "的handler不存在");
    }
    try {
        HandlerTask task = new HandlerTask(ctx, command, handler);
        ExecutorService commandExecutor = handler.getExecutorService(command);
        if (commandExecutor == null) {
            // 如果没用指定处理该命令专用的线程池,则在普通业务线程池执行
            this.serviceExecutor.submit(task);
        } else {
            commandExecutor.submit(task);
        }
    } catch (RejectedExecutionException e) {
        throw new ServiceTooBusyException("too many requests and thread pool is busy, reject request %d from %s ", e);
    }
}
Also used : ServiceTooBusyException(pers.cy.iris.commons.exception.ServiceTooBusyException) Header(pers.cy.iris.commons.network.protocol.Header) HandlerTask(pers.cy.iris.commons.network.HandlerTask) ExecutorService(java.util.concurrent.ExecutorService) UnknowCommandException(pers.cy.iris.commons.exception.UnknowCommandException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

Header (pers.cy.iris.commons.network.protocol.Header)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 ServiceTooBusyException (pers.cy.iris.commons.exception.ServiceTooBusyException)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ExecutorService (java.util.concurrent.ExecutorService)1 UnknowCommandException (pers.cy.iris.commons.exception.UnknowCommandException)1 HandlerTask (pers.cy.iris.commons.network.HandlerTask)1 ResponseFuture (pers.cy.iris.commons.network.ResponseFuture)1 Command (pers.cy.iris.commons.network.protocol.Command)1 ErrorResponse (pers.cy.iris.commons.network.protocol.response.ErrorResponse)1