Skip to content

collinistevens/Swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Swift

Swift provides a simple way to broadcast typed payloads across a Redis Pub/Sub network and dispatch them to annotated listeners with priority ordering. Swift interacts with Redis using the Lettuce library.

Core Concepts

Payload

A payload is any Java object you want to send across the network.

public class MessagePayload {
    private final String message;
}

Payload Listener

A listener is a class that implements PayloadListener and contains methods annotated with @PayloadHandler

public class ChatListener implements PayloadListener {

    @PayloadHandler(priority = PayloadPriority.NORMAL)
    public void onChat(ChatMessagePayload payload) {
        System.out.println(payload.getMessage());
    }
}

Each handler method:

  • Must be annotated with @PayloadHandler
  • Must take exactly one parameter
  • That parameter defines the payload type it handles

Payload Priority

Handlers are executed from HIGHEST to LOWEST.

public enum PayloadPriority {
    HIGHEST,
    HIGH,
    NORMAL,
    LOW,
    LOWEST
}

Usage

Creating a Swift instance

RedisClient redisClient = RedisClient.create("redis://localhost:6379");
JsonProvider jsonProvider = new GsonJsonProvider();

Swift swift = new Swift(
    "my-network",   // Redis channel (network)
    "unit-1",       // Sender identifier (unit)
    redisClient,
    jsonProvider
);

network - Redis Pub/Sub channel that payloads will be broadcast on.

unit - Identifier of the payload sender.

Registering listeners

swift.registerListener(new ChatMessageListener());

Or multiple at once:

swift.registerListeners(
    new ChatMessageListener(),
    new UserConnectionListener()
);

Listeners can be registered at runtime and are thread-safe.

Broadcasting a payload

swift.broadcastPayload(new ChatMessagePayload("Hello world"));

This:

  1. Serializes the payload using the provided JsonProvider.
  2. Wraps it in a Message object.
  3. Publishes it to Redis.

About

Redis pub/sub payload handler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages