How do I reduce lines of code in this piece of code? A number of lines are being repeated in updateProfileFlags and fetchProfileObj This method initiates a new connection with the thrift client and returns a profile object

 private TResume fetchProfileObj(int userId) {
    try{
        TTransport transport = new TSocket("172.XX.X.XXX”, 9311);  
        transport.open();
        TProtocol protocol = new  TBinaryProtocol(transport);
        Map&lt;String, String&gt; ids = new HashMap&lt;String, String&gt;();
        ids.put("requestId", "settingsService");
        TResumeService.Client resumeServiceClient = new TResumeService.Client(protocol);
        TResume profileObj = resumeServiceClient.getFullActiveProfileFromUserId(ids, 105, userId, "resman5_1");
        transport.close();
        return profileObj;
    } catch(Exception e) {
        e.printStackTrace();
        return null;
    }
}


This method updates the profile object recieved from the fetchProfile method

private void updateProfileFlags(TResume profile,int recruiterJobAlert) {
    try{
        TTransport transport = new TSocket("172.XX.X.XXX", 9321);  
        transport.open();
        TProtocol protocol = new  TBinaryProtocol(transport);
        Map&lt;String, String&gt; ids = new HashMap&lt;String, String&gt;();
        ids.put("requestId", "settingsService");
        TUpdateResume.Client updateResumeClient = new TUpdateResume.Client(protocol);
        profile.getUser().setResdexVisibility(recruiterJobAlert&gt;0?"a":"c");
        updateResumeClient.saveProfile(ids, 105, profile.getProfile(), "now", "resman5_1", null);
        transport.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
}


Now calling the above methods

public void myTestMethod(){
updateProfileFlags(fetchProfileObj(userId),settings.getRecruiterJobAlert());
}
I am not sure how many lines of code can be reduced but one thing is for sure: whenever you encounter a resource that can be closed, you should use Java try-with-resources feature (since java 7).  Not only this feature saves you the close() operation (one line less!) but also ensures that the resource is properly closed by the end of the try block no matter if it ended successfully or not (the compiler adds a finally clause). in the current code, if an exception is thrown, the Ttransport resource is not closed properly.

I looked at the thrift javadoc and indeed from version 0.10.0, Ttransport is auto-closeable.  So the code on both methods should be 

try (TTransport transport = new TSocket("172.XX.X.XXX", 9321)) {
  transport.open();
  ...  // do not call close()!!
  // do not call close()!! it is added by the compiler
} catch(Exception e) {
  e.printStackTrace();
}


Regarding reducing duplicated lines, I can offer that you make a method to create the ids map.  so if the contents of the map need to be modified due to modifications in the requirements or API, there will be only one place to modify them.  creating constant variables instead of embedded literals is also considered best practice.  This can apply to other literals used in the code

public static final String PROFILE_REQUEST_KEY = "requestId";
public static final String PROFILE_REQUEST_VALUE = "settingsService";
public static final int    PROFILE_WHATEVER_105_MEANS = 105;
public static final String PROFILE_WHATEVER_RESMAN5_1_MEANS = "resman5_1";

public Map&lt;String, String&gt; getProfileRequestProperties() {
  return Collections.singletonMap(PROFILE_REQUEST_KEY, PROFILE_REQUEST_VALUE);
}


now, since we eliminated close(), we can return the value direct from the call to the client, eliminating the creation of the profileObj reference:

return resumeServiceClient.getFullActiveProfileFromUserId(
         getProfileRequestProperties(),
         PROFILE_WHATEVER_105_MEANS,
         userId,
         PROFILE_WHATEVER_RESMAN5_1_MEANS);


so, I ma not sure how many LOC are now present, but im my eyes, the code is clearer and more maintainable and extensible.