using lettuce redis on java

2020-08-12

A general set/get command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mycompany.app;

import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
RedisClient redisClient = RedisClient.create("redis://localhost:6379/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "Hello, Redis!");
connection.close();
redisClient.shutdown();
}
}

##publish/subscribe command

setup the redis connection for pubsub

1
2
3
4
5
6
7
8
9
10

RedisClient redisClient = RedisClient.create("redis://localhost:6379/0");
StatefulRedisPubSubConnection<String, String> pubsubConn = redisClient.connectPubSub();


//add the listener to consume the message
pubsubConn.addListener(new AppRedisPubSubListener());

RedisPubSubAsyncCommands<String, String> async = pubsubConn.async();
async.subscribe("CM");

###add the listener

RedisPubSubListener is the interface, we need to implement a listener class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54


import io.lettuce.core.pubsub.RedisPubSubListener;

public class AppRedisPubSubListener implements RedisPubSubListener<String, String> {

public void message(String channel, String message) {
System.out.println("Got {} on channel {}" + message + ":" + channel);
}

public void message(String pattern, String channel, String message){

}

/**
* Subscribed to a channel.
*
* @param channel Channel.
* @param count Subscription count.
*/
public void subscribed(String channel, long count){

}

/**
* Subscribed to a pattern.
*
* @param pattern Pattern.
* @param count Subscription count.
*/
public void psubscribed(String pattern, long count){

}

/**
* Unsubscribed from a channel.
*
* @param channel Channel.
* @param count Subscription count.
*/
public void unsubscribed(String channel, long count){

}

/**
* Unsubscribed from a pattern.
*
* @param pattern Channel.
* @param count Subscription count.
*/
public void punsubscribed(String pattern, long count){

}
}

To test the publish/subscribe command , the main thread shouldn’t exit quickly,
int the main() we can add something code like this, to make applicaiton always in running state.

1
2
3
4
5
6
7
8
9
10
while (true) {
try {
// thread to sleep for 1000 milliseconds
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
pubsubConn.close();
redisClient.shutdown();
}
}