Use Redis to implement MQ

        There are many common message centers, such as ActiveMQ, RabbitMQ, Kafka, etc. This blog mainly focuses on using Redis to implement MQ functions.

        The function implemented by MQ is simply to consume the produced messages. The specific consumption model is point-to-point consumption, publish/subscribe consumption. Redis also implements point-to-point, publish/subscribe this way, and the specific code is displayed.

        Point-to-point consumers:

public class RedisQueueConsumer {

    @Test
    public  void consumer() {

        Jedis jedis = new Jedis("xxx.xxx.xxx.xxx",6379);
        while (true)
        {
            //使用redis的rpop来获取对应的key的value
            String value = jedis.rpop("key");
            while(value != null)
            {
                System.out.println(value);
                break;
            }

        }

    }
}

        Point-to-point producer:

public class RedisQueueTest {

    @org.junit.Test
    public  void  producer()
    {
        Jedis jedis = new Jedis("xxx.xxx.xxx.xxx",6379);
        // 生产消息
        for (int i = 1;i <= 10;i++)
        {
            //主要使用redis中的lpush
            jedis.lpush("key", "这是第" + i + "个消息");
        }

        jedis.close();
    }

}

        Start the consumer first, and then the producer produces the message, the result graph:

        Publish\subscribe model:

        First write the monitoring code, inherit JedisPubSub, and then rewrite the onMessage method inside:


public class Subscriber  extends JedisPubSub {
    public Subscriber(){}
    @Override
    public void onMessage(String channel, String message) {
        System.out.println(message);
    }
    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        super.onSubscribe(channel, subscribedChannels);
    }
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        super.onUnsubscribe(channel, subscribedChannels);
    }
}

        consumer:

public class RedisTopicConsumer {
    @Test
    public  void consumer() {

        Subscriber subscriber = new Subscriber();

        Jedis jedis = new Jedis("xxx.xxx.xxx.xx",6379);
        while (true)
        {
            //监听消息
            jedis.subscribe(subscriber,"key");
        }
    }
}

        Producer:

public class RedisTopicTest {
    @org.junit.Test
    public  void  producer()
    {
        Jedis jedis = new Jedis("xxx.xxx.xxx.xxx",6379);
        // 生产消息
        for (int i = 1;i <= 10;i++)
        {
            //取消息
            jedis.publish("key", "这是第" + i + "个消息");
        }

        jedis.close();
    }
}

       Start the consumer, open the producer to produce the message, and the message is subscribed for consumption:

        The whole process, in fact, is to use the function of Redis, the production message uses lpush to join the queue, and the fetching message is the rpop out of the queue; the production message publish publishes the message to the specified channel, and subscribe to subscribe to the specific message.

 

Guess you like

Origin blog.csdn.net/qq_41061437/article/details/108517630