redis简单队列java_使用Redis的简单消息队列
redis簡單隊(duì)列java
在本文中,我們將使用列表命令將Redis用作簡單的消息隊(duì)列。
假設(shè)我們有一個(gè)允許用戶上傳照片的應(yīng)用程序。 然后在應(yīng)用程序中,我們以不同大小顯示照片,例如Thumb,Medium和Large。
在第一個(gè)實(shí)現(xiàn)中,我們可以具有在同一請求中處理上傳的圖像的任務(wù)。 由于這是一項(xiàng)昂貴的任務(wù),因此會使我們的請求變慢。
一個(gè)可能的解決方案是使用消息隊(duì)列(MQ)使該處理異步進(jìn)行,有許多眾所周知的MQ,例如ActiveMQ,RabbitMQ,IBM MQ等。 在下面的示例中,我們將使用LIST結(jié)構(gòu)將Redis用作消息隊(duì)列。
想法是要有一個(gè)LIST,生產(chǎn)者將在其中放置要處理的消息,而某些消費(fèi)者將觀看LIST以處理所發(fā)送的消息。
基本上,生產(chǎn)者將使用“ RPUSH隊(duì)列消息 ”將消息添加到列表的末尾,而消費(fèi)者將使用“ LPOP隊(duì)列 ”將列表開頭的消息配置為FIFO處理。
客戶端將一直在尋找新消息,為此,我們將使用BLPOP命令,該命令是LPOP命令的阻止版本。 基本上,會有一個(gè)while循環(huán)調(diào)用BLPOP來處理新消息。
考慮到圖像上傳示例,假設(shè)我們有一個(gè)類ImageUploader負(fù)責(zé)將圖像上傳到服務(wù)器,它將在隊(duì)列中添加一條新消息,指示有要處理的圖像,消息可以是JSON字符串像這樣:
{“imagePath”:”/path/to/image”, “user”:”userid”}ImageUploder類可能是這樣的:
public class ImageUploader {public void uploadImage(HttpServletRequest request){String imagePath = saveImage(request);String jsonPayload = createJsonPayload(request, imagePath);jedis.rpush("queue", jsonPayload);//... keep with the processing}//.... other methods in the class }這只是生產(chǎn)者如何工作的一個(gè)例子。 在這種情況下,我們已經(jīng)將圖像處理與ImageUploader類分離了。 我們只是在隊(duì)列中創(chuàng)建一條新消息,以便使用者處理它們。
消息使用者可能是這樣的:
package br.com.xicojunior.redistest;import java.util.List;import redis.clients.jedis.Jedis;public class MessageConsumer {public static void main( String[] args ){Jedis jedis = new Jedis("localhost"); List<String> messages = null;while(true){System.out.println("Waiting for a message in the queue");messages = jedis.blpop(0,"queue");System.out.println("Got the message");System.out.println("KEY:" + messages.get(0) + " VALUE:" + messages.get(1));String payload = messages.get(1);//Do some processing with the payloadSystem.out.println("Message received:" + payload);}} }此使用者代碼可以在不同的進(jìn)程甚至不同的機(jī)器上運(yùn)行。 這個(gè)使用者代碼是可運(yùn)行的,我們可以編譯它并使用eclipse或java命令運(yùn)行它。
對于此代碼,我們使用jedis.blpop方法,它返回包含2個(gè)字符串的列表,(0)–鍵,(1)–返回的值。 該方法還接收一個(gè)整數(shù),它表示超時(shí)。 我們傳遞了0表示沒有超時(shí)。
當(dāng)我們運(yùn)行此代碼并且列表中沒有值時(shí),在控制臺中,我們將僅看到消息
"Waiting for a message in the queue".然后,如果客戶在“隊(duì)列”列表中添加元素,我們的消費(fèi)者類將獲得其價(jià)值。 我們可以使用redis-cli或另一個(gè)將在隊(duì)列中添加元素的類來模擬測試,如下所示:
package br.com.xicojunior.redistest;import redis.clients.jedis.Jedis;public class MessageProducer {public static void main(String[] args) {Jedis jedis = new Jedis("localhost");jedis.rpush("queue", "Value 1");jedis.rpush("queue", "Value 2");jedis.rpush("queue", "Value 3");}}如果我們在MessageConsumer類已經(jīng)運(yùn)行之后運(yùn)行MessageProducer類,我們將在控制臺中看到以下輸出:
Waiting for a message in the queue Got the message KEY:queue VALUE:Value 1 Message received:Value 1 Waiting for a message in the queue Got the message KEY:queue VALUE:Value 2 Message received:Value 2 Waiting for a message in the queue Got the message KEY:queue VALUE:Value 3 Message received:Value 3 Waiting for a message in the queue 因此,消息隊(duì)列將是Redis的另一個(gè)可能的用例。 在redis之上建立了一些隊(duì)列,如RestMQ , Resque – Job Queue等。
翻譯自: https://www.javacodegeeks.com/2014/01/simple-message-queue-using-redis.html
redis簡單隊(duì)列java
總結(jié)
以上是生活随笔為你收集整理的redis简单队列java_使用Redis的简单消息队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓xbmc可以播放光盘(安卓xbmc)
- 下一篇: jedis与redis_Redis与Je