{"id":137,"date":"2019-08-02T10:58:05","date_gmt":"2019-08-02T10:58:05","guid":{"rendered":"http:\/\/www.matez.de\/?p=137"},"modified":"2019-08-02T10:58:06","modified_gmt":"2019-08-02T10:58:06","slug":"fault-tolerance-for-jax-ws-with-resilience4j","status":"publish","type":"post","link":"https:\/\/www.matez.de\/index.php\/2019\/08\/02\/fault-tolerance-for-jax-ws-with-resilience4j\/","title":{"rendered":"fault tolerance for jax-ws with resilience4j"},"content":{"rendered":"\n<p>In a recent project, we still have to use SOAP webservices and I wanted to apply some resilience pattern such as <a href=\"https:\/\/docs.microsoft.com\/de-de\/azure\/architecture\/patterns\/retry\">retry <\/a>to my project.<br> Also a collegue just presented a java library called <a href=\"https:\/\/github.com\/resilience4j\/resilience4j\">resilience4j<\/a>  so I wanted to use that one.<\/p>\n\n\n\n<p>Of course, there are a lot of other possibilites like using other libraries (like Hystrix) or applying the <a href=\"https:\/\/docs.microsoft.com\/de-de\/azure\/architecture\/patterns\/sidecar\">sidecar pattern<\/a> outside of my application in a cluster. <\/p>\n\n\n\n<p>As you can see in the <a href=\"https:\/\/resilience4j.readme.io\/docs\/retry#section-decorate-and-execute-a-functional-interface\">documentation<\/a>, resilience4j is build for functional programming style and it supports some functional interfaces <br> which can be decorated to apply the retry mechanism to the function invocation. In the examples, you can always find a simple setup to pass the supplier and decorate it only for the particular method.<\/p>\n\n\n\n<p>In my use case, I do not want to write the decoration code for each and every method (or function). I have a third party WSDL, generated the webservice interface and port via <a href=\"https:\/\/javaee.github.io\/metro-jax-ws\/doc\/user-guide\/ch04.html#tools-wsimport-ant-task\">wsimport <\/a>and now I want to generically apply the retry mechanism to the webservice client. But my solution is not very complicated, it is using a reflection proxy and invocation handler to direcly decorate and execute the method via the Retry classes.<\/p>\n\n\n<div id=\"gist97574916\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-webservicefactory-java\" class=\"file my-2\">\n    \n    <div itemprop=\"text\" class=\"Box-body p-0 blob-wrapper data type-java  \" style=\"overflow: auto\" tabindex=\"0\" role=\"region\" aria-label=\"WebserviceFactory.java content, created by mwiede on 10:55AM on August 02, 2019.\">\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewbox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"%7B%7B%20revealButtonHref%20%7D%7D\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewbox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"WebserviceFactory.java\">\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-webservicefactory-java-LC1\" class=\"blob-code blob-code-inner js-file-line\">import java.lang.reflect.InvocationHandler;<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-webservicefactory-java-LC2\" class=\"blob-code blob-code-inner js-file-line\">import java.lang.reflect.Proxy;<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-webservicefactory-java-LC3\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-webservicefactory-java-LC4\" class=\"blob-code blob-code-inner js-file-line\">import io.github.resilience4j.retry.Retry;<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-webservicefactory-java-LC5\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-webservicefactory-java-LC6\" class=\"blob-code blob-code-inner js-file-line\">public final class WebserviceFactory{<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-webservicefactory-java-LC7\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-webservicefactory-java-LC8\" class=\"blob-code blob-code-inner js-file-line\">    static &lt;T&gt; T decorateWithRetryer(final T service, Retry retry) {<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-webservicefactory-java-LC9\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-webservicefactory-java-LC10\" class=\"blob-code blob-code-inner js-file-line\">        InvocationHandler invocationHandler = (proxy, method, args) -&gt; retry.executeCheckedSupplier(() -&gt; method.invoke(service, args));<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-webservicefactory-java-LC11\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-webservicefactory-java-LC12\" class=\"blob-code blob-code-inner js-file-line\">        return (T) Proxy.newProxyInstance(service.getClass().getClassLoader(),<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-webservicefactory-java-LC13\" class=\"blob-code blob-code-inner js-file-line\">                service.getClass().getInterfaces(), invocationHandler);<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-webservicefactory-java-LC14\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-webservicefactory-java-LC15\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-webservicefactory-java-LC16\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr class=\"line\">\n          <td id=\"file-webservicefactory-java-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-webservicefactory-java-LC17\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/mwiede\/9c4c9ba370f5481af4b09661039369e2\/raw\/db47624bcdb7e9355e3cd63293c0d6c0bb23a9c5\/WebserviceFactory.java\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/mwiede\/9c4c9ba370f5481af4b09661039369e2#file-webservicefactory-java\" class=\"Link--inTextBlock\">\n          WebserviceFactory.java\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n\n\n\n<p>This way I am able to decorate my whole service interface.<\/p>\n\n\n\n<p>One note I can make regarding the exception handling: In case of an exception, an InvocationTargetException is thrown. If you want to have the target one, you have to unwrap it. <\/p>\n\n\n\n<p>The source code of my example is available at <a href=\"https:\/\/github.com\/mwiede\/jaxws-resilience4j\">https:\/\/github.com\/mwiede\/jaxws-resilience4j<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a recent project, we still have to use SOAP webservices and I wanted to apply some resilience pattern such as retry to my project. Also a collegue just presented a java library called resilience4j so I wanted to use that one. Of course, there are a lot of other possibilites like using other libraries &hellip; <a href=\"https:\/\/www.matez.de\/index.php\/2019\/08\/02\/fault-tolerance-for-jax-ws-with-resilience4j\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;fault tolerance for jax-ws with resilience4j&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[34,39,2,40,41],"_links":{"self":[{"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/posts\/137"}],"collection":[{"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/comments?post=137"}],"version-history":[{"count":2,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}