title: Use pivotal cloud foundry redis and rabbitmq service using as vcap service in spring boot application
body: <p>I am able to use REDIS and RABBITMQ services which is on pivotal.While binding services I am able to get the Credentials and using that credentials in my application.properties for spring boot project. </p>
  
  <p>But this configuration that I am using is hard-coded in application.Properties
  To Make this configuration dynamically I came to know that we can use vcap services provided by pivotal. </p>
  
  <p>So want to use run-time credentials for redis and rabbimq.</p>
  
  <p>My Code is below for reference.</p>
  
  <p>application.propeties</p>
  
  <pre><code>rabbitmq.host=hostname
  rabbitmq.virtual-host=vhostanme
  rabbitmq.username=username
  rabbitmq.password=password
  rabbit.mainqueue=abhi
  rabbit.errorqueue=abc
  redis.host=redishostname
  redis.port=port
  redis.password=password
  </code></pre>
  
  <p>My Config class:</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Value;
  import org.springframework.stereotype.Component;
  
  @Component
  public class Config {
  
  
      static String rabbitMqHost;
      static String rabbitMqVHost;
      static String rabbitMqUsername;
      static String rabbitMqPassword;
      static String rabbitMqMainQueue;
      static String rabbitMqErrorQueue;
      static String redisHost;
      static int redisPort;
      static String redisPassword;
  
  
      Config() {
      }
  
  
      public static String getRedisHost() {
          return redisHost;
      }
  
      public static void setRedisHost(String redisHost) {
          Config.redisHost = redisHost;
      }
  
      public static int getRedisPort() {
          return redisPort;
      }
  
      public static void setRedisPort(int redisPort) {
          Config.redisPort = redisPort;
      }
  
      public static String getRedisPassword() {
          return redisPassword;
      }
  
      public static void setRedisPassword(String redisPassword) {
          Config.redisPassword = redisPassword;
      }
  
      public static String getRabbitMqMainQueue() {
          return rabbitMqMainQueue;
      }
  
      public static void setRabbitMqMainQueue(String rabbitMqMainQueue) {
          Config.rabbitMqMainQueue = rabbitMqMainQueue;
      }
  
      public static String getRabbitMqErrorQueue() {
          return rabbitMqErrorQueue;
      }
  
      public static void setRabbitMqErrorQueue(String rabbitMqErrorQueue) {
          Config.rabbitMqErrorQueue = rabbitMqErrorQueue;
      }
  
      public static String getRabbitMqHost() {
          return rabbitMqHost;
      }
  
      public static void setRabbitMqHost(String rabbitMqHost) {
          Config.rabbitMqHost = rabbitMqHost;
      }
  
      public static String getRabbitMqVHost() {
          return rabbitMqVHost;
      }
  
      public static void setRabbitMqVHost(String rabbitMqVHost) {
          Config.rabbitMqVHost = rabbitMqVHost;
      }
  
      public static String getRabbitMqUsername() {
          return rabbitMqUsername;
      }
  
      public static void setRabbitMqUsername(String rabbitMqUsername) {
          Config.rabbitMqUsername = rabbitMqUsername;
      }
  
      public static String getRabbitMqPassword() {
          return rabbitMqPassword;
      }
  
      public static void setRabbitMqPassword(String rabbitMqPassword) {
          Config.rabbitMqPassword = rabbitMqPassword;
      }
  
      @Value("${rabbitmq.host}")
      public void setRabbitMqHosts(String url) {
          setRabbitMqHost(url);
      }
  
      @Value("${rabbitmq.virtual-host}")
      public void setRabbitMqVHosts(String url) {
          setRabbitMqVHost(url);
      }
  
      @Value("${rabbitmq.username}")
      public void setRabbitUsernames(String url) {
          setRabbitMqUsername(url);
      }
  
      @Value("${rabbitmq.password}")
      public void setRabbitPasswords(String url) {
          setRabbitMqPassword(url);
      }
  
      @Value("${rabbit.mainqueue}")
      public void setRabbitMainQueues(String url) {
          setRabbitMqMainQueue(url);
      }
  
      @Value("${rabbit.errorqueue}")
      public void setRabbitErrorQueues(String url) {
          setRabbitMqErrorQueue(url);
      }
  
      @Value("${redis.host}")
      public void setRedisHosts(String url) {
          setRedisHost(url);
      }
  
      @Value("${redis.port}")
      public void setRedisPorts(int url) {
          setRedisPort(url);
      }
      @Value("${redis.password}")
      public void setRedisPasswords(String url) {
          setRedisPassword(url);
      }
  }
  </code></pre>
  
  <p>My MessagesConsumer class in which I am using that Conguration to take message from rabbitmq jms queue and save to redis:</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  import javax.annotation.PostConstruct;
  import javax.jms.ConnectionFactory;
  import javax.jms.Message;
  import javax.jms.MessageConsumer;
  import javax.jms.Session;
  
  import org.json.JSONObject;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.jms.core.JmsTemplate;
  import org.springframework.scheduling.annotation.Scheduled;
  import org.springframework.stereotype.Component;
  
  import com.es.consumer.config.Config;
  import com.rabbitmq.jms.admin.RMQConnectionFactory;
  
  import redis.clients.jedis.Jedis;
  import redis.clients.jedis.JedisShardInfo;
  
  @Component
  public class MessagesConsumer {
      @Autowired
      JmsTemplate jmsTemplate;
      final Logger logger = LoggerFactory.getLogger(MessagesConsumer.class);
  
      Jedis jedis;
  
      JedisShardInfo shardInfo;
  
      @PostConstruct
      public void init() {
  
          shardInfo = new JedisShardInfo(Config.getRedisHost(), Config.getRedisPort());
          shardInfo.setPassword(Config.getRedisPassword());
          jedis = new Jedis(shardInfo);
          jedis.connect();
          jedis.select(2);
  
      }
  
      @Bean
      ConnectionFactory connectionFactory() {
  
          RMQConnectionFactory connectionFactory = new RMQConnectionFactory();
          connectionFactory.setUsername(Config.getRabbitMqUsername());
          connectionFactory.setPassword(Config.getRabbitMqPassword());
          connectionFactory.setVirtualHost(Config.getRabbitMqVHost());
          connectionFactory.setHost(Config.getRabbitMqHost());
          return connectionFactory;
  
      }
  
      @SuppressWarnings("rawtypes")
      @Scheduled(fixedRate = 1)
      public void readQueueAndSaveData() {
  // take message process it and save to redis as hmset
  
  }}
  </code></pre>
  
  <p>Any Help would be appreciable.</p>