Skip to content

Martini Invoking Services via a Redis Pub/Sub Trigger

The Redis Pub/Sub listener trigger enables developers to create applications that respond to messages broadcasted through Redis channels. This functionality facilitates real-time messaging patterns, allowing publishers to send messages to an unspecified number of subscribers via a channel.

Properties

General Configuration

The Redis Pub/Sub listener trigger inherits general configuration properties applicable to all trigger types in Martini. These settings include options for logging, transaction management, and security, among others.

Redis Pub/Sub Listener-Specific Configuration

The following properties are specific to configuring the Redis Pub/Sub listener trigger:

Properties Default Description
Connection Name (required) Name of the Redis database connection.
Channel (optional) Name of the channel this trigger should listen to. If this is specified, the patterns list must be empty.
Patterns (optional) A list of Redis patterns that this trigger should listen to. If a list of patterns is specified, the channel name must be empty.

Ad hoc Message Publishing

Redis Pub/Sub messages can be published ad hoc by: 1. Clicking on the envelope icon next to the Channel text field in the Redis trigger editor. 2. Right-clicking on a Redis listener trigger and selecting "Send Redis Channel Message". This action opens a dialog where you can specify the channel name and message content to send.

Service Parameters

When a message is received through the configured Redis Pub/Sub channel or pattern, the following parameters are passed to the service triggered by this event:

Name Type Description
databaseName java.lang.String The name of the Redis database connection from which the message was received.
channel java.lang.String The name of the channel through which the message was sent.
pattern java.lang.String The matching pattern that triggered this listener to receive the message.
message java.lang.String The content of the message.
content java.lang.String Alias for message.

Best Practices and Considerations

  • Ensure the Redis connection is securely configured, especially when dealing with sensitive information.
  • Monitor the performance impact of using Pub/Sub in high-volume environments to avoid affecting both the Martini application and the Redis server.
  • Utilize patterns judiciously to prevent unnecessary message processing and to ensure efficient use of resources.

For more information on configuring and using Redis within Martini, refer to the Redis database connection documentation and the Redis Pub/Sub patterns guide.

Example

Groovy Script as Service

Consider a Groovy script designed to print the available variables in the context. This script is invoked when the Redis channel configured for the trigger receives a message:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[
    'parameters',
    'martiniPackage',
    'properties',
    'channel',
    'pattern',
    'message',
    '$trackerId',
    'content'
].each {
    println "$it\t : " + this[it]
}

When the trigger is activated—by a Redis message sent to the configured channel—the console will display logs similar to the following:

1
2
3
4
5
6
7
8
parameters     : [ <omitted> ]
martiniPackage : martiniPackage [name=examples]
properties     : [ <omitted> ]
channel        : Channel1
pattern        : null
message        : Hello Martini!
$trackerId     : null
content        : Hello Martini!

Groovy Method as Service

The trigger can also be configured to use Groovy methods as services. Consider the following example where a class method is designed to log a message received from Redis:

1
2
3
4
5
class TestRedis {
    static void helloRedis(def message) {
        "Message received from Redis: ${message}".warn()
    }
}

Assuming the service TestRedis#helloRedis is configured in the trigger, it can be invoked by sending a message to a matching channel. This approach offers a flexible and powerful mechanism for responding to real-time data flows within your application, leveraging the Martini platform's integration capabilities.