Tuesday, August 1, 2017

Getting started with Spring XD(exstream Data)in windows environment and Retrieve twitter live tweets data using Spring XD

Step 0: Java 1.7 or above needs to be installed.
Step1 : Download spring xd from below URL Spring XD
Step2: Unzip and place it where ever you desire. I have placed it in F:\softwares\spring-xd
Step3: Open command Prompt- go to F:\softwares\spring-xd\spring-xd-1.2.0.RELEASE\xd\bin
Step 4: F:\softwares\spring-xd\spring-xd-1.2.0.RELEASE\xd\bin>xd-singlenode
Spring xd will start and displayed as mentioned below.



Step 5: Now open another command prompt to run the shell- F:\softwares\spring-xd\spring-xd-1.2.0.RELEASE\shell\bin
Step 6: F:\softwares\spring-xd\spring-xd-1.2.0.RELEASE\shell\bin>xd-shell
Shell prompt will display as mentioned below.



Step 7: Now we need to create twitter steam inside Spring XD shell
Step 8: We need to create an application inside twitter to get the consumer key and consumer secret key
Step 9: I have created application with name- twitterspringxdsearchjava.
Step 10: Please login into - https://apps.twitter.com and create your own application



Step 11: Now we need to run the created twitter application using spring xd shell
Xd> stream create --name twitterspringxdsearchjava --definition "twittersearch --consumerKey=a7gswQzBwemVLW4rFBz3kERXd --consumerSecret=YewRBaxRZUXP85xsOUnquFCTOcESTy5QCTmfQSUfsuk7S1bCVv --query='java' | file" –deploy

Step 12: output file created inside - F:\tmp\xd\output with name – twitterspringxdsearchjava.out
Step 13: This file will have live tweets data in json format.


Thank you very much for for viewing this post




Friday, June 2, 2017

How to load Environment specific properties using Spring PropertyPlaceholderConfigurer

This post will explain how to load environment specific[DEV,SIT,UAT,PROD] properties from the single properties file

Step 1: Create simple java project using eclipse
Step 2: Create a abstract class , with under package - com.javaguru.property, i have created with name - PropertyClient.java
package com.javaguru.property;

public abstract class PropertyClient {

 
 protected String hostName;
 protected String userId;
 protected String password;
 
 
 
 public PropertyClient(String hostName, String userId, String password) {
  super();
  this.hostName = hostName;
  this.userId = userId;
  this.password = password;
  
 }
 
 public PropertyClient(){
  
 }
 
 public String getHostName() {
  return hostName;
 }
 public void setHostName(String hostName) {
  this.hostName = hostName;
 }
 public String getUserId() {
  return userId;
 }
 public void setUserId(String userId) {
  this.userId = userId;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 
 //common methods related to functionality
}
   

Step 3: Create implementation class.
package com.javaguru.property;

public class PropertyClientImpl extends PropertyClient{

 
 public PropertyClientImpl(String hostName, String userId, String password) {
  super(hostName, userId, password);
 }
 public PropertyClientImpl(){
  super();
 }
 //Implementation of abstract methods
}

Step 4: Create property file name called - application.properties under src folder

[DEV]
ftp.dev.hostname=dev.com
ftp.dev.username=user
ftp.dev.password=pass

[SIT]
ftp.sit.hostname=sit.net
ftp.sit.username=user
ftp.sit.password=pass

[UAT]
ftp.uat.hostname=uat.net
ftp.uat.username=user
ftp.uat.password=pass

[PROD]
ftp.prod.hostname=prod.net
ftp.prod.username=user
ftp.prod.password=pass



Step 5: Create applicationContext.xml under src- folder





 

 
   
  application.properties
  
 


    
    
    



Step 6: Create Test class with name - TestEnvSpecificProperty.java- or call the code which ever place you need to load the properties

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javaguru.property.PropertyClient;
import com.javaguru.property.PropertyClientImpl;
/**
 * 
 * @author siva
 *
 */
public class TestEnvSpecificProperty {

 public static void main(String[] args) {
  //Set which environment properties we need to load.
  System.setProperty("env","dev");
  //load the applicationContext.xml file
  ApplicationContext context =  new ClassPathXmlApplicationContext("applicationContext.xml");
  PropertyClient propertyClient= (PropertyClientImpl)context.getBean("propertyClient");
  //print property details
   System.out.println("Host Name: ["+propertyClient.getHostName()+"]");
   System.out.println("User ID: ["+propertyClient.getUserId()+"]");
   System.out.println("Password: ["+propertyClient.getPassword()+"]");
  
 }
}

Step 7: Output would be like below, if you mention System property as dev
Host Name: [dev.com]
User ID: [user]
Password: [pass]

Step 8: Required jars

commons-codec-1.3.jar
commons-collections-3.2.jar
commons-logging-1.1.1.jar
commons-net-3.3.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.4.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.4.RELEASE.jar
spring-expression-3.2.1.release.jar

AddToAny

Contact Form

Name

Email *

Message *