Listening to a queue in redis with spring-boot application and lettuce library

739 views Asked by At

I have developed different services based on Spring-boot, Spring-cloud, and microservices architecture, also I am using the following dependency as Redis-client:

implementation 'io.lettuce:lettuce-core:6.1.5.RELEASE'

I have defined my RedisBean in Spring as below:

@Configuration
public class RedisBean {
    @Bean
    public RedisClient redisClientFactory(AppConfig config) {
        var client =  RedisClient.create(config.getRedis());
        client.setDefaultTimeout(Duration.ZERO);
        return client;
    }

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public StatefulRedisConnection<byte[], byte[]> redisConnectionFactory(RedisClient client) {
        return client.connect(ByteArrayCodec.INSTANCE);
    }

    @Bean
    public RedisCommands<byte[], byte[]> redisCommands(StatefulRedisConnection<byte[], byte[]> connection) {
        return connection.sync();
    }
}

I have SSO and ACCOUNTING services.

In my SSO service when a user register, I will push the username in Queue like this:

@Service
public class AccountingServiceImpl implements AccountingService {

    private final RedisCommands<byte[], byte[]> redis;
    private final AppConfig appConfig;

    public AccountingServiceImpl(AppConfig appConfig, RedisCommands<byte[], byte[]> redis) {
        this.redis = redis;
        this.appConfig = appConfig;
    }

    @Override
    public void requestCreateDefaultAccount(String username) {
        redis.rpush(
                appConfig.getCreateAccountQueueName().getBytes(StandardCharsets.UTF_8)
                , username.getBytes(StandardCharsets.UTF_8)
        );
    }
}

Now in the ACCOUNTING service, I want to regularly listen to the registered Queue and whenever a username was pushed, I take it and create a default account for the user.

I have searched a lot but couldn't find a suitable solution for my requirements using io.lettuce:lettuce-core library.

Any help would be appreciated!!

0

There are 0 answers