title: Refreshing RedisTemplate
body: <p>I am using spring quartz job to connect to Redis to perform a operation. I have configured the RedisTemplate to connect to only the master node.</p>
  
  <p>When failover happens in Redis and new master is elected I am notified via <code>client-reconfig-script</code> in sentinel.conf. After this I am trying to rewire my RedisTemplate to talk to the new master. This rewire part is not working.</p>
  
  <p>Spring boot config for quartz job and RedisTemplate:</p>
  
  <pre><code>package com.XXX.XXX;
  
  import org.quartz.JobDetail;
  import org.quartz.SimpleTrigger;
  import org.quartz.Trigger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Primary;
  import org.springframework.core.io.ClassPathResource;
  import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  import org.springframework.data.redis.core.RedisTemplate;
  import org.springframework.scheduling.quartz.JobDetailFactoryBean;
  import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
  import org.springframework.scheduling.quartz.SpringBeanJobFactory;
  
  import de.chandre.quartz.spring.AutowiringSpringBeanJobFactory;
  import de.chandre.quartz.spring.QuartzSchedulerAutoConfiguration;
  
  @SpringBootApplication(exclude = { QuartzSchedulerAutoConfiguration.class })
  public class OrderProcessorApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(OrderProcessorApplication.class, args);
      }
  
      @Bean
      public JobDetailFactoryBean jobDetail() {
          JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
          jobDetailFactory.setJobClass(OrderProcessorJob.class);
          jobDetailFactory.setDurability(true);
          return jobDetailFactory;
      }
  
      @Value("${job.interval}")
      private int interval;
  
      @Bean
      public SimpleTriggerFactoryBean trigger(JobDetail job) {
          SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
          trigger.setJobDetail(job);
          trigger.setRepeatInterval(interval);
          trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
          return trigger;
      }
  
      @Bean
      public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job) {
          SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
          schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));
  
          schedulerFactory.setJobFactory(springBeanJobFactory());
          schedulerFactory.setJobDetails(job);
          schedulerFactory.setTriggers(trigger);
          return schedulerFactory;
      }
  
      @Autowired
      private ApplicationContext ap;
  
      @Bean
      @Primary
      public SpringBeanJobFactory springBeanJobFactory() {
          AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
          jobFactory.setApplicationContext(ap);
          return jobFactory;
      }
  
      // @Bean
      // @Qualifier("sentinel")
      // public RedisTemplate&lt;String, Object&gt; createRedisTemplate() {
      // RedisSentinelConfiguration sc = new RedisSentinelConfiguration()
      // .master("redis-cluster")
      // .sentinel("127.0.0.1", 26179)
      // .sentinel("127.0.0.1", 26381);
      //
      // JedisConnectionFactory jcf = new JedisConnectionFactory(sc);
      // jcf.setUsePool(true);
      // jcf.afterPropertiesSet();
      //
      // RedisTemplate rt = new RedisTemplate();
      // rt.setConnectionFactory(jcf);
      // rt.afterPropertiesSet();
      // return rt;
      // }
  
      @Bean
      @Primary
      public RedisTemplate&lt;String, Object&gt; createMasterOnlyRedisTemplate() {
          JedisConnectionFactory jcf = new JedisConnectionFactory();
          jcf.setHostName("127.0.0.1");
          jcf.setPort(6379);
          jcf.setUsePool(true);
          jcf.afterPropertiesSet();
  
          RedisTemplate rt = new RedisTemplate();
          rt.setConnectionFactory(jcf);
          rt.afterPropertiesSet();
          return rt;
      }
  }
  </code></pre>
  
  <p>Refresh REST service invoked by sentinel:</p>
  
  <pre><code>package com.store.platform;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  import org.springframework.data.redis.core.RedisTemplate;
  import org.springframework.http.MediaType;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.kohls.store.platform.common.model.ui.UpdateRedisMaster;
  
  @RestController
  @RequestMapping(path = "/refresh", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  public class Test {
  
      @Autowired
      private RedisTemplate&lt;String, Object&gt; rt;
  
      @RequestMapping(method = RequestMethod.POST)
      public void refresh(@RequestBody UpdateRedisMaster um) {
          JedisConnectionFactory jcf = (JedisConnectionFactory) rt.getConnectionFactory();
          jcf.setHostName(um.getIp());
          jcf.setPort(um.getPort());
          jcf.setShardInfo(null);
          jcf.afterPropertiesSet();
      }
  }
  </code></pre>
  
  <p>After successfully invoking refresh, next the quartz job tries to connect to Redis I get following exception:</p>
  
  <pre><code>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
      at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:204) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:348) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:129) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:92) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:79) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:194) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:91) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:182) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      at com.XXX.platform.OrderProcessorJob.execute(OrderProcessorJob.java:32) ~[classes/:na]
      at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.3.0.jar:na]
      at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.3.0.jar:na]
  Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
      at redis.clients.util.Pool.getResource(Pool.java:53) ~[jedis-2.9.0.jar:na]
      at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226) ~[jedis-2.9.0.jar:na]
      at redis.clients.jedis.JedisPool.getResource(JedisPool.java:16) ~[jedis-2.9.0.jar:na]
      at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:194) ~[spring-data-redis-1.8.4.RELEASE.jar:na]
      ... 11 common frames omitted
  Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)
      at redis.clients.jedis.Connection.connect(Connection.java:207) ~[jedis-2.9.0.jar:na]
      at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93) ~[jedis-2.9.0.jar:na]
      at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767) ~[jedis-2.9.0.jar:na]
      at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106) ~[jedis-2.9.0.jar:na]
      at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868) ~[commons-pool2-2.4.2.jar:2.4.2]
      at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435) ~[commons-pool2-2.4.2.jar:2.4.2]
      at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) ~[commons-pool2-2.4.2.jar:2.4.2]
      at redis.clients.util.Pool.getResource(Pool.java:49) ~[jedis-2.9.0.jar:na]
      ... 14 common frames omitted
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
      at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
      at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
      at redis.clients.jedis.Connection.connect(Connection.java:184) ~[jedis-2.9.0.jar:na]
      ... 21 common frames omitted
  </code></pre>
  
  <p>I want to get consistent data, thats why I want to use master for read and write, and slave for redundancy and failover.</p>
  
  <p>I am struck on this issue for two days. Any help is highly appreciated.</p>