1. 程式人生 > >使用spring的RedisTemplate操作redis佇列

使用spring的RedisTemplate操作redis佇列

場景:
    1.在基於spring的web專案中使用redis,一般會在配置檔案中配置Jedis連線池,連線工廠,RedisTemplate模板等,
     把這些類交給spring容器去建立物件,使用時只需注入即可.
    2.本例是將spring中用配置檔案配置使用redis方式,改成用程式碼實現配置.可在main函式中直接使用連線池技術操作redis等

細節:
本例共計一個類包含以下部分:
    1.配置連線池引數 

/**1.配置連線池引數*/
	public static JedisPoolConfig getPoolConfig(){
		
		JedisPoolConfig jedisPoolConfig = new redis.clients.jedis.JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(1024);
		jedisPoolConfig.setMaxIdle(100);
		jedisPoolConfig.setMinEvictableIdleTimeMillis(50000);
		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(20000);
		jedisPoolConfig.setNumTestsPerEvictionRun(-1);
		jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000);
		jedisPoolConfig.setMaxWaitMillis(1000);
		jedisPoolConfig.setTestOnBorrow(true);
		jedisPoolConfig.setTestWhileIdle(true);
		jedisPoolConfig.setTestOnReturn(false);
		jedisPoolConfig.setJmxEnabled(true);
		jedisPoolConfig.setJmxNamePrefix("pool");
		jedisPoolConfig.setBlockWhenExhausted(false);
		return jedisPoolConfig;
	}


    2.獲取連線工廠
      使用時注意一定要加jedisConnetFactory.afterPropertiesSet();否則會無法初始化例項

/**2.獲取連線工廠*/
	public static JedisConnectionFactory getConnectionFactory(JedisPoolConfig poolConfig){
		
		JedisConnectionFactory jedisConnetFactory = new JedisConnectionFactory();
		jedisConnetFactory.setPoolConfig(poolConfig);
		jedisConnetFactory.setHostName(hostName);
		jedisConnetFactory.setPort(port);
		
		/**必須執行這個函式,初始化JedisConnectionFactory*/
		jedisConnetFactory.afterPropertiesSet();
		 
		return jedisConnetFactory;
	}


    3.獲取RedisTemplate例項
      使用時注意一定要加redisTemplate.afterPropertiesSet();否則會無法初始化例項.

/**3.獲取RedisTemplate例項*/
	public static RedisTemplate getRedisTemplate(JedisConnectionFactory connectionFactory){
		
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(connectionFactory);
		StringRedisSerializer serializer = new StringRedisSerializer();
		redisTemplate.setDefaultSerializer(serializer);
		redisTemplate.setKeySerializer(serializer);
		redisTemplate.setValueSerializer(serializer);
		
		/**必須執行這個函式,初始化RedisTemplate*/
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}


    4.main函式,屬性變數,獲取JSON資料字串函式

package com.zbz.redis.client;

import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 *  
 * @ClassName: RedisTemplateClient
 * @date: 2018-12-26 下午6:11:33
 */
public class RedisTemplateClient {
	
	/**redis服務ip*/
	public static final String hostName = "127.0.0.1";
	/**redis服務埠*/
	public static final int port = 6379;
	/**佇列名稱*/
	public static final String template_Queue = "TEMPLATE_QUEUE";
	/**測試資料*/
	public static final String data = getData();
	
	public static void main(String [] args) throws InterruptedException{
		
		/**1.配置連線池引數*/
		JedisPoolConfig  poolConfig =getPoolConfig();
		/**2.獲取連線工廠*/
		JedisConnectionFactory connectionFactory = getConnectionFactory(poolConfig);
		/**3.獲取RedisTemplate例項*/
		RedisTemplate redisTemplate = getRedisTemplate(connectionFactory);
		
		System.out.println("測試開始......");
		System.out.println("佇列寫入資料:");
		System.out.println(data);
		/**獲取RedisTemplate操作方式*/
		ListOperations operation = redisTemplate.opsForList();
		 
		operation.leftPush(template_Queue, data);
		System.out.println("主執行緒休眠10秒......");
		Thread.sleep(10000);
		Object strJson = operation.rightPop(template_Queue,10, TimeUnit.SECONDS);
		System.out.println("佇列讀出資料:");
		System.out.println(strJson);
		System.out.println("測試結束......");
		
	}

	/**獲取JSON資料字串*/
	public static String getData(){
		
		 String data=  "{\n" +
	            "    \"AREA_CODE\": \"0592\",\n" +
	            "    \"AREA_NUMBER\": \"350200\",\n" +
	            "    \"CITY_NAME\": \"廈門\",\n" +
	            "    \"DESCRIBE\": \"適合居住\",\n" +
	            "    \"POSTAL_CODE\": \"361000\"\n" +
	            "}";
		 return data;
	}


    
測試:
    使用命令 Llen TEMPLATE_QUEUE,在redis-cli.exe客戶端觀察佇列變化情況.
 
     
以上,TKS.