Redis 队列 Java调用简单实现

编程教程 > Java (5655) 2024-11-26 14:39:04

简述

在本博客中,我们将会创建一个reids的消息队列,Redis可以被当成消息队列使用。消息会被存放在一个key-value集合中。
redis消息生产者使用RPUSH命令将消息添加到队列的尾部,而消息消费者可以使用BLPOP命令获取列表开头的消息,使用FIFO(先进先出)规则。

注意:本博客前置条件,熟悉redis并且知道如何启动redis服务器

Redis 队列实现需要的maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xqlee.redis</groupId>
    <artifactId>message-queue</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>REDDIS - ${project.artifactId}</name>
    <url>http://xqlee.com</url>

    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.2</version>
        </dependency>
    </dependencies>

</project>

Redis消息队列生产者

我们先使用rpush()方法将消息发布到mq-key队列中。这条消息将会添加到列表的最末端。
 
import redis.clients.jedis.Jedis;

public class MessageProducer {

    public static void main(String... args) {
        Jedis jedis = new Jedis("localhost");

        jedis.rpush("mq-key", "first message");
        jedis.rpush("mq-key", "second message");
        jedis.rpush("mq-key", "third message");
    }

}

Redis消息队列消费者

我们可以使用lpop()或者blpop()方法来消费消息。下面我们将会使用阻塞的lpop 方法,就如方法名称一样,使用该方法线程会进入阻塞状态直到下一个消息过来。我们可以设置一个等待消息的超时时间。下面设置的超时时间为0,表示永久等待没有超时时间。
import redis.clients.jedis.Jedis;
import java.util.List;

public class MessageConsumer {

    private static final int TIMEOUT = 0;

    public static void main(String... args ) {
        Jedis jedis = new Jedis("localhost");

        while(true){
            System.out.println("Waiting for a message in the queue");
            List<String> messages = jedis.blpop(TIMEOUT, "mq-key");
            System.out.println("received message with key:" + messages.get(0) + " with value:" + messages.get(1));
        }

    }
}

启动消息队列消费者

$title(console)
Waiting for a message in the queue

启动消息队列生产者

$title(console)
Waiting for a message in the queue
received message with key:mq-key with value:first message
Waiting for a message in the queue
received message with key:mq-key with value:second message
Waiting for a message in the queue
received message with key:mq-key with value:third message
Waiting for a message in the queue

参考文档:

评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
简述在本博客中,我们将会创建一个reids的消息队列,Redis可以被当成消息队列使用
Java连接redis启动报错Error redis clients jedis HostAndPort cant resolve localhost address
redis 命令查看使用情况redis info命令详解,redis查看内存使用情况。redis info命令的详细解释
Redis 删除/清除数据​​​​​​​1.访问redis根目录    cd  /usr/local/redis-2.8.192.登录redis:redis-cli -h 127.0.0.1 -...
Redis 禁用持久化配置
Spring Boot 2.0 Redis整合,通过spring boot 2.0整合Redis作为spring缓存框架的实现。
spring boot 1.5整合redis实现spring的缓存框架,spring boot,redis
今天遇到Redis “MISCONF Redis is configured to save RDB snapshots, but is currently not able to persis...
前置条件Docker 环境开始安装redis-stack打开docker官方hub的地址 redis/redis-stack Tags | Docker Hub
redis在window系统上的下载安装使用说明
简述本文主要通过一个简单的例子模拟实现秒杀情景,其中主要使用Redis事物进行实现spring boot为提供方便的环境
spring data redis设置缓存的过期时间,spring data redis更新缓存的过期时间
FLUSHALL FLUSHDB 命令会清空数据,而且从不失败,对于线上集群非常危险。KEYS * 命令,当数据规模较大时使用,会严重影响Redis性能,也非常危险。如果从根本上规避这些风险呢?...
Docker安装部署Redisdocker 安装部署Redis环境Linux系统dockerdocker-compose 相关文章:Ubuntu 在线安装 Docker-xqlee (blog....
目标使用Redis Stack中间件作为向量数据库(Vector Database)实现文档数据的存储和查询功能。先决条件已安装好的 redis stack ,