---
title: 'Designfrage: Wie nutzt man GWT-RPC-Aufrufe lokal etwa bei Offline-Anwendungen?'
url: 'https://tutego.de/blog/javainsel/2012/07/designfrage-wie-nutzt-man-gwt-rpc-aufrufe-lokal'
markdown: 'https://tutego.de/blog/javainsel/2012/07/designfrage-wie-nutzt-man-gwt-rpc-aufrufe-lokal.md'
date: '2012-07-24'
description: 'Standardmäßig sieht es ja im “normalen” entfernen RPC-Aufruf so aus: Als erstes die Schnittstelle: public interface ContactRpcService extends RemoteService { Contact getContactById( long id ); } Dann die zugehörige Async-Schnittstelle: public interface ContactRpcServiceAsync { void getContactB…'
taxonomy:
  category:
    - GWT
---

# Designfrage: Wie nutzt man GWT-RPC-Aufrufe lokal etwa bei Offline-Anwendungen?

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## Designfrage: Wie nutzt man GWT-RPC-Aufrufe lokal etwa bei Offline-Anwendungen?

24.07.2012 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

Standardmäßig sieht es ja im “normalen” entfernen RPC-Aufruf so aus: Als erstes die Schnittstelle:

```java
public interface ContactRpcService extends RemoteService {

 Contact getContactById( long id );

}
```

Dann die zugehörige Async-Schnittstelle:

```java
public interface ContactRpcServiceAsync {

 void getContactById( long id, AsyncCallback<Contact> callback );

}
```

Der Client hat nun so was wie

```java
ContactRpcServiceAsync contactService = GWT.create( ContactRpcService.class );

contactService.getContactById( contactId, new DefaultCallback<Contact>() {

 @Override protected void handleResponse( Contact response ) {
```

….

```java
 }

} );
```

DefaultCallback ist eine meine abstrakte Klasse, die AsyncCallback implementiert, aber das ist jetzt nicht so wichtig.

Auch etwa anders mache ich noch, damit ich weniger schreiben muss; ich habe mir eine Klasse `Rpc` deklariert, mit Konstanten für alle GWT-`creates()`:

```java
public class Rpc {

 private Rpc() {}

 public final static ContactRpcServiceAsync contactService = GWT.create( ContactRpcService.class );

 // ... un ddie Anderen

}
```

Normalerweise sieht es also bei mir so aus:

```java
Rpc.contactService.getContactById( contactId, new DefaultCallback<Contact>() {

 @Override protected void handleResponse( Contact response ) {
```

…

```java
 }

} );
```

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ünscht ist, delegiert man an an die echte `Rpc`-Implementierung, andernfalls an eine lokale Variante.

```java
public class Rpc {

 private Rpc() {}

 public final static ContactRpcServiceAsync contactService = new ContactRpcServiceAsync() {

 private final ContactRpcServiceAsync delegate = GWT.create( ContactRpcService.class );

 private final ContactRpcServiceAsync local = new ContactRpcServiceAsync() {
 @Override public void getContactById( long id, AsyncCallback<Contact> callback ) {
```

// hier alles lokale machen, also etwas aus dem Cache holen. Wenn alles gut geht, und die Daten vorhanden sind, dann aufrufen

```java
 callback.onSuccess( result );
 }

 };

 @Override public void getContactById( long id, AsyncCallback<Contact> callback ) {
```

wenn der remote Fall

```java
 delegate.getContactById( id, callback );
```

andernfalls

```java
 local.getContactById( id, callback );

 }

 };

}
```

Kategorien: [GWT](https://tutego.de/blog/javainsel/category/gwt/)

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [JPA-Beispiel in wenigen Minuten mit OpenJPA](https://tutego.de/blog/javainsel/2012/07/jpa-beispiel-in-wenigen-minuten-mit-openjpa.md)
- Next: [How to put files in a ZIP file with NIO.2](https://tutego.de/blog/javainsel/2012/07/how-to-put-files-in-a-zip-file-with-nio-2.md)
