Search in sources :

Example 6 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class DefaultMQProducerImpl method checkTransactionState.

@Override
public void checkTransactionState(final String address, final String producerGroupe, final Message msg) {
    Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
                LocalTransactionState localState = producer.getTransactionCheckListener().checkLocalTransactionState(msg);
                CheckTransactionStateResponseHeader header = new CheckTransactionStateResponseHeader();
                header.setCommitOrRollback(localState.name());
                header.setMessageId(msg.getMessageId());
                header.setProducerGroup(defaultMQProducer.getProducerGroup());
                RemotingCommand request = RemotingCommand.buildRequestCmd(header, RequestCode.CHECK_TRANSACTION_RESPONSE);
                mqProducerFactory.getRemotingClient().invokeOneway(address, request, 0);
            } catch (Exception e) {
                log.error("check transaction state error:{}", e);
            }
        }
    };
    this.checkExecutor.submit(run);
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) LocalTransactionState(com.yanghui.elephant.common.constant.LocalTransactionState) CheckTransactionStateResponseHeader(com.yanghui.elephant.common.protocol.header.CheckTransactionStateResponseHeader) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer) RemotingSendRequestException(com.yanghui.elephant.remoting.exception.RemotingSendRequestException) MQClientException(com.yanghui.elephant.client.exception.MQClientException) RemotingConnectException(com.yanghui.elephant.remoting.exception.RemotingConnectException) RemotingTimeoutException(com.yanghui.elephant.remoting.exception.RemotingTimeoutException)

Example 7 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class NettyRemotingAbstract method handlerRequestProcessor.

private void handlerRequestProcessor(final ChannelHandlerContext ctx, final RemotingCommand request, Pair<RequestProcessor, ExecutorService> pair) {
    final RequestProcessor requestProcessor = pair.getObject1();
    ExecutorService executorService = pair.getObject2();
    if (executorService == null) {
        RemotingCommand respose = requestProcessor.processRequest(ctx, request);
        if (respose != null)
            ctx.writeAndFlush(respose);
        return;
    }
    executorService.submit(new Runnable() {

        @Override
        public void run() {
            RemotingCommand respose = requestProcessor.processRequest(ctx, request);
            if (respose != null)
                ctx.writeAndFlush(respose);
        }
    });
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) ExecutorService(java.util.concurrent.ExecutorService) RequestProcessor(com.yanghui.elephant.remoting.RequestProcessor)

Example 8 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class NettyRemotingAbstract method invokeSyncImpl.

public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingTimeoutException, RemotingSendRequestException {
    final int unique = request.getUnique();
    try {
        final SocketAddress addr = channel.remoteAddress();
        final ResponseFuture responseFuture = new ResponseFuture(unique, timeoutMillis);
        this.responseTable.put(unique, responseFuture);
        channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    responseFuture.setSendRequestOK(true);
                    return;
                }
                responseFuture.setSendRequestOK(false);
                responseTable.remove(unique);
                responseFuture.setCause(future.cause());
                responseFuture.putResponse(null);
                log.warn("send a request command to channel <" + addr + "> failed.");
            }
        });
        RemotingCommand respose = responseFuture.waitRespose(timeoutMillis);
        if (null == respose) {
            if (responseFuture.isSendRequestOK()) {
                throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis, responseFuture.getCause());
            } else {
                throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
            }
        }
        return respose;
    } finally {
        this.responseTable.remove(unique);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) RemotingSendRequestException(com.yanghui.elephant.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(com.yanghui.elephant.remoting.exception.RemotingTimeoutException) SocketAddress(java.net.SocketAddress) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingSendRequestException(com.yanghui.elephant.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(com.yanghui.elephant.remoting.exception.RemotingTimeoutException)

Example 9 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class MessageRequestProcessor method transactionMessageHandle.

private RemotingCommand transactionMessageHandle(SendMessageRequestHeader requestHeader, RemotingCommand request) {
    RemotingCommand response = RemotingCommand.buildResposeCmd(ResponseCode.SERVER_FAIL, request.getUnique());
    MessageEntity entity = buildMessageEntity(request.getBody(), requestHeader, true);
    int responseCode = saveMessage(entity);
    response.setCode(responseCode);
    return response;
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) MessageEntity(com.yanghui.elephant.store.entity.MessageEntity)

Example 10 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class TransactionCheckJob method execute.

@Override
public void execute(ShardingContext shardingContext) {
    List<MessageEntity> findList = this.messageEntityMapper.queryTransactionNotComplete();
    log.info("查询没有完成的事务消息(查询1分钟之前的)记录数:{}", findList);
    if (findList.isEmpty()) {
        return;
    }
    try {
        for (MessageEntity entity : findList) {
            CheckTransactionStateRequestHeader requestHeader = new CheckTransactionStateRequestHeader();
            requestHeader.setMessageId(entity.getMessageId());
            requestHeader.setDestination(entity.getDestination());
            requestHeader.setProducerGroup(entity.getGroup());
            if (!StringUtil.isNullOrEmpty(entity.getProperties())) {
                requestHeader.setProperties(entity.getProperties());
            }
            RemotingCommand request = RemotingCommand.buildRequestCmd(requestHeader, RequestCode.CHECK_TRANSACTION);
            request.setBody(entity.getBody());
            sentToClient(request);
        }
    } catch (Exception e) {
        log.error("回查发送异常:{}", e);
    }
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) MessageEntity(com.yanghui.elephant.store.entity.MessageEntity) CheckTransactionStateRequestHeader(com.yanghui.elephant.common.protocol.header.CheckTransactionStateRequestHeader)

Aggregations

RemotingCommand (com.yanghui.elephant.remoting.procotol.RemotingCommand)10 RemotingSendRequestException (com.yanghui.elephant.remoting.exception.RemotingSendRequestException)3 RemotingTimeoutException (com.yanghui.elephant.remoting.exception.RemotingTimeoutException)3 MessageEntity (com.yanghui.elephant.store.entity.MessageEntity)3 MQClientException (com.yanghui.elephant.client.exception.MQClientException)2 RemotingConnectException (com.yanghui.elephant.remoting.exception.RemotingConnectException)2 SendResult (com.yanghui.elephant.client.producer.SendResult)1 TransactionMQProducer (com.yanghui.elephant.client.producer.TransactionMQProducer)1 TransactionSendResult (com.yanghui.elephant.client.producer.TransactionSendResult)1 LocalTransactionState (com.yanghui.elephant.common.constant.LocalTransactionState)1 Message (com.yanghui.elephant.common.message.Message)1 CheckTransactionStateRequestHeader (com.yanghui.elephant.common.protocol.header.CheckTransactionStateRequestHeader)1 CheckTransactionStateResponseHeader (com.yanghui.elephant.common.protocol.header.CheckTransactionStateResponseHeader)1 EndTransactionRequestHeader (com.yanghui.elephant.common.protocol.header.EndTransactionRequestHeader)1 SendMessageRequestHeader (com.yanghui.elephant.common.protocol.header.SendMessageRequestHeader)1 RequestProcessor (com.yanghui.elephant.remoting.RequestProcessor)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 SocketAddress (java.net.SocketAddress)1 ExecutorService (java.util.concurrent.ExecutorService)1