{"id":1461,"date":"2012-07-24T10:19:30","date_gmt":"2012-07-24T08:19:30","guid":{"rendered":"http:\/\/www.tutego.de\/blog\/javainsel\/?p=1461"},"modified":"2012-07-24T10:30:52","modified_gmt":"2012-07-24T08:30:52","slug":"designfrage-wie-nutzt-man-gwt-rpc-aufrufe-lokal","status":"publish","type":"post","link":"https:\/\/www.tutego.de\/blog\/javainsel\/2012\/07\/designfrage-wie-nutzt-man-gwt-rpc-aufrufe-lokal\/","title":{"rendered":"Designfrage: Wie nutzt man GWT-RPC-Aufrufe lokal etwa bei Offline-Anwendungen?"},"content":{"rendered":"<p>Standardm\u00e4\u00dfig sieht es ja im \u201cnormalen\u201d entfernen RPC-Aufruf so aus: Als erstes die Schnittstelle:<\/p>\n<p>public interface ContactRpcService extends RemoteService {<\/p>\n<p>&#160; Contact getContactById( long id );<\/p>\n<p>}<\/p>\n<p>Dann die zugeh\u00f6rige Async-Schnittstelle:<\/p>\n<p>public interface ContactRpcServiceAsync {<\/p>\n<p>&#160; void getContactById( long id, AsyncCallback&lt;Contact&gt; callback );<\/p>\n<p>}<\/p>\n<p>Der Client hat nun so was wie<\/p>\n<p>ContactRpcServiceAsync contactService = GWT.create( ContactRpcService.class );<\/p>\n<p>contactService.getContactById( contactId, new DefaultCallback&lt;Contact&gt;() { <\/p>\n<p>&#160; @Override protected void handleResponse( Contact response ) { <\/p>\n<p>&#160;&#160;&#160; \u2026.<\/p>\n<p>&#160; }<\/p>\n<p>} );<\/p>\n<p>DefaultCallback ist eine meine abstrakte Klasse, die AsyncCallback implementiert, aber das ist jetzt nicht so wichtig.<\/p>\n<p>Auch etwa anders mache ich noch, damit ich weniger schreiben muss; ich habe mir eine Klasse Rpc deklariert, mit Konstanten f\u00fcr alle GWT-creates():<\/p>\n<p>public class Rpc {<\/p>\n<p>&#160; private Rpc() {}<\/p>\n<p>&#160; public final static ContactRpcServiceAsync&#160; contactService = GWT.create( ContactRpcService.class );<\/p>\n<p>&#160; \/\/ \u2026 un ddie Anderen<\/p>\n<p>}<\/p>\n<p>Normalerweise sieht es also bei mir so aus:<\/p>\n<p>Rpc.contactService.getContactById( contactId, new DefaultCallback&lt;Contact&gt;() {<\/p>\n<p>&#160; @Override protected void handleResponse( Contact response ) {<\/p>\n<p>&#160;&#160;&#160; \u2026<\/p>\n<p>&#160; }<\/p>\n<p>} );<\/p>\n<p>Damit nun Rpc.contactService.getContactById() im Offline-Modus etwas anderes macht, kann man zum Beispiel folgendes tun: Rpc.contactService stammt nicht von GWT.create(), sondern ist ein Proxy. Falls nun der Remote-Fall gew\u00fcnscht ist, delegiert man an an die echte Rpc-Implementierung, andernfalls an eine lokale Variante.<\/p>\n<p>public class Rpc {<\/p>\n<p>&#160; private Rpc() {}<\/p>\n<p>&#160; public final static ContactRpcServiceAsync&#160; contactService = new ContactRpcServiceAsync() {<\/p>\n<p>&#160;&#160;&#160; private final ContactRpcServiceAsync delegate = GWT.create( ContactRpcService.class );<\/p>\n<p>&#160;&#160;&#160; private final ContactRpcServiceAsync local = new ContactRpcServiceAsync() {   <br \/>&#160;&#160;&#160;&#160;&#160; @Override public void getContactById( long id, AsyncCallback&lt;Contact&gt; callback ) {<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/ hier alles lokale machen, also etwas aus dem Cache holen. Wenn alles gut geht, und die Daten vorhanden sind, dann aufrufen   <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; callback.onSuccess( result );    <br \/>&#160;&#160;&#160;&#160;&#160; }<\/p>\n<p>&#160;&#160;&#160; };<\/p>\n<p>&#160;&#160;&#160; @Override public void getContactById( long id, AsyncCallback&lt;Contact&gt; callback ) {<\/p>\n<p>&#160;&#160;&#160;&#160;&#160; wenn der remote Fall<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; delegate.getContactById( id, callback );<\/p>\n<p>&#160;&#160;&#160;&#160;&#160; andernfalls<\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; local.getContactById( id, callback );<\/p>\n<p>&#160;&#160;&#160; }<\/p>\n<p>&#160; };<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Standardm\u00e4\u00dfig sieht es ja im \u201cnormalen\u201d entfernen RPC-Aufruf so aus: Als erstes die Schnittstelle: public interface ContactRpcService extends RemoteService { &#160; Contact getContactById( long id ); } Dann die zugeh\u00f6rige Async-Schnittstelle: public interface ContactRpcServiceAsync { &#160; void getContactById( long id, AsyncCallback&lt;Contact&gt; callback ); } Der Client hat nun so was wie ContactRpcServiceAsync contactService = GWT.create( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false,"_links_to":"","_links_to_target":""},"categories":[16],"tags":[],"class_list":["post-1461","post","type-post","status-publish","format-standard","hentry","category-gwt"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/comments?post=1461"}],"version-history":[{"count":4,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1461\/revisions"}],"predecessor-version":[{"id":1465,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1461\/revisions\/1465"}],"wp:attachment":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/media?parent=1461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/categories?post=1461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/tags?post=1461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}