strust+spring+redis+sqlite配置

一、安装


windows下的安装参见:http://os.51cto.com/art/201403/431103.htm


二、需要的jar包


strust+spring+redis+sqlite配置_第1张图片


三、配置文件


1、redis.property

#\u4E3B\u673A
redis.host=127.0.0.1
#\u7AEF\u53E3\u53F7
redis.port=6379
#client\u7A7A\u95F2\u591A\u4E45\u65AD\u5F00
redis.timeout=1000
#\u6700\u5927\u80FD\u591F\u4FDD\u6301idel\u72B6\u6001\u7684\u5BF9\u8C61\u6570 
redis.maxIdle=500
redis.minIdle=50
redis.maxWait=-1
#\u6700\u5927\u5206\u914D\u7684\u5BF9\u8C61\u6570    
redis.maxTotal=10000
#\u591A\u957F\u65F6\u95F4\u68C0\u67E5\u4E00\u6B21\u8FDE\u63A5\u6C60\u4E2D\u7A7A\u95F2\u7684\u8FDE\u63A5 
redis.timeBetweenEvictionRunsMillis=30000  
#\u7A7A\u95F2\u8FDE\u63A5\u591A\u957F\u65F6\u95F4\u540E\u4F1A\u88AB\u6536\u56DE 
redis.minEvictableIdleTimeMillis=100 
#\u5F53\u8C03\u7528borrow Object\u65B9\u6CD5\u65F6\uFF0C\u662F\u5426\u8FDB\u884C\u6709\u6548\u6027\u68C0\u67E5   
redis.testOnBorrow=true
redis.testWhileIdle=true


redis的各项配置要根据实际需求及设备配置,否则可能影响性能;


2、spring-redis.xml



	
	

	
	
		
		
		
		
		
		
		
	
	
	
		
		
		
		
		
	

	
		
		
			
		
		
			
		
	

3、applicationContext.xml





	


	 
	 
		
		
	



 
  

4、Struts.xml


  
  
     
     
	   
     
     
    
     
         
     	  
     	  
     	    
     	        returnMsg 
     	    
     	    
     	        returnMsg 
     	    
     	  
     
     
     
     	
     
  


5、web.xml



  LogDemo
  
  	
 	 
     	 contextConfigLocation
     	 
     	 
     	 /WEB-INF/applicationContext.xml
  	 
  	 
  	
	  
	  
	    org.springframework.web.context.ContextLoaderListener  
	  
	
	 
	
    struts2
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  
  
    struts2
    /*
  

  
  
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
   

三、自定义RedisTemplate


可以使用RedisTemplate.executePipelined()实现批量操作,但很多时候需要自定义返回值,所以需要自定义RedisTemplate。


package com.pccw.replacega.util;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationUtils;

import com.pccw.replacega.entity.LogDetail;
 
public class LogRedisTemplate extends RedisTemplate {
    @Override
    public List executePipelined(final RedisCallback action,
        final   RedisSerializer resultSerializer) {
        return execute(new RedisCallback>() {
            public List doInRedis(RedisConnection connection) throws DataAccessException {
                connection.openPipeline();
                boolean pipelinedClosed = false;
                try {
                    Object result = action.doInRedis(connection);
                    if (result != null) {
                        throw new InvalidDataAccessApiUsageException(
                                "Callback cannot return a non-null value as it gets overwritten by the pipeline");
                    }
                    List closePipeline = connection.closePipeline();
                    pipelinedClosed = true;
                    return deserializeMixedResults(closePipeline, getValueSerializer(),getHashKeySerializer(),resultSerializer);
                } finally {
                    if (!pipelinedClosed) {
                        connection.closePipeline();                     
                    }
                }
            }
        });
    }
     
    /**
     * 
    * @Title: executeConn
    * @Description: 序列化POJO
    * @return List
    * @throws
     */
    public List executeConn(final RedisCallback action,final HashMapper jhm) {
            return execute(new RedisCallback>() {
                public List doInRedis(RedisConnection connection) throws DataAccessException {
                    connection.openPipeline();
                    boolean pipelinedClosed = false;
                    try {
                        Object result = action.doInRedis(connection);
                        if (result != null) {
                            throw new InvalidDataAccessApiUsageException(
                                    "Callback cannot return a non-null value as it gets overwritten by the pipeline");
                        }
                        List closePipeline = connection.closePipeline();
                        pipelinedClosed = true;
                        return deserializeMixedResults(closePipeline, getHashKeySerializer(),getHashValueSerializer(),jhm);
                    } finally {
                        if (!pipelinedClosed) {
                            connection.closePipeline();
                        }
                    }
                }
            });
        }
     
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private List deserializeMixedResults(List rawValues, RedisSerializer valueSerializer,
            RedisSerializer hashKeySerializer, RedisSerializer hashValueSerializer) {
        if (rawValues == null) {
            return null;
        }
        List values = new ArrayList();
        for (Object rawValue : rawValues) {
            if (rawValue instanceof byte[] && valueSerializer != null) {
                values.add(valueSerializer.deserialize((byte[]) rawValue));
            } else if (rawValue instanceof List) {
                // Lists are the only potential Collections of mixed values....
                values.add(deserializeMixedResults((List) rawValue, valueSerializer, hashKeySerializer, hashValueSerializer));
            } else if (rawValue instanceof Set && !(((Set) rawValue).isEmpty())) {
                values.add(deserializeSet((Set) rawValue, valueSerializer));
            } else if (rawValue instanceof Map && !(((Map) rawValue).isEmpty())
                    && ((Map) rawValue).values().iterator().next() instanceof byte[]) {
                values.add(SerializationUtils.deserialize((Map) rawValue, hashKeySerializer, hashValueSerializer));
            } else {
                values.add(rawValue);
            }
        }
        return values;
    }
     
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private List deserializeMixedResults(List rawValues,RedisSerializer hashKeySerializer, RedisSerializer hashValueSerializer,HashMapper jhm) {
        if (rawValues == null) {
            return null;
        }
        List values = new ArrayList();
        for (Object rawValue : rawValues) {
            Map  obj = (Map) rawValue;
            Map map = new LinkedHashMap(obj.size());
            for (Map.Entry entry : obj.entrySet()) {
                map.put((K) hashKeySerializer.deserialize((entry.getKey())), (V) hashValueSerializer.deserialize((entry.getValue())));
            }
            values.add(jhm.fromHash(map));
        }
        return values;
    }
     
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private Set deserializeSet(Set rawSet, RedisSerializer valueSerializer) {
        if (rawSet.isEmpty()) {
            return rawSet;
        }
        Object setValue = rawSet.iterator().next();
        if (setValue instanceof byte[] && valueSerializer != null) {
            return (SerializationUtils.deserialize((Set) rawSet, valueSerializer));
        } else if (setValue instanceof Tuple) {
            return convertTupleValues(rawSet, valueSerializer);
        } else {
            return rawSet;
        }
    }
     
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private Set> convertTupleValues(Set rawValues, RedisSerializer valueSerializer) {
        Set> set = new LinkedHashSet>(rawValues.size());
        for (Tuple rawValue : rawValues) {
            Object value = rawValue.getValue();
            if (valueSerializer != null) {
                value = valueSerializer.deserialize(rawValue.getValue());
            }
            set.add(new DefaultTypedTuple(value, rawValue.getScore()));
        }
        return set;
    }
    
    
    public List executeConn(final RedisCallback action) {
        return execute(new RedisCallback>() {
            public List doInRedis(RedisConnection connection) throws DataAccessException {
                connection.openPipeline();
                boolean pipelinedClosed = false;
                try {
                    Object result = action.doInRedis(connection);
                    if (result != null) {
                        throw new InvalidDataAccessApiUsageException(
                                "Callback cannot return a non-null value as it gets overwritten by the pipeline");
                    }
                    List closePipeline = connection.closePipeline();
                    pipelinedClosed = true;
                    return deserializeMixedResults(closePipeline);
                } finally {
                    if (!pipelinedClosed) {
                        connection.closePipeline();
                    }
                }
            }
        });
    }
    
    private List deserializeMixedResults(List rawValues) {
        if (rawValues == null) {
            return null;
        }
        List values = new ArrayList();
        for (Object rawValue : rawValues) {
        	if(rawValue instanceof byte[]){
        		byte[] bs=(byte[])rawValue;
        		LogDetail logDetail=(LogDetail)RedisHelper.getObjectFromBytes(bs);
        		values.add(logDetail);
        	}
        }
        return values;
    }
    
    
} 
  


四、参考资料


http://blog.csdn.net/tiantiandjava/article/details/42913691

http://blog.csdn.net/liuzhigang1237/article/details/8283797







你可能感兴趣的:(java)