apache commons의 http client와 jaxb로 어떻게 테스트할지에 대해서 작성한 글이다.

특정 URL을 호출하면 사원 정보가 나온다고 가정한다.

<ROOT>

<EMPLOYEE>

<id></id>

<nm></nm>

….

</EMPLOYEE>

<EMPLOYEE>

<id></id>

<nm></nm>

….

</EMPLOYEE>

</ROOT>

 

객체는 Root-List<Employee>의 구조로 생각,

# Root.java 소스

package test;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "ROOT")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
   
    @XmlElement(name="EMPLOYEE")
    private List<Employee> employees;

    public Root() {
        employees = new ArrayList<Employee>();
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

}

 

# Employee.java

package test;

public class Employee {

private String id;
private String nm;

….

… // set, get

}

 

찾고 싶은 사람의 사번을 작성한다.

# XEmployee.java

public class XEmployee {

    private static List<String> employeeList;
   
    public static List<String> getEmployeeList() {
        return employeeList;
    }
   
    static {
        employeeList = new ArrayList<String>();

        employeeList.add("aaa");
         employeeList.add("bbb");
         employeeList.add("ccc");

    }
}

 

찾고 싶은 사람을 찾아서 출력한다.

 

# main 메서드

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class CheckEmp {
    public static final boolean isSaved = false;
    public static final String URL = "http://xxxxxxxx";
    public static JAXBContext jaxbContext;
    static {
        try {
            jaxbContext = JAXBContext
                    .newInstance(new Class[] { test.Root.class });
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

        public static void main(String[] args) throws Exception {

        // connect url
        BufferedReader reader = connectURL();

        // save file
        if (isSaved) {
            saveFile(reader);
        }
       

        // get object
        Root root = getRoot(reader);

        // find
        for (String retiredEmpID : XEmployee.getEmployeeList()) {
            boolean found = false;
            for(Employee emp : root.getEmployees()) {
                if (emp.getID().trim().equalsIgnoreCase(retiredEmpID)) {
                    found = true;
                }
            }
            if (found == false) {
                System.out.println(retiredEmpID);
            }
        }
    }

    private static Root getRoot(BufferedReader reader) throws JAXBException {
        Root root = (Root) jaxbContext.createUnmarshaller().unmarshal(reader);
        return root;
    }

    private static BufferedReader connectURL() throws IOException,
            ClientProtocolException, UnsupportedEncodingException {

        HttpGet httpget = new HttpGet(URL);
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter("http.socket.timeout", 999000);
       
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            throw new RuntimeException("url error");
        }
        InputStream instream = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "utf-8"));
        return reader;
    }

    private static void saveFile(BufferedReader reader) throws IOException {
        Writer fstream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("aaa.txt"), "utf-8"));
        BufferedWriter out = new BufferedWriter(fstream);
        char[] a = new char[1024] ;
        while (reader.read(a) > 0) {
            out.write(a);
        }
        out.flush();
        System.out.println("File created successfully.");
    }

}


# pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>test1123</groupId>
 <artifactId>teswt1</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <dependencies>
  <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>4.1.2</version>
  </dependency>
  <dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
   <version>2.2</version>
  </dependency>
  <dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-xjc</artifactId>
   <version>2.2</version>
  </dependency>
  <dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.2</version>
  </dependency>
 </dependencies>
</project>
Posted by '김용환'
,

 

아키텍트ㅡㄹ은 프로젝트에 착수할 때 자신의 가치를 입증하려는 욕구가 있습니다. 자신의 가치를 입증하는 것이 개인의 기술적 탁월함으로 팀을 좌절시키는 쇼맨십으로 이루어져야 한다고 오해하는 아키텍트들이 있습니다.

아키텍트는 확고한 리더십으로 팀의 존경을 얻어야만 하고 기술과 팀이 운영하는 비즈니스 도메인의 이해가 있어야 합니다.

책임을 지고 다른 이들을 돌보는 청지기 정신(stewardship)은 아키텍트에게 적합한 역할입니다. 아키텍트는 그들의 고객을 위해 최선을 다해야 하며 고객의 요구를 이용하지 말아야 합니다.

시간과 노력은 회사의 자원이므로, 소트프웨어 아키텍트는 사심없는 청지기가 되어야 합니다. 최신의 따끈따끈한 프레워크나 기술 전문 유행어로 이루어진 과도하게 복잡한 시스템은 기업 지출의 희생을 담보로 하고 있습니다. 아키텍트의 활동은 투자 브로커처럼 합리적인 ROI를 창출할 수 있다는 전제 하에 고객의 돈을 사용하고 있어야 합니다.

쇼맨십을 넘는 가치 있는 청지기 정신은 여러분이 다른 사람의 돈을 사용하고 있음을 절대 잊지 않는 것입니다.

- Barry Hawkins

 

위대한 건축가는 머리가 아니라 노력과 풍요로운 마음으로 만들어진다.

- Fank Lloyd Wright

 

아키텍트들이 오늘날 가지고 있는 문제들 중 하나는 분석에 무기력한 것입니다. 이 문제를 더 크게 만드는 것은 미래에 있는 최고의 기술을 짐작하려 하는 것입니다. 그저 지금 당장 좋은 기술을 선택하는 것도 충분히 어려운 일입니다. 하물며 미래에 의미있을 것 같은 기술을 고르는 것은 헛된 일입니다.

- Richard Monson-Haefel

 

최종 사용자에게는 인터페이스가 시스템이다.

- Vinayak Hegde

Posted by '김용환'
,

일반적으로 자바 배치가 많은 경우에 cpu가 튄다. jvm 단위의

이럴 때는 여러가지를 사용하는데, 
1.  cpulimit 를 쓰는 방법
2. 서버를 여러대로 분산하는 법,
3. 웹 서버로 해서 배치를 만드는 법 (quartz 이용)
4. 스케쥴을 잘 정리할 것

자바는 기본적으로 jvm 시작시, 클래스 로딩, jvm 종료시 서버에 부하를 준다. 따라서 이 작업을 최대한 피하게 하는 것이 좋다. 따라서 10분 단위 미만의 배치는 웹 서버로 바꿔 배치를 돌리는 것이 합리적인 것 같다.
Posted by '김용환'
,