List of usage examples for org.apache.thrift TApplicationException TApplicationException
public TApplicationException(String message)
From source file:com.alibaba.dubbo.rpc.protocol.swift.ThriftCodec.java
License:Open Source License
private void encodeResponse(Channel channel, ChannelBuffer buffer, Response response) throws IOException { RpcResult result = (RpcResult) response.getResult(); RequestData rd = cachedRequest.get((int) response.getId()); String service = channel.getUrl().getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME); ClassNameGenerator generator = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class) .getExtension(service);//www . j ava 2 s. c o m String resultClassName = generator.generateResultClassName(rd.serviceName, rd.methodName); if (StringUtils.isEmpty(resultClassName)) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, new StringBuilder(32) .append("Could not encode response, the specified interface may be incorrect.").toString()); } Class clazz = cachedClass.get(resultClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(resultClassName); cachedClass.putIfAbsent(resultClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase resultObj; try { resultObj = (TBase) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } TApplicationException applicationException = null; TMessage message; if (result.hasException()) { Throwable throwable = result.getException(); int index = 1; boolean found = false; while (true) { TFieldIdEnum fieldIdEnum = resultObj.fieldForId(index++); if (fieldIdEnum == null) { break; } String fieldName = fieldIdEnum.getFieldName(); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); String setMethodName = ThriftUtils.generateSetMethodName(fieldName); Method getMethod; Method setMethod; try { getMethod = clazz.getMethod(getMethodName); if (getMethod.getReturnType().equals(throwable.getClass())) { found = true; setMethod = clazz.getMethod(setMethodName, throwable.getClass()); setMethod.invoke(resultObj, throwable); } } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } if (!found) { applicationException = new TApplicationException(throwable.getMessage()); } } else { Object realResult = result.getResult(); // result field id is 0 String fieldName = resultObj.fieldForId(0).getFieldName(); String setMethodName = ThriftUtils.generateSetMethodName(fieldName); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); Method getMethod; Method setMethod; try { getMethod = clazz.getMethod(getMethodName); setMethod = clazz.getMethod(setMethodName, getMethod.getReturnType()); setMethod.invoke(resultObj, realResult); } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } if (applicationException != null) { message = new TMessage(rd.methodName, TMessageType.EXCEPTION, rd.id); } else { message = new TMessage(rd.methodName, TMessageType.REPLY, rd.id); } RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024); TIOStreamTransport transport = new TIOStreamTransport(bos); TBinaryProtocol protocol = new TBinaryProtocol(transport); try { protocol.writeMessageBegin(message); switch (message.type) { case TMessageType.EXCEPTION: applicationException.write(protocol); break; case TMessageType.REPLY: resultObj.write(protocol); break; } protocol.writeMessageEnd(); protocol.getTransport().flush(); } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } buffer.writeBytes(bos.toByteArray()); }
From source file:com.alibaba.dubbo.rpc.protocol.thrift.ThriftCodec.java
License:Open Source License
private void encodeResponse(Channel channel, OutputStream output, Response response) throws IOException { RpcResult result = (RpcResult) response.getResult(); RequestData rd = cachedRequest.get(response.getId()); String resultClassName = ExtensionLoader .getExtensionLoader(ClassNameGenerator.class).getExtension(channel.getUrl() .getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME)) .generateResultClassName(rd.serviceName, rd.methodName); if (StringUtils.isEmpty(resultClassName)) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, new StringBuilder(32) .append("Could not encode response, the specified interface may be incorrect.").toString()); }//from w w w .j a v a 2s .c o m Class clazz = cachedClass.get(resultClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(resultClassName); cachedClass.putIfAbsent(resultClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase resultObj; try { resultObj = (TBase) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } TApplicationException applicationException = null; TMessage message; if (result.hasException()) { Throwable throwable = result.getException(); int index = 1; boolean found = false; while (true) { TFieldIdEnum fieldIdEnum = resultObj.fieldForId(index++); if (fieldIdEnum == null) { break; } String fieldName = fieldIdEnum.getFieldName(); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); String setMethodName = ThriftUtils.generateSetMethodName(fieldName); Method getMethod; Method setMethod; try { getMethod = clazz.getMethod(getMethodName); if (getMethod.getReturnType().equals(throwable.getClass())) { found = true; setMethod = clazz.getMethod(setMethodName, throwable.getClass()); setMethod.invoke(resultObj, throwable); } } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } if (!found) { applicationException = new TApplicationException(throwable.getMessage()); } } else { Object realResult = result.getResult(); // result field id is 0 String fieldName = resultObj.fieldForId(0).getFieldName(); String setMethodName = ThriftUtils.generateSetMethodName(fieldName); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); Method getMethod; Method setMethod; try { getMethod = clazz.getMethod(getMethodName); setMethod = clazz.getMethod(setMethodName, getMethod.getReturnType()); setMethod.invoke(resultObj, realResult); } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } if (applicationException != null) { message = new TMessage(rd.methodName, TMessageType.EXCEPTION, rd.id); } else { message = new TMessage(rd.methodName, TMessageType.REPLY, rd.id); } RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024); TIOStreamTransport transport = new TIOStreamTransport(bos); TBinaryProtocol protocol = new TBinaryProtocol(transport); int messageLength; int headerLength; byte[] bytes = new byte[4]; try { // magic protocol.writeI16(MAGIC); // message length protocol.writeI32(Integer.MAX_VALUE); // message header length protocol.writeI16(Short.MAX_VALUE); // version protocol.writeByte(VERSION); // service name protocol.writeString(rd.serviceName); // id protocol.writeI64(response.getId()); protocol.getTransport().flush(); headerLength = bos.size(); // message protocol.writeMessageBegin(message); switch (message.type) { case TMessageType.EXCEPTION: applicationException.write(protocol); break; case TMessageType.REPLY: resultObj.write(protocol); break; } protocol.writeMessageEnd(); protocol.getTransport().flush(); int oldIndex = messageLength = bos.size(); try { TFramedTransport.encodeFrameSize(messageLength, bytes); bos.setWriteIndex(MESSAGE_LENGTH_INDEX); protocol.writeI32(messageLength); bos.setWriteIndex(MESSAGE_HEADER_LENGTH_INDEX); protocol.writeI16((short) (0xffff & headerLength)); } finally { bos.setWriteIndex(oldIndex); } } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } output.write(bytes); bos.writeTo(output); output.flush(); }
From source file:com.linecorp.armeria.client.thrift.ThriftClientDelegate.java
License:Apache License
private Object decodeResponse(ThriftFunction method, HttpData content) throws TException { if (content.isEmpty()) { if (method.isOneway()) { return null; }/*from w w w . ja va 2 s . c om*/ throw new TApplicationException(TApplicationException.MISSING_RESULT); } final TMemoryInputTransport inputTransport = new TMemoryInputTransport(content.array(), content.offset(), content.length()); final TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport); final TMessage msg = inputProtocol.readMessageBegin(); if (msg.type == TMessageType.EXCEPTION) { TApplicationException ex = TApplicationException.read(inputProtocol); inputProtocol.readMessageEnd(); throw ex; } if (!method.name().equals(msg.name)) { throw new TApplicationException(TApplicationException.WRONG_METHOD_NAME, msg.name); } TBase<? extends TBase<?, ?>, TFieldIdEnum> result = method.newResult(); result.read(inputProtocol); inputProtocol.readMessageEnd(); for (TFieldIdEnum fieldIdEnum : method.exceptionFields()) { if (result.isSet(fieldIdEnum)) { throw (TException) result.getFieldValue(fieldIdEnum); } } TFieldIdEnum successField = method.successField(); if (successField == null) { // void method return null; } if (result.isSet(successField)) { return result.getFieldValue(successField); } throw new TApplicationException(TApplicationException.MISSING_RESULT, result.getClass().getName() + '.' + successField.getFieldName()); }
From source file:com.linecorp.armeria.client.thrift.THttpClientDelegate.java
License:Apache License
private void handle(ClientRequestContext ctx, DefaultRpcResponse reply, ThriftFunction func, HttpData content) throws TException { if (func.isOneWay()) { handleSuccess(ctx, reply, null, null); return;/* w w w . j a va 2s .c o m*/ } if (content.isEmpty()) { throw new TApplicationException(TApplicationException.MISSING_RESULT); } final TMemoryInputTransport inputTransport = new TMemoryInputTransport(content.array(), content.offset(), content.length()); final TProtocol inputProtocol = protocolFactory.getProtocol(inputTransport); final TMessage header = inputProtocol.readMessageBegin(); final TApplicationException appEx = readApplicationException(func, inputProtocol, header); if (appEx != null) { handleException(ctx, reply, new ThriftReply(header, appEx), appEx); return; } TBase<? extends TBase<?, ?>, TFieldIdEnum> result = func.newResult(); result.read(inputProtocol); inputProtocol.readMessageEnd(); final ThriftReply rawResponseContent = new ThriftReply(header, result); for (TFieldIdEnum fieldIdEnum : func.exceptionFields()) { if (result.isSet(fieldIdEnum)) { final TException cause = (TException) ThriftFieldAccess.get(result, fieldIdEnum); handleException(ctx, reply, rawResponseContent, cause); return; } } final TFieldIdEnum successField = func.successField(); if (successField == null) { // void method handleSuccess(ctx, reply, null, rawResponseContent); return; } if (result.isSet(successField)) { final Object returnValue = ThriftFieldAccess.get(result, successField); handleSuccess(ctx, reply, returnValue, rawResponseContent); return; } handleException(ctx, reply, rawResponseContent, new TApplicationException(TApplicationException.MISSING_RESULT, result.getClass().getName() + '.' + successField.getFieldName())); }
From source file:com.vmware.photon.controller.common.thrift.ClientProxyImplTest.java
License:Open Source License
@Test public void testFailedProxyMethodCallWithApplicationException() throws Exception { when(clientPool.acquire()).thenReturn(Futures.immediateFuture(client)); mockCallError(client, new TApplicationException("Something happened")); ClientProxyImpl<AsyncClient> proxy = new ClientProxyImpl<>(executor, typeLiteral, clientPool); try {// w ww . j a va2s . c om performEchoCall(proxy.get(), "foobar"); fail(); } catch (TException e) { assertThat(e.getMessage(), is("Something happened")); } verify(clientPool).acquire(); verify(clientPool).release(client, true); verifyNoMoreInteractions(clientPool); }
From source file:org.apache.dubbo.rpc.protocol.thrift.ThriftCodec.java
License:Apache License
private void encodeResponse(Channel channel, ChannelBuffer buffer, Response response) throws IOException { RpcResult result = (RpcResult) response.getResult(); RequestData rd = cachedRequest.get(response.getId()); String resultClassName = ExtensionLoader .getExtensionLoader(ClassNameGenerator.class).getExtension(channel.getUrl() .getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME)) .generateResultClassName(rd.serviceName, rd.methodName); if (StringUtils.isEmpty(resultClassName)) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "Could not encode response, the specified interface may be incorrect."); }/*from w ww. ja v a 2 s . c o m*/ Class clazz = cachedClass.get(resultClassName); if (clazz == null) { try { clazz = ClassHelper.forNameWithThreadContextClassLoader(resultClassName); cachedClass.putIfAbsent(resultClassName, clazz); } catch (ClassNotFoundException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } TBase resultObj; try { resultObj = (TBase) clazz.newInstance(); } catch (InstantiationException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } TApplicationException applicationException = null; TMessage message; if (result.hasException()) { Throwable throwable = result.getException(); int index = 1; boolean found = false; while (true) { TFieldIdEnum fieldIdEnum = resultObj.fieldForId(index++); if (fieldIdEnum == null) { break; } String fieldName = fieldIdEnum.getFieldName(); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); String setMethodName = ThriftUtils.generateSetMethodName(fieldName); Method getMethod; Method setMethod; try { getMethod = clazz.getMethod(getMethodName); if (getMethod.getReturnType().equals(throwable.getClass())) { found = true; setMethod = clazz.getMethod(setMethodName, throwable.getClass()); setMethod.invoke(resultObj, throwable); } } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } if (!found) { applicationException = new TApplicationException(throwable.getMessage()); } } else { Object realResult = result.getResult(); // result field id is 0 String fieldName = resultObj.fieldForId(0).getFieldName(); String setMethodName = ThriftUtils.generateSetMethodName(fieldName); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); Method getMethod; Method setMethod; try { getMethod = clazz.getMethod(getMethodName); setMethod = clazz.getMethod(setMethodName, getMethod.getReturnType()); setMethod.invoke(resultObj, realResult); } catch (NoSuchMethodException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (InvocationTargetException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } catch (IllegalAccessException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } } if (applicationException != null) { message = new TMessage(rd.methodName, TMessageType.EXCEPTION, rd.id); } else { message = new TMessage(rd.methodName, TMessageType.REPLY, rd.id); } RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024); TIOStreamTransport transport = new TIOStreamTransport(bos); TBinaryProtocol protocol = new TBinaryProtocol(transport); int messageLength; int headerLength; byte[] bytes = new byte[4]; try { // magic protocol.writeI16(MAGIC); // message length protocol.writeI32(Integer.MAX_VALUE); // message header length protocol.writeI16(Short.MAX_VALUE); // version protocol.writeByte(VERSION); // service name protocol.writeString(rd.serviceName); // id protocol.writeI64(response.getId()); protocol.getTransport().flush(); headerLength = bos.size(); // message protocol.writeMessageBegin(message); switch (message.type) { case TMessageType.EXCEPTION: applicationException.write(protocol); break; case TMessageType.REPLY: resultObj.write(protocol); break; } protocol.writeMessageEnd(); protocol.getTransport().flush(); int oldIndex = messageLength = bos.size(); try { TFramedTransport.encodeFrameSize(messageLength, bytes); bos.setWriteIndex(MESSAGE_LENGTH_INDEX); protocol.writeI32(messageLength); bos.setWriteIndex(MESSAGE_HEADER_LENGTH_INDEX); protocol.writeI16((short) (0xffff & headerLength)); } finally { bos.setWriteIndex(oldIndex); } } catch (TException e) { throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e); } buffer.writeBytes(bytes); buffer.writeBytes(bos.toByteArray()); }
From source file:org.apache.hadoop.corona.ClusterManager.java
License:Apache License
@Override public void sessionEnd(String handle, SessionStatus status) throws TException, InvalidSessionHandle, SafeModeException { checkSafeMode("sessionEnd"); try {/*w w w .jav a 2 s.c om*/ Session session = sessionManager.getSession(handle); InetAddress sessionAddr = session.getAddress(); LOG.info("sessionEnd called for session: " + handle + " on " + sessionAddr.getHost() + ":" + sessionAddr.getPort() + " with status: " + status); if (status == SessionStatus.TIMED_OUT) { if (session.getUrl() != null && session.getUrl().indexOf(handle) < 0) { metrics.timeoutRemoteJT(1); } } if (status == SessionStatus.FAILED_JOBTRACKER) { metrics.recordCJTFailure(); } Collection<ResourceGrant> canceledGrants = sessionManager.deleteSession(handle, status); if (canceledGrants == null) { return; } for (ResourceGrant grant : canceledGrants) { nodeManager.cancelGrant(grant.nodeName, handle, grant.id); metrics.releaseResource(grant.type); } scheduler.notifyScheduler(); sessionNotifier.deleteSession(handle); } catch (RuntimeException e) { LOG.error("Error in sessionEnd of " + handle, e); throw new TApplicationException(e.getMessage()); } }
From source file:org.apache.hadoop.corona.ClusterManager.java
License:Apache License
@Override public void sessionUpdateInfo(String handle, SessionInfo info) throws TException, InvalidSessionHandle, SafeModeException { checkSafeMode("sessionUpdateInfo"); try {//from w w w .j av a 2 s . co m LOG.info("sessionUpdateInfo called for session: " + handle + " with info: " + info); sessionManager.heartbeat(handle); sessionManager.updateInfo(handle, info); } catch (RuntimeException e) { throw new TApplicationException(e.getMessage()); } }
From source file:org.apache.hadoop.corona.ClusterManager.java
License:Apache License
@Override public void sessionHeartbeat(String handle) throws TException, InvalidSessionHandle, SafeModeException { checkSafeMode("sessionHeartbeat"); try {/*www .ja v a2s .com*/ sessionManager.heartbeat(handle); } catch (RuntimeException e) { throw new TApplicationException(e.getMessage()); } }
From source file:org.apache.hadoop.corona.ClusterManager.java
License:Apache License
@Override public void sessionHeartbeatV2(String handle, HeartbeatArgs jtInfo) throws TException, InvalidSessionHandle, SafeModeException { checkSafeMode("sessionHeartbeatV2"); try {//from ww w .ja v a 2s .c o m Session session = sessionManager.getSession(handle); if (!session.checkHeartbeatInfo(jtInfo)) { sessionEnd(session.getSessionId(), SessionStatus.FAILED_JOBTRACKER); } sessionManager.heartbeatV2(handle, jtInfo); } catch (RuntimeException e) { throw new TApplicationException(e.getMessage()); } }