Skip to content
JoanZapata edited this page Dec 19, 2014 · 11 revisions

Send events

In the previous example, you've seen how to create and use a AsyncService service, with a basic method String getUser(String username). Only be able to send one single type of result is very limiting. What if you can't find the user? Returning null will never reach the activity back, as you learned with rule #2.

@AsyncService
public abstract class DemoService implements EnhancedService {

   User getUser(String username){
      User user = // ...
      if (user == null) send(new UserNotFound(username));
      return user;
   }

}

So here it is. First you need to make your service abstract and implement EnhancedService. This will add a few methods to your service. send(Object message) is one of them. It sends an event exactly as if it was the return of the method, and it's sent back to the caller like any other result.

There is actually an annotation to shorten this code, called @Null :

@Null(UserNotFound.class)
User getUser(String username){
   return // ... 
}

Caching

Here's what EnhancedService adds to your service:

// Send a message to the current caller
void send(Object message);

// Cache an object at the given key (override previous value if any)
void cache(String key, Serializable object);

// Retrieve the cache value at the given key
<T extends Serializable> T getCached(String key, Class<T> returnType);

The last two methods access a persistent key-value storage, currently backed with SnappyDB. SnappyDB is included in the lib, you don't need an extra dependency.

Note: if you store an object A in the cache, and then update this object in further release (change a field's name, add a field, etc...), the cache is invalidated, so you don't need to care about migration.

In the next part, you'll see typical uses of the cache and how to shorten some common patterns using Kiss features.

Next step: [caching patterns](Caching Patterns)

Learn

  1. Learn the basics.
  2. How to [enhance your service](Enhance Service).
  3. Take advantage of [caching](Caching Patterns).
  4. How to [handle exceptions](Handle Exceptions).
  5. Want more? See what's [behind the scenes](Behind The Scenes).
Clone this wiki locally