In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to increase the serialization output of java8 objects in golang". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. New Features1.1 add GetStackTrace method into Throwabler and its implements. # 207
When the go language client requests the java language service, if the java language throws an exception, the stack information corresponding to the exception is saved in StackTraceElement.
This exception information is best printed in the log to facilitate the client to troubleshoot the problem, so the acquisition of StackTraceElement is added in the Throwabler and corresponding subclasses.
Note: there is actually a better way to do this. All specific exception types include java_exception/exception.go 's Throwable struct. All you need to do is add the GetStackTrace method to Throwable. But this approach requires more testing and verification, and the logic of the changes will be relatively complicated. But the code will be cleaner. This method is not used here.
1.2 catch user defined exceptions. # 208
Add a serialization output method for Exception objects in java to golang:
Func JavaException () [] byte {e: = hessian.NewEncoder () exception: = java_exception.NewException ("java_exception") e.Encode (exception) return e.Buffer ()}
Provide a call entry in output/output.go: add the following function initialization declaration
Func init () {funcMap ["JavaException"] = testfuncs.JavaException}
Add the serialization result of calling go method to java code: note: Assert.assertEquals cannot directly compare whether Exception objects are equal.
/ * test java java.lang.Exception object and go java_exception Exception struct * / @ Test public void testException () {Exception exception = new Exception ("java_exception"); Object javaException = GoTestUtil.readGoObject ("JavaException"); if (javaException instanceof Exception) {Assert.assertEquals (exception.getMessage (), ((Exception) javaException). GetMessage ());} 1.3 support java8 time object. # 212, # 221
Add a serialization output method to the java8 object in golang:
/ / test java8 java.time.Yearfunc Java8TimeYear () [] byte {e: = hessian.NewEncoder () year: = java8_time.Year {Year: 2020} e.Encode (year) return e.Buffer ()} / / test java8 java.time.LocalDatefunc Java8LocalDate () [] byte {e: = hessian.NewEncoder () date: = java8_time.LocalDate {Year: 2020, Month: 9, Day: 12} e.Encode (date) return e.Buffer ()}
Provide call entry in output/output.go: add function initialization declaration
Func init () {funcMap ["Java8TimeYear"] = testfuncs.Java8TimeYear funcMap ["Java8LocalDate"] = testfuncs.Java8LocalDate}
Add the serialization result of calling the go method to the java code:
/ * test java8 java.time.* object and go java8_time/* struct * / @ Testpublic void testJava8Year () {Year year = Year.of (2020); Assert.assertEquals (year, GoTestUtil.readGoObject ("Java8TimeYear")); LocalDate localDate = LocalDate.of (2020, 9, 12); Assert.assertEquals (localDate, GoTestUtil.readGoObject ("Java8LocalDate"));} 1.4 support test golang encoding data in java. # 213
In order to better test and verify the hessian library, it has been supported to test the serialized data of java in golang, but now it is added to test the serialized data of golang in java to achieve two-way test verification.
Add serialization output method in golang:
Func HelloWorldString () [] byte {e: = hessian.NewEncoder () e.Encode ("hello world") return e.Buffer ()}
Register the method with output/output.go
/ / add all output func here func init () {funcMap ["HelloWorldString"] = testfuncs.HelloWorldString}
Output/output.go provides the invocation entry:
Func main () {flag.Parse () if * funcName = "" {_, _ = fmt.Fprintln (os.Stderr, "func name required") os.Exit (1)} f, exist: = funcMap [* funcName] if! exist {_, _ = fmt.Fprintln (os.Stderr, "func name not exist:" * funcName) os.Exit (1)} defer func () {if err: = recover () Err! = nil {_, _ = fmt.Fprintln (os.Stderr, "error:", err) os.Exit (1)} () if _, err: = os.Stdout.Write (f ()); err! = nil {_, _ = fmt.Fprintln (os.Stderr, "call error:", err) os.Exit (1)} os.Exit (0)}
Add the serialization result of calling the go method to the java code:
Public class GoTestUtil {public static Object readGoObject (String func) {System.out.println ("read go data:" + func); try {Process process = Runtime.getRuntime () .exec ("go run output/output.go-func_name=" + func, null, new File ("..")) Int exitValue = process.waitFor (); if (exitValue! = 0) {Assert.fail (readString (process.getErrorStream (); return null;} InputStream is = process.getInputStream (); Hessian2Input input = new Hessian2Input (is); return input.readObject () } catch (Exception e) {e.printStackTrace (); return null;}} private static String readString (InputStream in) throws IOException {StringBuilder out = new StringBuilder (); InputStreamReader reader = new InputStreamReader (in, StandardCharsets.UTF_8); char [] buffer = new char [4096]; int bytesRead While ((bytesRead = reader.read (buffer))! =-1) {out.append (buffer, 0, bytesRead);} return out.toString ();}
Add java test code:
@ Testpublic void testHelloWordString () {Assert.assertEquals ("hello world", GoTestUtil.readGoObject ("HelloWorldString"));} 1.5 support java.sql.Time & java.sql.Date. # 219
Added java class java.sql.Time and java.sql.Date support, corresponding to hessian.Time and hessian.Date, respectively
2. Enhancement2.1 Export function EncNull. # 225
Open the hessian.EncNull method so that users can use it in specific situations.
3. Bugfixes3.1 fix enum encode error in request. # 203
It turns out that there is no judgment of enum type in the dubbo request object, this pr adds a judgment of whether it is a POJOEnum type or not.
3.2 fix [] byte field decoding issue. # 216
Before v1.7.0, if the struct contains a [] byte field that cannot be deserialized, the error "error list tag: 0x29" is mainly due to being treated as list. In this case, it should be processed according to binary data.
Type Circular struct {Num int Previous * Circular Next * Circular ResponseDataBytes [] byte / /
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.