无法在Redis中保存java对象

时间:2016-06-30 11:10:05

标签: java redis persistence

我试图在我的机器中运行以下示例:

  

https://examples.javacodegeeks.com/enterprise-java/spring/spring-data-redis-example/

public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("/spring/spring-config.xml").getPath());
RedisRepo redisRepo = (RedisRepo)context.getBean(“redisRepo");

        try{

            JedisPool pool = new JedisPool(new JedisPoolConfig(), "x.x.x.x”);
            Jedis jedis = pool.getResource();

            System.out.println("Connected to Redis”);//connected to Redis
            System.out.println("server is running: "+jedis.ping());//PONG
            System.out.println("current keys are :"+jedis.keys(“*”));//[ ]

            Employee s = new Employee();
            s.setId(1);
            s.setName(“abc”);
            redisRepo.saveState(s);
            System.out.println("server is running: "+jedis.ping());//PONG
            System.out.println("Finding the One : "+redisRepo.getState(1);//Finding the one :null

            }
        catch(Exception e){
            logger.error(e.getMessage(), e);
        }
}

但在尝试保存模型对象时遇到以下错误:

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

我想补充一点,我可以通过ping来测试与redis服务器的连接。如何解决此问题以在Redis中保留数据?

1 个答案:

答案 0 :(得分:0)

我可以通过在本地运行redis来解决在本地实例上保存数据的问题。为了在远程服务器上保存数据,我在主类中有导入资源。喜欢:

  

@ImportResource({类路径:路径到youXML})