You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by jk...@apache.org on 2017/02/28 04:40:06 UTC

thrift git commit: THRIFT-1805 Provide option for handling RTEs Client: Java

Repository: thrift
Updated Branches:
  refs/heads/master e2bc97275 -> 5038466e5


THRIFT-1805 Provide option for handling RTEs
Client: Java

Adds a Java option to the generator to generate code which lets Thrift
handle RuntimeExceptions from a service, and present them as
TApplicationException to the client.

This closes #1186


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/5038466e
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/5038466e
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/5038466e

Branch: refs/heads/master
Commit: 5038466e5e57b17b881684bae0e541408aafac0e
Parents: e2bc972
Author: Christopher Tubbs <ct...@apache.org>
Authored: Thu Feb 9 22:32:00 2017 -0500
Committer: James E. King, III <jk...@apache.org>
Committed: Mon Feb 27 23:38:44 2017 -0500

----------------------------------------------------------------------
 .../cpp/src/thrift/generate/t_java_generator.cc | 12 ++++++++
 .../src/org/apache/thrift/ProcessFunction.java  | 31 +++++++++++++-------
 2 files changed, 33 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/5038466e/compiler/cpp/src/thrift/generate/t_java_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/thrift/generate/t_java_generator.cc b/compiler/cpp/src/thrift/generate/t_java_generator.cc
index b732631..593eda6 100644
--- a/compiler/cpp/src/thrift/generate/t_java_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_java_generator.cc
@@ -70,6 +70,7 @@ public:
     use_option_type_ = false;
     undated_generated_annotations_  = false;
     suppress_generated_annotations_ = false;
+    handle_runtime_exceptions_ = false;
     for( iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
       if( iter->first.compare("beans") == 0) {
         bean_style_ = true;
@@ -91,6 +92,8 @@ public:
         reuse_objects_ = true;
       } else if( iter->first.compare("option_type") == 0) {
         use_option_type_ = true;
+      } else if( iter->first.compare("handle_runtime_exceptions") == 0) {
+        handle_runtime_exceptions_ = true;
       } else if( iter->first.compare("generated_annotations") == 0) {
         if( iter->second.compare("undated") == 0) {
           undated_generated_annotations_  = true;
@@ -367,6 +370,7 @@ private:
   bool use_option_type_;
   bool undated_generated_annotations_;
   bool suppress_generated_annotations_;
+  bool handle_runtime_exceptions_;
 
 };
 
@@ -3527,6 +3531,11 @@ void t_java_generator::generate_process_function(t_service* tservice, t_function
   indent(f_service_) << "  return " << ((tfunction->is_oneway()) ? "true" : "false") << ";" << endl;
   indent(f_service_) << "}" << endl << endl;
 
+  indent(f_service_) << "@Override" << endl;
+  indent(f_service_) << "protected boolean handleRuntimeExceptions() {" << endl;
+  indent(f_service_) << "  return " << ((handle_runtime_exceptions_) ? "true" : "false") << ";" << endl;
+  indent(f_service_) << "}" << endl << endl;
+
   indent(f_service_) << "public " << resultname << " getResult(I iface, " << argsname
                      << " args) throws org.apache.thrift.TException {" << endl;
   indent_up();
@@ -5265,6 +5274,9 @@ THRIFT_REGISTER_GENERATOR(
     "    android_legacy:  Do not use java.io.IOException(throwable) (available for Android 2.3 and "
     "above).\n"
     "    option_type:     Wrap optional fields in an Option type.\n"
+    "    handle_runtime_exceptions:\n"
+    "                     Send TApplicationException to the client when RuntimeException occurs on "
+    "the server. (Default behavior is to close the connection instead.)\n"
     "    java5:           Generate Java 1.5 compliant code (includes android_legacy flag).\n"
     "    reuse-objects:   Data objects will not be allocated, but existing instances will be used "
     "(read and write).\n"

http://git-wip-us.apache.org/repos/asf/thrift/blob/5038466e/lib/java/src/org/apache/thrift/ProcessFunction.java
----------------------------------------------------------------------
diff --git a/lib/java/src/org/apache/thrift/ProcessFunction.java b/lib/java/src/org/apache/thrift/ProcessFunction.java
index 992e859..5c039fe 100644
--- a/lib/java/src/org/apache/thrift/ProcessFunction.java
+++ b/lib/java/src/org/apache/thrift/ProcessFunction.java
@@ -1,6 +1,3 @@
-/**
- * 
- */
 package org.apache.thrift;
 
 import org.apache.thrift.protocol.TMessage;
@@ -39,13 +36,12 @@ public abstract class ProcessFunction<I, T extends TBase> {
       result = getResult(iface, args);
     } catch(TException tex) {
       LOGGER.error("Internal error processing " + getMethodName(), tex);
-      if (!isOneway()) {
-        TApplicationException x = new TApplicationException(TApplicationException.INTERNAL_ERROR, 
-          "Internal error processing " + getMethodName());
-        oprot.writeMessageBegin(new TMessage(getMethodName(), TMessageType.EXCEPTION, seqid));
-        x.write(oprot);
-        oprot.writeMessageEnd();
-        oprot.getTransport().flush();
+      handleException(seqid, oprot);
+      return;
+    } catch(RuntimeException rex) {
+      LOGGER.error("Internal error processing " + getMethodName(), rex);
+      if (handleRuntimeExceptions()) {
+        handleException(seqid, oprot);
       }
       return;
     }
@@ -58,6 +54,21 @@ public abstract class ProcessFunction<I, T extends TBase> {
     }
   }
 
+  private void handleException(int seqid, TProtocol oprot) throws TException {
+    if (!isOneway()) {
+      TApplicationException x = new TApplicationException(TApplicationException.INTERNAL_ERROR,
+        "Internal error processing " + getMethodName());
+      oprot.writeMessageBegin(new TMessage(getMethodName(), TMessageType.EXCEPTION, seqid));
+      x.write(oprot);
+      oprot.writeMessageEnd();
+      oprot.getTransport().flush();
+    }
+  }
+
+  protected boolean handleRuntimeExceptions() {
+    return false;
+  }
+
   protected abstract boolean isOneway();
 
   public abstract TBase getResult(I iface, T args) throws TException;