Search in sources :

Example 1 with Command

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

the class NettyClientTest method init.

private void init() {
    nettyClient = new NettyClient(new NettyClientConfig());
    try {
        nettyClient.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        Channel channel = nettyClient.createChannelSync(new InetSocketAddress("localhost", 50088));
        PutMessage command = new PutMessage();
        command.setProducerId(new ProducerId(new ConnectionId(new ClientId("1.0", "localhost", System.currentTimeMillis()))));
        Message message = new Message("test", "第一条消息", "1");
        message.setApp("app");
        Message[] messages = new Message[1];
        messages[0] = message;
        command.setMessages(messages);
        Command command1 = nettyClient.sync(channel, command, 10000);
        if (command1.getHeader().getStatus() != 200) {
            System.out.println(command1.getHeader().getError());
        }
    } catch (ConnectException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (RemotingIOException e) {
        e.printStackTrace();
    }
}
Also used : Message(pers.cy.iris.commons.model.message.Message) PutMessage(pers.cy.iris.commons.network.protocol.request.PutMessage) ProducerId(pers.cy.iris.commons.network.netty.session.ProducerId) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) RemotingIOException(pers.cy.iris.commons.exception.RemotingIOException) RequestTimeoutException(pers.cy.iris.commons.exception.RequestTimeoutException) ConnectException(pers.cy.iris.commons.exception.ConnectException) RemotingIOException(pers.cy.iris.commons.exception.RemotingIOException) NettyClient(pers.cy.iris.commons.network.netty.client.NettyClient) RequestTimeoutException(pers.cy.iris.commons.exception.RequestTimeoutException) ConnectionId(pers.cy.iris.commons.network.netty.session.ConnectionId) Command(pers.cy.iris.commons.network.protocol.Command) NettyClientConfig(pers.cy.iris.commons.network.netty.client.NettyClientConfig) ClientId(pers.cy.iris.commons.network.netty.session.ClientId) PutMessage(pers.cy.iris.commons.network.protocol.request.PutMessage) ConnectException(pers.cy.iris.commons.exception.ConnectException)

Example 2 with Command

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

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

the class NettyTransport method sync.

@Override
public Command sync(Channel channel, Command command, int timeout) throws RemotingIOException, RequestTimeoutException {
    int sendTimeout = timeout <= 0 ? config.getSendTimeout() : timeout;
    // 同步调用
    ResponseFuture future = async(channel, command, null);
    future.setTimeout(sendTimeout);
    Command response;
    try {
        response = future.get(sendTimeout);
    } catch (InterruptedException e) {
        throw new RemotingIOException("线程被中断", e);
    }
    if (!future.isDone()) {
        throw new RequestTimeoutException("请求 requestId=" + command.getRequestId() + " 超时");
    }
    if (!future.isSuccess()) {
        throw new RemotingIOException(future.getCause());
    }
    return response;
}
Also used : RequestTimeoutException(pers.cy.iris.commons.exception.RequestTimeoutException) Command(pers.cy.iris.commons.network.protocol.Command) RemotingIOException(pers.cy.iris.commons.exception.RemotingIOException) ResponseFuture(pers.cy.iris.commons.network.ResponseFuture)

Aggregations

Command (pers.cy.iris.commons.network.protocol.Command)3 RemotingIOException (pers.cy.iris.commons.exception.RemotingIOException)2 RequestTimeoutException (pers.cy.iris.commons.exception.RequestTimeoutException)2 Channel (io.netty.channel.Channel)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 InetSocketAddress (java.net.InetSocketAddress)1 ConnectException (pers.cy.iris.commons.exception.ConnectException)1 Message (pers.cy.iris.commons.model.message.Message)1 ResponseFuture (pers.cy.iris.commons.network.ResponseFuture)1 NettyClient (pers.cy.iris.commons.network.netty.client.NettyClient)1 NettyClientConfig (pers.cy.iris.commons.network.netty.client.NettyClientConfig)1 ClientId (pers.cy.iris.commons.network.netty.session.ClientId)1 ConnectionId (pers.cy.iris.commons.network.netty.session.ConnectionId)1 ProducerId (pers.cy.iris.commons.network.netty.session.ProducerId)1 Header (pers.cy.iris.commons.network.protocol.Header)1 PutMessage (pers.cy.iris.commons.network.protocol.request.PutMessage)1 ErrorResponse (pers.cy.iris.commons.network.protocol.response.ErrorResponse)1