use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.
the class MQClientAPIImplTest method testSendMessageOneWay_WithException.
@Test
public void testSendMessageOneWay_WithException() throws RemotingException, InterruptedException, MQBrokerException {
doThrow(new RemotingTimeoutException("Remoting Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
try {
mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
failBecauseExceptionWasNotThrown(RemotingException.class);
} catch (RemotingException e) {
assertThat(e).hasMessage("Remoting Exception in Test");
}
doThrow(new InterruptedException("Interrupted Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
try {
mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
failBecauseExceptionWasNotThrown(InterruptedException.class);
} catch (InterruptedException e) {
assertThat(e).hasMessage("Interrupted Exception in Test");
}
}
use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.
the class AdminBrokerProcessor method callConsumer.
private RemotingCommand callConsumer(final int requestCode, final RemotingCommand request, final String consumerGroup, final String clientId) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null);
ClientChannelInfo clientChannelInfo = this.brokerController.getConsumerManager().findChannel(consumerGroup, clientId);
if (null == clientChannelInfo) {
response.setCode(ResponseCode.SYSTEM_ERROR);
response.setRemark(String.format("The Consumer <%s> <%s> not online", consumerGroup, clientId));
return response;
}
if (clientChannelInfo.getVersion() < MQVersion.Version.V3_1_8_SNAPSHOT.ordinal()) {
response.setCode(ResponseCode.SYSTEM_ERROR);
response.setRemark(String.format("The Consumer <%s> Version <%s> too low to finish, please upgrade it to V3_1_8_SNAPSHOT", clientId, MQVersion.getVersionDesc(clientChannelInfo.getVersion())));
return response;
}
try {
RemotingCommand newRequest = RemotingCommand.createRequestCommand(requestCode, null);
newRequest.setExtFields(request.getExtFields());
newRequest.setBody(request.getBody());
return this.brokerController.getBroker2Client().callClient(clientChannelInfo.getChannel(), newRequest);
} catch (RemotingTimeoutException e) {
response.setCode(ResponseCode.CONSUME_MSG_TIMEOUT);
response.setRemark(String.format("consumer <%s> <%s> Timeout: %s", consumerGroup, clientId, RemotingHelper.exceptionSimpleDesc(e)));
return response;
} catch (Exception e) {
response.setCode(ResponseCode.SYSTEM_ERROR);
response.setRemark(String.format("invoke consumer <%s> <%s> Exception: %s", consumerGroup, clientId, RemotingHelper.exceptionSimpleDesc(e)));
return response;
}
}
use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.
the class NettyRemotingAbstract method invokeSyncImpl.
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
final int opaque = request.getOpaque();
try {
final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, null, null);
this.responseTable.put(opaque, responseFuture);
final SocketAddress addr = channel.remoteAddress();
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) throws Exception {
if (f.isSuccess()) {
responseFuture.setSendRequestOK(true);
return;
} else {
responseFuture.setSendRequestOK(false);
}
responseTable.remove(opaque);
responseFuture.setCause(f.cause());
responseFuture.putResponse(null);
log.warn("send a request command to channel <" + addr + "> failed.");
}
});
RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
if (null == responseCommand) {
if (responseFuture.isSendRequestOK()) {
throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis, responseFuture.getCause());
} else {
throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
}
}
return responseCommand;
} finally {
this.responseTable.remove(opaque);
}
}
use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.
the class NettyRemotingAbstract method invokeAsyncImpl.
public void invokeAsyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis, final InvokeCallback invokeCallback) throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException {
final int opaque = request.getOpaque();
boolean acquired = this.semaphoreAsync.tryAcquire(timeoutMillis, TimeUnit.MILLISECONDS);
if (acquired) {
final SemaphoreReleaseOnlyOnce once = new SemaphoreReleaseOnlyOnce(this.semaphoreAsync);
final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, invokeCallback, once);
this.responseTable.put(opaque, responseFuture);
try {
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) throws Exception {
if (f.isSuccess()) {
responseFuture.setSendRequestOK(true);
return;
} else {
responseFuture.setSendRequestOK(false);
}
responseFuture.putResponse(null);
responseTable.remove(opaque);
try {
executeInvokeCallback(responseFuture);
} catch (Throwable e) {
log.warn("excute callback in writeAndFlush addListener, and callback throw", e);
} finally {
responseFuture.release();
}
log.warn("send a request command to channel <{}> failed.", RemotingHelper.parseChannelRemoteAddr(channel));
}
});
} catch (Exception e) {
responseFuture.release();
log.warn("send a request command to channel <" + RemotingHelper.parseChannelRemoteAddr(channel) + "> Exception", e);
throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel), e);
}
} else {
if (timeoutMillis <= 0) {
throw new RemotingTooMuchRequestException("invokeAsyncImpl invoke too fast");
} else {
String info = String.format("invokeAsyncImpl tryAcquire semaphore timeout, %dms, waiting thread nums: %d semaphoreAsyncValue: %d", timeoutMillis, this.semaphoreAsync.getQueueLength(), this.semaphoreAsync.availablePermits());
log.warn(info);
throw new RemotingTimeoutException(info);
}
}
}
use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.
the class RemotingHelper method invokeSync.
public static RemotingCommand invokeSync(final String addr, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException {
long beginTime = System.currentTimeMillis();
SocketAddress socketAddress = RemotingUtil.string2SocketAddress(addr);
SocketChannel socketChannel = RemotingUtil.connect(socketAddress);
if (socketChannel != null) {
boolean sendRequestOK = false;
try {
socketChannel.configureBlocking(true);
// bugfix http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4614802
socketChannel.socket().setSoTimeout((int) timeoutMillis);
ByteBuffer byteBufferRequest = request.encode();
while (byteBufferRequest.hasRemaining()) {
int length = socketChannel.write(byteBufferRequest);
if (length > 0) {
if (byteBufferRequest.hasRemaining()) {
if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
throw new RemotingSendRequestException(addr);
}
}
} else {
throw new RemotingSendRequestException(addr);
}
Thread.sleep(1);
}
sendRequestOK = true;
ByteBuffer byteBufferSize = ByteBuffer.allocate(4);
while (byteBufferSize.hasRemaining()) {
int length = socketChannel.read(byteBufferSize);
if (length > 0) {
if (byteBufferSize.hasRemaining()) {
if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
throw new RemotingTimeoutException(addr, timeoutMillis);
}
}
} else {
throw new RemotingTimeoutException(addr, timeoutMillis);
}
Thread.sleep(1);
}
int size = byteBufferSize.getInt(0);
ByteBuffer byteBufferBody = ByteBuffer.allocate(size);
while (byteBufferBody.hasRemaining()) {
int length = socketChannel.read(byteBufferBody);
if (length > 0) {
if (byteBufferBody.hasRemaining()) {
if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
throw new RemotingTimeoutException(addr, timeoutMillis);
}
}
} else {
throw new RemotingTimeoutException(addr, timeoutMillis);
}
Thread.sleep(1);
}
byteBufferBody.flip();
return RemotingCommand.decode(byteBufferBody);
} catch (IOException e) {
log.error("invokeSync failure", e);
if (sendRequestOK) {
throw new RemotingTimeoutException(addr, timeoutMillis);
} else {
throw new RemotingSendRequestException(addr);
}
} finally {
try {
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
throw new RemotingConnectException(addr);
}
}
Aggregations