LoginSignup
0
0

More than 5 years have passed since last update.

heap

Posted at
public class CustomResultHandler implements ResultHandler {

    private ArrayList<HashMap<String,String>> recordList;
    private CallbackInterface callbackClass;
    private int MAX_COUNT= 100;

    public CustomResultHandler() {
        super();
        this.recordList = new ArrayList<HashMap<String,String>>();
    }

    @Override
    public void handleResult(ResultContext resultContext) {

        recordList.add((HashMap<String, String>) resultContext.getResultObject());

        if(recordList.size() == MAX_COUNT) {
            System.out.println("Invoking Callback method");
            callbackClass.processData(recordList);
            recordList.clear();
        }


    }

    public ArrayList<HashMap<String,String>> getRecordList() {
        return recordList;
    }

    public void setRecordList(ArrayList<HashMap<String,String>> recordList) {
        this.recordList = recordList;
    }

    public Object getCallbackClass() {
        return callbackClass;
    }

    public void setCallbackClass(CallbackInterface callbackClass) {
        this.callbackClass = callbackClass;
    }


}
public interface CallbackInterface {

    public void processData(ArrayList<HashMap<String,String>> recordList);

}
public class CallbackImpl implements CallbackInterface {

    @Override
    public void processData(ArrayList<HashMap<String, String>> recordList) {
        System.out.println("Callback operation invoked for list size: " + recordList.size());

    }

}
public ArrayList<HashMap<String, String>> getAllUsers(CustomResultHandler objCustomResultHandler) {
        //set the custom result handler in sql session
        sqlSession.select("com.memoryleak.poc.persistence.UserMapper.getAllUsers", objCustomResultHandler);

        return userMapper.getAllUsers(objCustomResultHandler);
}
public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        UserService userService = (UserService) context.getBean("userService");

        CustomResultHandler objResultHandler = new CustomResultHandler();
        CallbackInterface objCallback = new CallbackImpl();
        objResultHandler.setCallbackClass(objCallback);

        System.out.println("Users List: " + userService.getAllUsers(objResultHandler));



}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0