Redis 队列 Java调用简单实现

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(2972)   2023-03-28 11:29:14

简述

在本博客中,我们将会创建一个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.leftso.redis</groupId>
    <artifactId>message-queue</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>REDDIS - ${project.artifactId}</name>
    <url>http://leftso.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

参考文档:
标签: Redis队列 Redis Java
地址:https://www.leftso.com/article/614.html

相关阅读

简述在本博客中,我们将会创建一个reids的消息队列,Redis可以被当成消息队列使用
redis 命令查看使用情况redis info命令详解,redis查看内存使用情况。redis info命令的详细解释
Java连接redis启动报错Error redis clients jedis HostAndPort cant resolve localhost address
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缓存框架的实现。
简述本文主要通过一个简单的例子模拟实现秒杀情景,其中主要使用Redis事物进行实现spring boot为提供方便的环境
redis在window系统上的下载安装使用说明
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...