๐Ÿ’ป Programming (358)

MyBatis TypeHandler๋ฅผ ์ด์šฉํ•œ ๊ฐ์ฒด๋ฆฌ์ŠคํŠธ ํ•ธ๋“ค๋ง

์ด๋ฒˆ์— ํŠน์ • ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๋‹ค๊ฐ€ JSON ํ˜•ํƒœ์˜ ์ŠคํŠธ๋ง์„ ๊ทธ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ–ˆ๋‹ค๊ฐ€ ๊บผ๋‚ด์จ์•ผํ•˜๋Š” ์ƒํ™ฉ์ด ์ƒ๊ฒผ๋‹ค.

๊ทธ๊ฒƒ๋„ RBD์ธ mysql ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ง์ด๋‹ค. mysql์ด json ์ปฌ๋Ÿผ์„ ์ง€์›ํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ json ํ•จ์ˆ˜๋ฅผ ์ฟผ๋ฆฌ์— ์“ธ ํ•„์š”๊นŒ์ง€๋Š” ์—†๋Š” ์ƒํ™ฉ์ด๋‹ค๋ณด๋‹ˆ ๊ทธ๋ƒฅ varcharํƒ€์ž…์œผ๋กœ ์ปฌ๋Ÿผ์„ ์ •์˜ํ–ˆ๊ณ  ์—ฌ๊ธฐ์— jsonํฌ๋งท์˜ ์ŠคํŠธ๋ง์„ ๊ทธ๋Œ€๋กœ ์ €์žฅํ–ˆ๋‹ค๊ฐ€ ๊บผ๋‚ด์“ธ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผํ–ˆ๋‹ค.

์ผ๋‹จ ํƒ€์ž…ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ๋กœ ํ–ˆ๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด์ง€ ๊ฒ€์ƒ‰์„ ์ข€ ํ•ด๋ดค๋‹ค. ๊ตฌ๊ธ€์—์„œ "Type handler for ArrayList in myBatis" ๋ผ๊ณ  ๊ฒ€์ƒ‰์„ ํ•˜๋‹ˆ ๋งŽ์€ ํฌ์ŠคํŒ…์ด ๊ฒ€์ƒ‰์ด ๋˜์—ˆ๊ณ  ๋‚ด ํ”„๋กœ์ ํŠธ์— ๋งž๊ฒŒ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์“ธ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์ž‘์—… ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

 

์šฐ์„  dto์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด MyJsonDataWrapperClass ๊ฐ€ myJsonData ๋ฆฌ์ŠคํŠธ๋ฅผ ๋“ค๊ณ  ์žˆ๋Š”๋ฐ ์ด๋•Œ MyJsonData ํด๋ž˜์Šค๊ฐ€ ๋ฐ”๋กœ json ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋  ์ •๋ณด์ด๋‹ค.

@Getter
@Setter
@JsonInclude(Include.NON_NULL)
public class MyJsonDataWrapperClass {

    private Integer someIntData;
    private List<MyJsonData> myJsonData = new ArrayList<>();
    
    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}
@Getter
@Setter
@EqualsAndHashCode
public class MyJsonData {
    private String name;
    private int age;

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—๋Š” myJsonData ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ textํƒ€์ž…์˜ ์ปฌ๋Ÿผ์„ ๋งŒ๋“ค์—ˆ๊ณ  mybatis์˜ insert๋ฌธ์— ์‚ฌ์šฉํ•  ํƒ€์ž…ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…์‹œํ•ด์ฃผ์—ˆ๋‹ค.

, myJsonData = #{myJsonData, typeHandler=MyJsonDataTypeHandler}

 

๊ทธ๋ฆฌ๊ณ  ํƒ€์ž…ํ•ธ๋“ค๋Ÿฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•˜์˜€๋‹ค.

@Slf4j
public class MyJsonDataTypeHandler extends BaseTypeHandler<List<MyJsonData>> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<MyJsonData> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, new Gson().toJson(parameter));
    }

    @Override
    public List<MyJsonData> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return convertToList(rs.getString(columnName));
    }

    @Override
    public List<MyJsonData> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return convertToList(rs.getString(columnIndex));
    }

    @Override
    public List<MyJsonData> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return convertToList(cs.getString(columnIndex));
    }

    private List<MyJsonData> convertToList(String myJsonDataListAsString) {
        try {
            return new ObjectMapper().readValue(myJsonDataListAsString, new TypeReference<List<MyJsonData>>() {
            });
        } catch (IOException e) {
            log.error("MyJsonDataTypeHandler failed to convert text to list, myJsonDataListAsString:{}", myJsonDataListAsString, e);
        }
        return Collections.emptyList();
    }
}

 

BaseTypeHandler๋ฅผ ์ƒ์†ํ•˜๋ฉด์„œ List<MyJsonData> ํƒ€์ž…์— ๋Œ€ํ•œ ํ•ธ๋“ค๋Ÿฌ์ž„์„ ๋ช…์‹œํ•ด์ฃผ์—ˆ๊ณ  setter์—๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋ฅผ json ์ŠคํŠธ๋ง์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋„๋ก Gson.toJson() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์˜€๊ณ , getter์—๋Š” ์กฐํšŒํ•œ json ์ŠคํŠธ๋ง์„ ๋‹ค์‹œ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋„๋ก ํ•˜์˜€๋Š”๋ฐ ์ด๋•Œ๋Š” ObjectMapper.readValue()๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค.

 

์กฐํšŒ์‹œ์—๋Š” mybatis ๋งคํ•‘ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด resultMap์„ ์ •์˜ํ•˜์—ฌ MyJsonDataTypeHandler๋ฅผ ์ด์šฉํ•ด์„œ myJsonDataํ•„๋“œ์˜ ๊ฐ’์„ ArrayList๋กœ ๋ณ€ํ™˜ํ•˜๋„๋ก ํ•˜์˜€์œผ๋ฉฐ, ์ด๋ ‡๊ฒŒ ์ •์˜ํ•œ resultMap์„ select ๋ฌธ์˜ resultMap์œผ๋กœ ์„ ์–ธํ•ด์ฃผ์—ˆ๋‹ค.

<resultMap id="myJsonDataClassMap" type="MyJsonDataWrapperClass">
<result property="myJsonData" column="myJsonData" javaType="java.util.ArrayList"
jdbcType="VARCHAR" typeHandler="MyJsonDataTypeHandler" />
</resultMap>

....์ค‘๋žต....

<select id="findMyJsonDataType"
resultMap="myJsonDataClassMap">
SELECT * FROM findMyJsonDataWrapperClass
</select>

 

์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ์†Œ์Šค๋ ˆ๋ฒจ์—์„œ๋Š” ํŠน์ • ๊ฐ์ฒด์˜ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ํ•ธ๋“ค๋ง์„ ํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—๋Š” json ์ŠคํŠธ๋ง์œผ๋กœ ์ €์žฅํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ปฌ๋Ÿผ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ชผ๊ฐœ์„œ ์ €์žฅํ•˜๊ธฐ ์• ๋งคํ•œ ์ƒํ™ฉ์—์„œ json ์ŠคํŠธ๋ง์„ varchar(text) ํƒ€์ž…์œผ๋กœ ํ†ต์œผ๋กœ ์ €์žฅํ•˜๊ณ  ์†Œ์Šค๋ ˆ๋ฒจ์—์„œ๋Š” ๊ฐ์ฒดํƒ€์ž…์œผ๋กœ ํ•ธ๋“ค๋งํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๊ฑด ์ •๋ง ๋ฉ‹์ง„ ๊ธฐ๋Šฅ์ธ๊ฒƒ ๊ฐ™๋‹ค.

 

 

์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ shards failed ๋กœ์ธํ•œ ์กฐํšŒ์˜ค๋ฅ˜ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

์ตœ๊ทผ์— ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ ๋กœ๊ทธ๋ฐฑ ์–ดํŽœ๋”๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์„ฑ๋Šฅ ์ด์Šˆ๊ฐ€ ์žˆ์–ด ์›๋ณต์„ ํ–ˆ๋‹ค.

 

๊ทผ๋ฐ ๊ทธ๋•Œ๋ถ€ํ„ฐ ํ‚ค๋ฐ”๋‚˜์—์„œ ๋กœ๊ทธ ์กฐํšŒ ์‹œ 5 of 240 shards failed ์™€ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ๋ฉด์„œ ์กฐํšŒ์— ์‹คํŒจํ•˜์˜€๋‹ค.

 

์ฒ˜์Œ์—๋Š” type์ด ๋ฐ”๋€Œ๋ฉด์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๊ฑด๊ฐ€ ์‹ถ์—ˆ๊ณ , ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๋ชฐ๋ผ ํŠน์ • ์ธ๋ฑ์Šค๋ฅผ ์‚ญ์ œํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

๊ฐœ๋ฐœํ™˜๊ฒฝ์—์„œ๋Š” ๊ทธ๋ ‡๊ฒŒ ํ–ˆ๋”๋‹ˆ ์กฐํšŒ๊ฐ€ ์ž˜ ๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ์œผ๋‚˜ ์ƒ์šฉํ™˜๊ฒฝ์—์„œ๋Š” ์—ฌ์ „ํžˆ ๋งˆ์ฐฌ๊ฐ€์ง€์˜€๋‹ค.

 

๋˜ํ•œ ์›๋ณตํ•œ ์ผ์ž์˜ ์ธ๋ฑ์Šค ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ด์ „ ๋‚ ์งœ์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค๋“ค๋„ ์กฐํšŒ์— ์‹คํŒจํ•˜์˜€๋‹ค. ๊ทธ๊ฒƒ๋„ ํŠน์ • ์ธ๋ฑ์Šค ํŒจํ„ด์—์„œ๋งŒ ๋ง์ด๋‹ค.

 

ElasticSearch Head ํ”Œ๋Ÿฌ๊ทธ์ธ์œผ๋กœ ๊ฐœ๋ฐœํ™˜๊ฒฝ๊ณผ ์ƒ์šฉํ™˜๊ฒฝ์˜ ์ธ๋ฑ์Šค ์ƒํƒœ์— ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•ด๋ณด์•˜๋Š”๋ฐ, 

๊ฐœ๋ฐœํ™˜๊ฒฝ์—๋Š” ํ‚ค๋ฐ”๋‚˜ ๊ด€๋ จ ์ธ๋ฑ์Šค ํŒŒ์ผ์ด 1๊ฐœ ์žˆ์—ˆ๊ณ  (.kibana_1) ์ƒํƒœ๊ฐ€ ํ‘ธ๋ฅธ์ƒ‰์ด์—ˆ๋‹ค. 

์ƒ์šฉํ™˜๊ฒฝ์—๋Š” ํ‚ค๋ฐ”๋‚˜ ๊ด€๋ จ ํŒŒ์ผ์ด 2๊ฐœ๊ฐ€ ์žˆ์—ˆ๊ณ  (.kibana_2, .kibana_1) .kibana_2 ํŒŒ์ผ์ด ์ฃผํ™ฉ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๊ฐ€ ๋˜์—ˆ๋‹ค.

 

๊ตฌ๊ธ€๋ง์„ ์ข€ ํ•ด๋ณด๋‹ˆ ์ธ๋ฑ์Šค ํŒจํ„ด์„ ์‚ญ์ œํ–ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ์ƒ์„ฑํ•ด๋ณด๋ผ๋Š” ์–˜๊ธฐ๊ฐ€ ์žˆ์–ด์„œ ๊ทธ๋ ‡๊ฒŒ ์ง„ํ–‰ํ–ˆ๋”๋‹ˆ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ์กฐํšŒ๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค ^-^

์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ RestHighLevelClient๋ฅผ ์ด์šฉํ•œ ๋กœ๊ทธ๋ฐฑ ์ปค์Šคํ…€ appender ๋งŒ๋“ค๊ธฐ

AWS์—์„œ ์ œ๊ณตํ•˜๋Š” ES๋ฅผ ์ƒ์šฉ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ๊ทธ์‹œ์Šคํ…œ์œผ๋กœ ํ™œ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ตœ๊ทผ ์ฝ”๋กœ๋‚˜ ๋•๋ถ„์— ์žฌํƒ๊ทผ๋ฌด๋ฅผ ํ•˜๋‹ค๋ณด๋‹ˆ ์ธํ”„๋ผํŒ€์—์„œ ํ‚ค๋ฐ”๋‚˜ ์ ‘์†์„ ์ œํ•œํ•˜๋ฉด์„œ ์žฌํƒ๊ทผ๋ฌด ํ•˜๋Š” ๋‚  ์ƒ์šฉ์„œ๋น„์Šค์— ๋Œ€ํ•œ ๋กœ๊ทธํ™•์ธ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค. ๊ทธ๋ž˜์„œ ํ‚ค๋ฐ”๋‚˜์— Cognito์„œ๋น„์Šค๋ฅผ ์ ์šฉํ•˜์—ฌ ์ธ์ฆ์„ ํ†ตํ•ด์„œ ํ‚ค๋ฐ”๋‚˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •์„ ํ•˜๊ธด ํ–ˆ๋Š”๋ฐ.....๋กœ๊ทธ๊ฐ€ ๊ธฐ๋ก์ด ์•ˆ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” internetitem/logback-elasticsearch-appender ์˜€๋Š”๋ฐ ๊ธฐ๋ณธ์ ์ธ rest api๋ฅผ ํ†ตํ•œ ์ธ๋ฑ์‹ฑ๋งŒ ๊ฐ€๋Šฅํ–ˆ๊ณ  ES์˜ access policy๋ฅผ role ๊ธฐ๋ฐ˜์œผ๋กœ cognito๋ฅผ ์„ค์ •ํ•˜๋ฉด์„œ ์ธ์ฆ๋ฌธ์ œ๋กœ ์ธํ•ด ES๋กœ๊น…์„ ๋ชปํ•˜๊ฒŒ ๋œ ๊ฒƒ์ด๋‹ค. 

๊ทธ๋Ÿผ ์ด ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ• ๊นŒ ํ•˜๋‹ค๊ฐ€ ๋‚ด๋ฆฐ ๊ฒฐ๋ก ์€ "Elasticsearch์—์„œ ์ œ๊ณตํ•˜๋Š” RestHighLevelClient๋ฅผ ์ด์šฉํ•ด์„œ logback appender๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ์จ๋ณด์ง€ ๋ญ~" ์˜€๋‹ค.

์šฐ์„  RestHighLevelClient๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ ค๋ฉด ์•„๋ž˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค. 

compile 'org.elasticsearch:elasticsearch:7.7.1' 
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.7.1' 
compile 'com.github.awslabs:aws-request-signing-apache-interceptor:deb7941e85' 
compile 'org.elasticsearch:elasticsearch-x-content:7.7.1'

์ด๊ฒƒ๋„ ๊ณต์‹ ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ ๋ฌธ์„œ๊ฐ€ ์ œ๋Œ€๋กœ ์—…๋ฐ์ดํŠธ ๋˜์–ด์žˆ์ง€ ์•Š๊ฑฐ๋‚˜ ๋ถˆ๋ช…ํ™•ํ•ด์„œ ๋Ÿฐํƒ€์ž„์‹œ์— ์ž๊พธ ์—๋Ÿฌ๋‚˜์„œ ๊ตฌ๊ธ€๋ง ์—ด์‹ฌํžˆ ํ•ด์„œ ์ฐพ์•„๋ƒˆ๋‹ค

์ž, ์ด์ œ AWS Elasticsearch์— access policy๋ฅผ ์–ด๋–ป๊ฒŒ ์„ค์ •ํ–ˆ๋Š”์ง€ ํ•œ๋ฒˆ ๋ณด์ž.

{
   "Version":"2016-10-17",
   "Statement":[
       {"Effect":"Allow",
         "Principal":{
         "AWS":"arn:aws:iam::77345249:role/my-iam-role"},
         "Action":"es:*",
         "Resource":"arn:aws:es:ap-northeast-2:77345249:domain/my-application/*"},
      {"Effect":"Allow",
         "Principal":{
            "AWS":"arn:aws:sts::77345249:assumed-role/Cognito_Role/CognitoIdentityCredentials"},
         "Action":"es:*",
         "Resource":"arn:aws:es:ap-northeast-2:77345249:domain/my-application/*"  }
    ]
}

์œ„ ์ฒ˜๋Ÿผ ์„ค์ •์„ ํ•˜๋ฉด ๊ธฐ์กด์— ์ž˜ ๋™์ž‘ํ•˜๋˜ ElasticsearchAppender๊ฐ€ ๋กœ๊ทธ๋ฅผ ์ œ๋Œ€๋กœ ๋‚จ๊ธฐ์ง€ ๋ชปํ•˜๊ฒŒ ๋œ๋‹ค. (my-iam-role์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธ์ฆ์„ ํ•ด์•ผ ์ •์ƒ์ ์œผ๋กœ ๋กœ๊น…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค) ๊ทธ๋ž˜์„œ internetitem/logback-elasticsearch-appender ์†Œ์Šค๋ฅผ ์ข€ ๊นŒ ๋ณด์•˜๋”๋‹ˆ ๋‚˜๋ฆ„ AWSAuthentication์ด๋ผ๋Š” ์ธ์ฆ ๊ด€๋ จ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๊ณ  logback ์„ค์ •์œผ๋กœ BasicAuthentication ๋Œ€์‹ ์— ํ•ด๋‹น ์ธ์ฆ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ logback ์„ค์ •์— AWSAuthentication์„ ์„ค์ •ํ•ด์ฃผ์—ˆ์ง€๋งŒ region์„ ์ œ๋Œ€๋กœ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ•˜๋Š” ๋ฒ„๊ทธ์™€ ํ•จ๊ป˜ CredentialChain์„ Default์–ด์ฉŒ๊ตฌ ์ฒด์ธ์„ ์‚ฌ์šฉํ•˜๊ณ ์žˆ์—ˆ๊ณ  role๊ธฐ๋ฐ˜ ์ธ์ฆ์„ ํ•˜๋„๋ก ์„ค์ •์„ ํ•  ์ˆ˜๊ฐ€ ์—†์—ˆ๋‹ค. ์–ด๋–ป๊ฒŒ๋“  ๊ธฐ์กด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ ค๊ณ  ํ•ด๋‹น ์ธ์ฆํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด์„œ ๋ณ„๋„์˜ ์ธ์ฆ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•œ ๋’ค ํ•ด๋‹น ์ธ์ฆ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณด๋ ค๊ณ  ์‹œ๋„ํ•ด๋ณด์•˜์œผ๋‚˜ ๊ฒฐ๊ตญ signed request๋ฅผ ๋‚ ๋ฆฌ์ง€ ๋ชปํ•˜๊ณ  ์ธ์ฆ์˜ค๋ฅ˜๋กœ ES์—์„œ 400 ์—๋Ÿฌ๋งŒ ์ฃผ๊ตฌ์žฅ์ฐฝ ๋ฐ›์•˜๋‹ค.

๊ทธ๋ž˜์„œ RestHighLevelClient๋ฅผ ์ด์šฉํ•ด์„œ ์ธ๋ฑ์‹ฑ๋ถ€ํ„ฐ ํ•ด๋ณด๊ณ  ๊ทธ๊ฒƒ์„ ๊ธฐ๋ฐ˜์œผ๋กœ logback custom appender๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. ์šฐ์„  ์ธ๋ฑ์‹ฑํ•˜๋Š” ๊ฒƒ์€ ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ ๋ฌธ์„œ์— ์นœ์ ˆํ•˜๊ฒŒ ๋‚˜์™€์žˆ๊ธด ํ•˜์˜€์œผ๋‚˜ ์˜ˆ์ œ๊ฐ€ ์ตœ์‹ ํ™”๊ฐ€ ์•ˆ๋˜์–ด์žˆ์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ํ•˜๋Š”๋ฐ๋งŒ๋„ ์‹œ๊ฐ„์„ ๋‚ญ๋น„ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ES์— ์š”์ฒญํ•˜๋Š” ์ˆœ๊ฐ„!!! cognito๊ฐ€ ์ ์šฉ๋˜์–ด์žˆ์–ด ์ธ์ฆ ์‹คํŒจ!! ๊ทธ๋ž˜์„œ ์œ„ ES access policy statement์˜ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์ฑ…์„ ์ถ”๊ฐ€ํ•ด์„œ ์ž„์‹œ๋กœ ํ—ˆ์šฉํ•ด์ค€๋‹ค.

,
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:ap-northeast-2:72345249:domain/my-application/*"
    }

ํ•ด๋‹น ๋„๋ฉ”์ธ์—์„œ ES๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ํ—ˆ์šฉํ•ด์ค€๋‹ค๋Š” ์˜๋ฏธ๋‹ค. 

์—ฌ๊ธฐ๊นŒ์ง€ ์„ฑ๊ณตํ–ˆ๋Š”๋ฐ ์ธ์ฆ์„ ๋„๋Œ€์ฒด ์–ด๋–ค credential์„ ์ด์šฉํ•ด์•ผํ• ์ง€ ๋„ˆ๋ฌด ๋‚œ๊ฐํ–ˆ๋‹ค. ๊ธฐ์กด์— ๊ฒฝ๋ ฅ 20๋…„๊ฐ€๊นŒ์ด ๋œ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•ด๋†“์€ ์ฝ”๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋Œ์•„๊ฐ€์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•˜๋Š” ์‹ฌ์ •์œผ๋กœ ๊ตฌ๊ธ€๋ง์„ ํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ๋™์‹œ์— aws sdk์˜ ์ธ์ฆ๊ด€๋ จ ํด๋ž˜์Šค๋“ค์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณด๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๋ˆˆ์— ๋“ค์–ด์˜จ ๋…€์„. ๋ฐ”๋กœ STSAssumeRoleSessionCredentialsProvider ์ด๋‹ค. ์ด ๋…€์„์„ ์ด์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด credential์„ ๋ฐ›์•„์˜ค๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํ•ด๋‹น credential์„ ์ด์šฉํ•ด์„œ ์ธ์ฆ์— ์„ฑ๊ณตํ–ˆ๋‹ค. 

  private AWSCredentialsProvider awsS3CredentialsProvider() {
      List<AWSCredentialsProvider> credentialsProviders = new ArrayList<>();

      String roleSessionName = "role-session-name-" + System.currentTimeMillis();

      credentialsProviders.add(new STSAssumeRoleSessionCredentialsProvider.Builder(roleArn, roleSessionName)
              .withStsClient(AWSSecurityTokenServiceClientBuilder.defaultClient())
              .build());

      return new AWSCredentialsProviderChain(credentialsProviders);
  }

์—ฌ๊ธฐ์„œ roleArn ์ •๋ณด๋งŒ ์žˆ์œผ๋ฉด ๋‚˜๋จธ์ง€๋Š” aws sdk์—์„œ ์•Œ์•„์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ์ž˜ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค. ์œ ํ›„~~ ์ธ์ฆ์— ์„ฑ๊ณตํ–ˆ์œผ๋‹ˆ ์œ„์—์„œ access policy์— ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ํ–ˆ๋˜ ํ•ญ๋ชฉ์„ ๋‹ค์‹œ ์ง€์›Œ๋„ ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ์œ„ ์ฝ”๋“œ๋ฅผ ์–ป๊ธฐ๊นŒ์ง€ ์•„๋ž˜ ์˜ค๋ฅ˜๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜๋„ ์—†์ด ๋ณด์•˜๋‹ค.

Failed to send events to Elasticsearch: Got response code [403] from server with data {"message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method.

Failed to send events to Elasticsearch: Got response code [403] from server with data {"message":"The security token included in the request is expired"}

๊ทธ๋ฆฌ๊ณ  ES์— max ์ƒค๋“œ ๊ฐœ์ˆ˜๊ฐ€ ๊ฝ‰์ฐจ์„œ ๋กœ๊น…์‹œ ์‹ ๊ทœ ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ๋„ ์žˆ์—ˆ๋Š”๋ฐ ์ด๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋–ด๋‹ค.

Suppressed: org.elasticsearch.client.ResponseException: method [PUT], host [https://vpc-my-application-ig5szafi26tsfulustdplosdu3f4.ap-northeast-2.es.amazonaws.com], URI [/index-2020-06-20/_doc/4ba14a8b96b242d0?timeout=1m], status line [HTTP/1.1 400 Bad Request] {"error":{"root_cause":[{"type":"index_creation_exception","reason":"failed to create index [api-2020-06-20]"}],"type":"validation_exception","reason":"Validation Failed: 1: this action would add [10] total shards, but this cluster currently has [1002]/[1000] maximum shards open;"},"status":400}

ํ‚ค๋ฐ”๋‚˜์—์„œ ์ง์ ‘ PUT myindex-2020-06-20 ๋ช…๋ น์œผ๋กœ ์‹œ๋„ํ•ด๋„ ์‹คํŒจํ–ˆ๊ณ , ์œ„ ์—๋Ÿฌ๋ฉ”์‹œ์ง€์™€ ๋™์ผํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ์—๋Ÿฌ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด max ์ƒค๋“œ๊ฐœ์ˆ˜๋ฅผ ์˜ฌ๋ ค์ฃผ๋˜์ง€ ์•„๋‹ˆ๋ฉด ๊ธฐ์กด์— ์žˆ๋Š” ์ธ๋ฑ์Šค๋“ค ์ค‘์—์„œ ๋ถˆํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ์ข€ ๋‚ ๋ ค์ฃผ๋ฉด ๋œ๋‹ค. ์ผ๋ถ€ ์ธ๋ฑ์Šค๋ฅผ ์‚ญ์ œํ•˜๋‹ˆ ๋กœ๊น…์ด ์ž˜ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋ญ”๊ฐ€ ์ด์ƒํ–ˆ๋‹ค. ํ…Œ์ŠคํŠธ API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋กœ๊ทธ๊ฐ€ 3๊ฐœ๊ฐ€ ์ฐํ˜€์•ผ๋˜๋Š”๋ฐ 1๊ฐœ๋งŒ ์ฐํžˆ๊ณ  ์žˆ์—ˆ๋‹ค. ๋„๋Œ€์ฒด ๋ญ˜๊นŒ ์–ด๋””๊ฐ€ ๋ฌธ์ œ์ผ๊นŒ ํ•œ์ฐธ์„ ๊ณ ๋ฏผํ•˜๊ณ  ์ด๊ฒƒ์ €๊ฒƒ ์ˆ˜์ •ํ•˜๋ฉด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ–ˆ๋‹ค.

์ฒ˜์Œ ๋กœ๊ทธ๋ฉ”์‹œ์ง€ ์ธ๋ฑ์‹ฑ์„ ์œ„ํ•œ ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ–ˆ์—ˆ๋‹ค.

  IndexRequest request = new IndexRequest(indexPrefix + LocalDate.now().toString())
          .id(UuidUtil.getTrackingId())
          .source(document);

  Runnable r = () -> {
      try (RestHighLevelClient esClient = esClient()) {
          esClient.index(request, RequestOptions.DEFAULT);
      } catch (Exception e) {
          log.error("ES logging ERROR!!! e-msg:{}", e.getMessage(), e);
      }
  };
  r.run();

์ €๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋‹ˆ ์•„๋ฌด ๋ฌธ์ œ์—†์ด ๋กœ๊น…์„ ํ–ˆ์œผ๋‚˜ ๋กœ๊ทธ๊ฐ€ 3๊ฐœ์ค‘ 1๊ฐœ๋งŒ ๊ธฐ๋ก์ด ๋˜๊ณ ์žˆ์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ IndexRequest์˜ create์„ค์ •(default:false)์„ true๋กœ ์ถ”๊ฐ€ํ•ด๋ณด์•˜๋‹ค. ์™ ์ง€ ๋ฎ์–ด์“ฐ๋Š” ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ด ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋žฌ๋”๋‹ˆ ์•„๋ž˜ "์˜ค๋ฅ˜ 1" ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ์ด ๋˜์—ˆ๊ณ , version ์„ค์ •์„ ํ–ˆ๋”๋‹ˆ ์˜ค๋ฅ˜ 2๊ฐ€ ์ถœ๋ ฅ์ด ๋˜์—ˆ๋‹ค. 

// ์˜ค๋ฅ˜ 1 : create(true) ์ถ”๊ฐ€ํ–ˆ๋”๋‹ˆ
status:CONFLICT, e-msg:Elasticsearch exception [type=version_conflict_engine_exception, reason=[bf0af4cfa5b64945]: version conflict, document already exists (current version [1])]

// ์˜ค๋ฅ˜ 2 : version ์„ค์ •์„ ํ•ด์คฌ๋”๋‹ˆ
create operations do not support explicit versions. use index instead

// ์˜ค๋ฅ˜ 3 : create ์„ค์ •์„ ๋‹ค์‹œ ๋บ๋”๋‹ˆ
internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead

// ์˜ค๋ฅ˜ 4 : ifSeqNo๋งŒ ์„ค์ •ํ–ˆ๋”๋‹ˆ
ifSeqNo is set, but primary term is [0];

// ์˜ค๋ฅ˜ 5 : ifPrimaryTerm๋„ ์„ค์ •ํ–ˆ๋”๋‹ˆ
version conflict, required seqNo [329563504204790], primary term [1592662218337]. but no document was found]

 

ํ•˜๋‚˜ ๋‘˜ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ๋‹ค์‹œ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ๋ฅผ ๋ช‡ ๋ฒˆ ํ•˜๊ณ ๋‚˜๋‹ˆ ๋™์ผํ•œ ๋ฒ„์ „์œผ๋กœ ์—ฌ๋Ÿฌ ๋„ํ๋จผํŠธ๋ฅผ ์ธ๋ฑ์‹ฑํ•˜๋ ค๋‹ค๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ(versioning๋ฌธ์ œ)ํ•˜๋Š” ๊ฒƒ์ด๋ผ ์ถ”์ธกํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , IndexRequest์— ์„ธํŒ…ํ•˜๋Š” ๊ฐ’์— version์ด ์žˆ์–ด์„œ ๊ทธ๊ฒƒ ๋•Œ๋ฌธ์ผ ๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ์—ˆ์œผ๋‚˜ ๋ฌธ์ œ๋Š” id์˜€๋‹ค. -_-;; ์šฐ์„ , ๋‚˜์˜ ๋ชฉ์ ์€ ๋ชจ๋“  ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ๋ ค๋Š” ๊ฑฐ์˜€๊ณ  ๋„ํ๋จผํŠธ๋ฅผ ๋ฒ„์ €๋‹ํ•ด๊ฐ€๋ฉด์„œ ์—…๋ฐ์ดํŠธ ํ•  ์ผ์€ ์—†๋Š”๋ฐ ์™œ ์ž๊พธ ๋ฒ„์ €๋‹ ๊ด€๋ จ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์ผ๊นŒ? ๊ณ ๋ฏผ์„ ์ข€ ํ•˜๋‹ค๊ฐ€ ๋ฐœ๊ฒฌํ•œ ์‚ฌ์‹ค! id๊ฐ’์„ ํŠน์ • ํ”„๋กœ์„ธ์Šค๋ฅผ ํŠธ๋ž˜ํ‚นํ•˜๊ธฐ์œ„ํ•œ ๋˜‘๊ฐ™์€ uuid๊ฐ’์„ ๋„ฃ๊ณ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ id์— ์„ธํŒ…ํ•˜๋Š” ๊ฐ’์„ ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜๋…ธ์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ์—ˆ๋‹ค.

IndexRequest request = new IndexRequest(indexPrefix + LocalDate.now().toString())
                .id(String.valueOf(System.nanoTime()))
                .source(document);

๊ทธ๋žฌ๋”๋‹ˆ ๋”์ด์ƒ ์˜ค๋ฅ˜๋„ ๋‚˜์ง€ ์•Š๊ณ  ์ธ์ฆ๋„ ์ž˜ํ•˜๊ณ  ์ •์ƒ์ ์œผ๋กœ cognito ์„ค์ •์„ ์™„๋ฃŒํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์‹ค์ œ ์ƒ์šฉ์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉํ•จ์— ์žˆ์–ด์„œ ๋ฒ„๊ทธ๊ฐ€ ์—†๋Š”์ง€ ๋˜๋Š” ์„ฑ๋Šฅ์— ๋ฌธ์ œ๊ฐ€ ์—†์„์ง€๋Š” ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ์ข€ ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

ํ™ฉ๊ธˆ๊ฐ™์€ ์ฃผ๋ง์— ์•„๊ฐ€๋ž‘ ๋†€์•„์ฃผ์ง€๋„ ๋ชปํ•˜๊ณ  ์ƒˆ๋ฒฝ๊นŒ์ง€ ์ด๋Ÿฌ๊ณ  ์žˆ๋‹ค๋‹ˆ ใ…œใ…œ

๊ทธ๋ž˜๋„ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ๋ฅผ ์ฐพ์•„์„œ ๊ธฐ์˜๋‹ค. ์”ป๊ณ  ์ž์•ผ๊ฒ ๋‹ค...

 

[6์›” 24์ผ ์—…๋ฐ์ดํŠธ]

๋ถ€ํ•˜ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ try with resource ๊ตฌ๋ฌธํ˜•ํƒœ๋กœ index ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ BAD_REQUEST Unable to parse response body ์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๋‚˜ ์•„๋‹ˆ๋ฉด "You have exceeded the number of permissible concurrent requests with unique IAM Identities. Please retry." ์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๋ฅผ ๋ณด๊ฒŒ๋  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒ์šฉ๋กœ๊ทธ์ฒ˜๋Ÿผ 1์ดˆ๋‚ด์— ์ˆ˜๋ฐฑ~์ˆ˜์ฒœ ๊ฑด์˜ ๋กœ๊ทธ๋ฅผ ์Œ“์•„์•ผ ํ•œ๋‹ค๋ฉด IndexRequest๋Œ€์‹  BulkRequest๋ฅผ bulk๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœํ•˜๋„๋ก ํ•˜๊ณ , try with resource ๊ตฌ๋ฌธ์ด ์•„๋‹Œ client๋ฅผ ํ•œ๋ฒˆ๋งŒ ์ƒ์„ฑํ•œ ๋‹ค์Œ ๊ณ„์† ์žฌ์‚ฌ์šฉํ•˜๋„๋ก ํ•ด์•ผํ•œ๋‹ค. ๊ด€๋ จ๋œ ๋‚ด์šฉ์€ ์•„๋ž˜ ๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-limits.html

www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html

 

[6์›” 30์ผ ์—…๋ฐ์ดํŠธ]

BulkRequest๋„ bulk ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ synchronousํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋กœ๊น…ํ•˜๋Š” ๊ฒƒ์ด ๊ธฐ์กด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง์˜ ์†Œ์š”์‹œ๊ฐ„์— ์˜ํ–ฅ์„ ์ฃผ๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋กœ๊น…์„ ๋งค์šฐ ๋นˆ๋ฒˆํ•˜๊ฒŒ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” bulkAsync ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ bulk ๋ฉ”์„œ๋“œ๋Š” bulkAsync ๋ฉ”์„œ๋“œ๋ณด๋‹ค 2๋ฐฐ์˜ ์„ฑ๋Šฅ ์ €ํ•˜ ํ˜„์ƒ์„ ๊ฐ€์ ธ์˜จ๋‹ค. ํ…Œ์ŠคํŠธ ์‹œ ES๋Š” AWS ES 7.1 ๋ฒ„์ „์œผ๋กœ ํ…Œ์ŠคํŠธํ–ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ํŽ˜์ด์ง€ ์ฒซ ๋ถ€๋ถ„์— ๋ช…์‹œํ–ˆ๋“ฏ์ด 7.7.1 ๋ฒ„์ „์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

[7์›” 7์ผ ์—…๋ฐ์ดํŠธ]

์„ธ ๊ฐ€์ง€ ์ด์Šˆ๊ฐ€ ์ถ”๊ฐ€๋กœ ๋ฐœ๊ฒฌ๋˜์—ˆ๋‹ค.

 

์ฒซ ๋ฒˆ์งธ, 6์›” 24์ผ ์—…๋ฐ์ดํŠธ์— ๋‚˜์™”๋˜ ๋ฌธ์ œ๊ฐ€ ๋˜ ๋‚˜์™”๋‹ค. 

method [POST], host [https://vpc-elasticsearch.ap-northeast-2.es.amazonaws.com], URI [/_bulk?timeout=1m], status line [HTTP/1.1 400 Bad Request]
{"Message":"You have exceeded the number of permissible concurrent requests with unique IAM Identities. Please retry."}

์ด๋ฒˆ์—๋Š” ์†Œ์Šค๋ ˆ๋ฒจ์—์„œ ๋งŽ์€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๊ฒŒ ์•„๋‹ˆ์—ˆ์ง€๋งŒ, AWS ์ฝ˜์†”์— ์ ‘์†ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ ์ข…์ข… ๋ฐœ์ƒํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์„ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ ๊ณ ๋ฏผ์„ ํ•ด๋ณด์•„๋„ ์ด ๋ถ€๋ถ„๊นŒ์ง€ ์ปจํŠธ๋กค ํ•  ์ˆ˜๋Š” ์—†์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค.

 

๋‘ ๋ฒˆ์งธ, ์˜๋„์น˜ ์•Š๊ฒŒ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ข…๋ฃŒ๋˜๋Š” ํ˜„์ƒ์ด ๊ฐœ๋ฐœ์„œ๋ฒ„์—์„œ ๋ฐœ๊ฒฌ๋˜์—ˆ๋‹ค.

IllegalStateExceptionRequest cannot be executed; I/O reactor status: STOPPED

์ด๊ฑด ์˜ˆ์™ธ๋ฅผ ์žก์•„์„œ ์ƒˆ๋กœ์šด ํด๋ผ์ด์–ธํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋„๋ก ์ˆ˜์ •ํ•˜๊ธด ํ–ˆ์œผ๋‚˜....์™œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ๋Š”์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†์–ด ๋‹ต๋‹ตํ•˜๋‹ค.

 

์„ธ ๋ฒˆ์งธ, ๋ถ€ํ•˜๋ฅผ ๋งŽ์ด ์ค€ ์ธ์Šคํ„ด์Šค์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฐœ์ƒ (thread: I/O dispatcher 2)

Connection lease request time out:
at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:411)
at org.apache.http.nio.pool.AbstractNIOConnPool.processNextPendingRequest(AbstractNIOConnPool.java:391)
at org.apache.http.nio.pool.AbstractNIOConnPool.release(AbstractNIOConnPool.java:355)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.releaseConnection(PoolingNHttpClientConnectionManager.java:364)
at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.releaseConnection(AbstractClientExchangeHandler.java:245)
at org.apache.http.impl.nio.client.MainClientExec.responseCompleted(MainClientExec.java:387)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:172)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:448)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:338)
at org.apache.http.impl.nio.client.InternalRequestExecutor.inputReady(InternalRequestExecutor.java:83)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:121)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
at java.lang.Thread.run(Thread.java:748)

 

 

 

์ฐธ๊ณ ์ž๋ฃŒ:

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-document-index.html http://logback.qos.ch/manual/appenders.html

 

 

์ž ์ด ๊นจ์„œ ์ƒˆ๋ฒฝ์— ํฌ์ŠคํŒ…ํ•˜๊ธฐ๋Š” ์ •๋ง ์˜ค๋žœ๋งŒ์ด๋„ค์š”

์ด๋ฒˆ์—๋Š” ์ฝ”ํ‹€๋ฆฐ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค.

 

์ฝ”ํ‹€๋ฆฐ์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•๋“ค

1. ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

data class Customer(val name: String, val email: String)

์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ์ด ํ•œ์ค„์ด๋ฉด ๊ฐ„๋‹จํžˆ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์•„๋ž˜ ๋ฉ”์„œ๋“œ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

- getters, setters

- equals()

- hashCode()

- toString()

- copy() <-- ์ž๋ฐ”์˜ clone๊ณผ ๊ฐ™์€ ๋…€์„

- component1(), component2(), ... <--์ด๊ฑด ๋‚˜์ค‘์— ํด๋ž˜์Šค์— ๋Œ€ํ•ด ์ž์„ธํžˆ ๋ณด๊ฒŒ๋  ๋•Œ ์•Œ๊ฒŒ๋  ๊ฒƒ ๊ฐ™๋„ค์š”

 

2. ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ธฐ๋ณธ ๊ฐ’ ์„ค์ •

ํ•จ์ˆ˜ ์ •์˜ ์‹œ ์ „๋‹ฌ๋ฐ›์„ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

fun foo(a: Int = 0, b: String = "") { ... }

์œ„ ์ฝ”๋“œ๋Š” foo ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ Int ํƒ€์ž…๊ณผ String ํƒ€์ž…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ 0๊ณผ ๋นˆ ์ŠคํŠธ๋ง ""์„ ์ด์šฉํ•˜์—ฌ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ foo()๋ผ๊ณ  ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

3. ๋ฆฌ์ŠคํŠธ ํ•„ํ„ฐ๋ง

val positives = list.filter { x -> x > 0 }

์ž๋ฐ”์˜ stream์—์„œ ํ•„ํ„ฐ๋ง ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋žŒ๋‹ค์‹์„ ์ด์šฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ๋“ค๋งŒ ์„ ๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ it ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

val positives = list.filter { it > 0 }

 

4. ์ปฌ๋ ‰์…˜์—์„œ ํŠน์ • ์š”์†Œ ์กด์žฌ์œ ๋ฌด ํ™•์ธํ•˜๊ธฐ

// ํŠน์ • ์ŠคํŠธ๋ง์ด ๋ฆฌ์ŠคํŠธ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
if ("john@example.com" in emailsList) { ... }

// ํŠน์ • ์ŠคํŠธ๋ง์ด ๋ฆฌ์ŠคํŠธ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธ
if ("jane@example.com" !in emailsList) { ... }

 

5. ์ŠคํŠธ๋ง ํฌ๋งคํŒ… (String Interpolation)

var name = "ํ™ฉ๋น„ํ™"
println("Name $name")

์ŠคํŠธ๋ง ๋‚ด์— ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์œผ๋กœ ์ž๋ฐ”์˜ ์ŠคํŠธ๋ง ํฌ๋งคํ„ฐ์™€ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์ด๋‹ค.

 

6. ์ธ์Šคํ„ด์Šค ๊ฒ€์‚ฌ (Instance Check)

when (x) {
    is Foo -> ...
    is Bar -> ...
    else   -> ...
}

์ฝ”ํ‹€๋ฆฐ์—์„œ when ์€ ์ž๋ฐ”์˜ switch ๋ฌธ๊ณผ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์œผ๋กœ ์ข€ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ switch๋ฌธ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

 

7. Map ์ˆœํšŒํ•˜๊ธฐ (Traversing a map/list of pairs)

for ((k, v) in map) {
    println("$k -> $v")
}

์ž๋ฐ”์—์„œ๋Š” keySet์ด๋‚˜ values๋ฅผ ๊ฐ๊ฐ ๊ฐ€์ ธ์™€์„œ ์ฒ˜๋ฆฌํ•ด์•ผ ํ–ˆ์ง€๋งŒ ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” key, value ๋ฅผ ๋™์‹œ์— ๊ฐ€์ ธ์™€์„œ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

8. ๋ฒ”์œ„ ์‚ฌ์šฉ๋ฒ•

loop๋ฅผ ์œ„ํ•ด์„œ๋Š” ํŠน์ • ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ๋ฒ”์œ„์ง€์ •๋ฒ•์„ ์ œ๊ณตํ•ด์ฃผ๊ณ  ์žˆ๋‹ค.

for (i in 1..100) { ... }  // closed range: 100 ํฌํ•จ
for (i in 1 until 100) { ... } // half-open range: 100 ๋ฏธํฌํ•จ
for (x in 2..10 step 2) { ... }	// 2๋ถ€ํ„ฐ 2์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ 10๊นŒ์ง€ ...
for (x in 10 downTo 1) { ... }	// 10๋ถ€ํ„ฐ 1์”ฉ ๊ฐ์†Œํ•˜๋ฉด์„œ 1๊นŒ์ง€ ...
if (x in 1..10) { ... }	// ๋งŒ์•ฝ x๊ฐ€ 1์ด์ƒ 10์ดํ•˜๋ฉด ...

 

9. Read-Only List/Map

val list = listOf("a", "b", "c")
val map = mapOf("a" to 1, "b" to 2, "c" to 3)

listOf, mapOf ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ๊ธฐ์ „์šฉ ๋ฆฌ์ŠคํŠธ๋‚˜ ๋งต์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

10. ํ‚ค๋ฅผ ์ด์šฉํ•œ ๋งต์˜ ๊ฐ’ ์ฝ์–ด์˜ค๊ธฐ

println(map["key"])
map["key"] = value

์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ๋งต์˜ ์š”์†Œ์— ์ ‘๊ทผํ•  ๋•Œ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

11. Lazy ์†์„ฑ

val p: String by lazy {
    // compute the string
}

 

12. ํ™•์žฅ ํ•จ์ˆ˜ (Extension Functions)

fun String.spaceToCamelCase() { ... }

"Convert this to camelcase".spaceToCamelCase()

String์˜ ํ™•์žฅํ•จ์ˆ˜๋ฅผ ์ž„์˜๋กœ ์ถ”๊ฐ€์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค

 

13. ์‹ฑ๊ธ€ํ„ด ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ

object Resource {
    val name = "Name"
}

 

14. if not null ์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑ

val files = File("Test").listFiles()

println(files?.size)

files์˜ size๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ nullable์„ ์˜๋ฏธํ•˜๋Š” ?๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด not null์ผ ๊ฒฝ์šฐ์—๋งŒ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ files๊ฐ€ null์ด๋ผ๋ฉด null์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

15. if not null and else

val files = File("Test").listFiles()

println(files?.size ?: "empty")

์ด๋ฒˆ์—๋Š” if not null ์— else ์กฐ๊ฑด์„ ๋ถ™์—ฌ null์ด๋ฉด "empty" ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜์˜€๋‹ค.

 

16. null์ผ ๊ฒฝ์šฐ ๋ฌธ(statement) ์‹คํ–‰ํ•˜๊ธฐ

val values = ...
val email = values["email"] ?: throw IllegalStateException("Email is missing!")

 

17. possibly empty collection์—์„œ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ

val emails = ... // might be empty
val mainEmail = emails.firstOrNull() ?: ""

 

18. null์ด ์•„๋‹Œ ๊ฒฝ์šฐ ์‹คํ–‰ํ•˜๊ธฐ

val value = ...

value?.let {
    ... // ๋ธ”๋Ÿญ๋‚ด์˜ ๋ฌธ์žฅ์€ value๊ฐ€ not null์ผ ๊ฒฝ์šฐ์— ์‹คํ–‰ํ•œ๋‹ค
}

 

19. nullable Boolean

val b: Boolean? = ...
if (b == true) {
    ...
} else {
    // `b` is false or null
}

Booleanํƒ€์ž…์˜ ๋ณ€์ˆ˜๊ฐ€ null์ธ์ง€ ์•„๋‹Œ์ง€ ๋ณ„๋„๋กœ ๊ฒ€์‚ฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

20. ๋‘ ๋ณ€์ˆ˜์˜ ๊ฐ’ ์Šค์™‘ํ•˜๊ธฐ

var a = 1
var b = 2
a = b.also { b = a }

์ž๋ฐ”์—์„œ๋Š” ๋‘ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ swapํ•  ๋•Œ ์ž„์‹œ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€๋กœ ๋งŒ๋“ค์–ด์„œ swapํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด์ง€๋งŒ ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

์ด์™ธ์—๋„ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ž๋ฐ”๋ณด๋‹ค ๋” ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•ด์ง„ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ๋ฒ•์ด ์žˆ์œผ๋‚˜ ์ด ์ •๋„๋งŒ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

๋‚˜๋จธ์ง€๋Š” ์•„๋ž˜ ์ถœ์ฒ˜์— ์–ธ๊ธ‰ํ•œ ์ฝ”ํ‹€๋ฆฐ ๊ณต์‹์‚ฌ์ดํŠธ์—์„œ ํ™•์ธํ•ด๋ณด์„ธ์š”.

 

์–ด๋Š ์ •๋„ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ๋ฒ•์— ์ต์ˆ™ํ•ด์ง€๋ฉด ์‰ฌ์šด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด๋ฅผ ์ฝ”ํ‹€๋ฆฐ์œผ๋กœ ํ•˜๋ฉด์„œ ๋น ๋ฅด๊ฒŒ ์ตํ˜€๋ณผ ๊ณ„ํšŒ์ž…๋‹ˆ๋‹ค.

 

 

 

์ถœ์ฒ˜ : https://kotlinlang.org/docs/reference/idioms.html

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ๊ฐ„๋žตํ•˜๊ฒŒ ์ฝ”ํ‹€๋ฆฐ์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ, ์ž๋ฐ”๋ฅผ ๋งŽ์ด ๋‹ฎ์€ ์–ธ์–ด๋ผ๊ณ  ๋Š๊ผˆ์œผ๋ฉฐ ๋งค์šฐ ์˜ค๋žœ๋งŒ์— ์ƒˆ๋กœ์šด ์–ธ์–ด๋ฅผ ๊ณต๋ถ€ํ•˜๋Š” ํ„ฐ๋ผ ์ƒˆ๋กญ๊ณ  ์‹ ๊ธฐํ•˜๊ณ  ํฅ๋ฏธ๋กœ์›€์„ ๋งŽ์ด ๋Š๊ผˆ๋‹ค. ๊ทธ๋ž˜์„œ ํšŒ์‚ฌ ๋„์„œ๊ด€์—์„œ ์ฝ”ํ‹€๋ฆฐ in action ์ฑ…์„ ๋นŒ๋ ค์„œ ์ฝ์–ด๋ณผ ์˜ˆ์ •์ด๋ฉฐ, ์ฑ…์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ๋„ ํฌ์ŠคํŒ…์„ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

 

์šฐ์„  ๊นŠ์ด์žˆ๊ฒŒ ๋ฌธ๋ฒ•๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์ฝ”ํ‹€๋ฆฐ์ด ์–ด๋–ค ์–ธ์–ด์ธ์ง€์— ๋Œ€ํ•œ ๋‚ด์šฉ๋ถ€ํ„ฐ ์ข€ ๋” ์ž์„ธํžˆ ์•Œ์•„์•ผ ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด ์ฑ…์—์„œ ๊ด€๋ จ ๋ถ€๋ถ„์„ ์ฝ์–ด๋ณด์•˜๊ณ  ๊ทธ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด๋ณธ๋‹ค.

 

์ฝ”ํ‹€๋ฆฐ์˜ ์ฃผ์š” ํŠน์„ฑ

1. ๋Œ€์ƒํ”Œ๋žซํผ: ์„œ๋ฒ„, ์•ˆ๋“œ๋กœ์ด๋“œ ๋“ฑ JVM์ด ์„ค์น˜๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ํ”Œ๋žซํผ

- ์ด ๋ถ€๋ถ„์€ ์ž๋ฐ”๋ฅผ ๋Œ€์ฒดํ•  ์ƒˆ๋กœ์šด ์–ธ์–ด์ž„์„ ํ™•์‹คํžˆ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

2. ์ •์  ํƒ€์ž… ์–ธ์–ด

- ์ปดํŒŒ์ผ ์‹œ์— ์˜ค๋ฅ˜๋ฅผ ๊ฐ์ง€ํ•˜์—ฌ ๋ฐฐํฌ ํ›„ ๋Ÿฐํƒ€์ž„์— ๋ฐœ์ƒํ•  ์˜ค๋ฅ˜๋ฅผ ๋ฏธ๋ฆฌ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

3. ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ง€์›

- ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ•ต์‹ฌ๊ฐœ๋…์€ 3๊ฐ€์ง€๊ฐ€ ์žˆ์ฃ . ์ผ๊ธ‰์‹œ๋ฏผ(first-class), ๋ถˆ๋ณ€์„ฑ(immutability), ๊ทธ๋ฆฌ๊ณ  ๋ถ€์ˆ˜ํšจ๊ณผ(side-effect)๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ. ์ฝ”๋“œ๋ฅผ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๊ณ , ๋‹ค์ค‘ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์•ˆ์ „ํ•˜๋ฉฐ, ํ…Œ์ŠคํŠธ๊ฐ€ ์‰ฝ๋‹ค๊ณ  ์–˜๊ธฐํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๋‹ค์ค‘ ์Šค๋ ˆ๋“œ ์‚ฌ์šฉ์ด ์•ˆ์ „ํ•˜๋‹ค๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์™ธ๋ถ€ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ณ€๊ฒฝ์„ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์–˜๊ธฐํ•ฉ๋‹ˆ๋‹ค.

 

4. ๋ฌด๋ฃŒ ์˜คํ”ˆ์†Œ์Šค

- ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํŠน์ง• ์ค‘ ํ•˜๋‚˜๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์ฃ . ๊ทธ๋™์•ˆ ํ•œ๊ตญ์˜ ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์— ํฐ ๊ธฐ์—ฌ๋ฅผ ํ•œ Java๋Š” ์˜ค๋ผํด์˜ ์†์— ๋„˜์–ด๊ฐ„ ๋’ค๋กœ ์œ ๋ฃŒํ™”๊ฐ€ ์ง„ํ–‰๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๊ตญ ๊ธฐ์—…๋“ค์—์„œ๋Š” ์ž๋ฐ”๋ฅผ ๋Œ€์ฒดํ•  ์–ธ์–ด๋ฅผ ์ฐพ์œผ๋ ค๊ณ  ํ• ๊ฒ๋‹ˆ๋‹ค. 

 

5. ๊ธฐ์กด Java ์†Œ์Šค์ฝ”๋“œ์™€์˜ ์—ฐ๋™

- ์ด๊ฑด ๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ์ฝ”ํ‹€๋ฆฐ์˜ ์ •๋ง ์ค‘์š”ํ•œ ํŠน์„ฑ์ด๋‹ค. ๊ธฐ์กด ์ž๋ฐ”์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ฝ”ํ‹€๋ฆฐ ํ”„๋กœ์ ํŠธ๋กœ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์€ ์ •๋ง ํ›Œ๋ฅญํ•œ ํŠน์„ฑ์ด๋‹ค. 

 

์ฝ”ํ‹€๋ฆฐ์˜ ์ฒ ํ•™

1. ์‹ค์šฉ์„ฑ : ์ด๊ฑด ๋ญ ๋งํ•  ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ, ๋‹จ์ง€ ์—ฐ๊ตฌ๋ฅผ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ๊ฐœ๋ฐœ๋œ ์–ธ์–ด๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ์ 

2. ๊ฐ„๊ฒฐ์„ฑ : ๋”์ด์ƒ ๊ฐœ๋ฐœ์ž๊ฐ€ getter, setter, toString ๋“ฑ์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ํ•„์š”์—†๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค. 

3. ์•ˆ์ „์„ฑ : ์ •์  ํƒ€์ž… ์–ธ์–ด์ด๋‹ˆ ์ปดํŒŒ์ผ ์‹œ์ ์— ์˜ค๋ฅ˜๋ฅผ ์ฐพ์•„๋‚ธ ๋‹ค๋Š” ์ , ๊ทธ๋ฆฌ๊ณ  ์ž๋ฐ”์—์„œ ํ”ํžˆ ๋ณผ ์ˆ˜ ์žˆ๋Š” ClassCastException, NPE ๋“ฑ์„ ๋ฏธ๋ฆฌ๋ฏธ๋ฆฌ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ๋‹ค๋Š” ์  ๋“ฑ์ด ์žˆ๋‹ค.

4. ์ƒํ˜ธ ์šด์šฉ์„ฑ : ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ๊ณ  ์ž๋ฐ” ์ฝ”๋“œ์—์„œ ์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์‹ฌ์ง€์–ด ์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ์—์„œ ์ž๋ฐ”๋กœ ์ •์˜ํ•œ ํด๋ž˜์Šค๋ฅผ ์ƒ์†/๊ตฌํ˜„๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋˜ํ•œ, ์ž๋ฐ”-์ฝ”ํ‹€๋ฆฐ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธฐ์กด ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ์ฝ”ํ‹€๋ฆฐ ์–ธ์–ด๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋Š” ์ธํ…”๋ฆฌJ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ดํด๋ฆฝ์Šค๋‚˜ ์›น์—์„œ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

๋‹ค์Œ์€ ์ฝ”ํ‹€๋ฆฐ ๋ฌธ๋ฒ•์„ ํ…Œ์ŠคํŠธ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ๋†€์ดํ„ฐ์ด๋‹ค. ๊ฐ์ข… ์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ ์ƒ˜ํ”Œ ์˜ˆ์ œ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฝ”ํ‹€๋ฆฐ์— ๋Œ€ํ•ด ๋” ๊ณต๋ถ€ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค๋ฉด ์ž์ฃผ ๋“ค๋Ÿฌ ๋†€๋‹ค๊ฐ€์ž.

 

์ฝ”ํ‹€๋ฆฐ ์˜จ๋ผ์ธ ๋†€์ดํ„ฐ : https://play.kotlinlang.org/

Try Kotlin

Try Kotlin right in the browser.

try.kotlinlang.org

 

ํšŒ์‚ฌ์—์„œ AWS Elasticsearch๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์šฉ์„œ๋น„์Šค์˜ ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๊ทธ ์šฉ๋Ÿ‰์ด ์ข€ ๋งŽ์•„ 10TB์šฉ๋Ÿ‰์œผ๋กœ 1๋‹ฌ ์ •๋„๋ฐ–์— ๋ฒ„ํ‹ฐ๊ณ  ์žˆ์ง€ ๋ชปํ•ด ์ด๋Ÿฐ ์ €๋Ÿฐ ๋ฆฌ์„œ์น˜๋ฅผ ์ข€ ํ•˜๋‹ค๊ฐ€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ S3๋กœ ๋ฐฑ์—…ํ•ด๋ณด๊ธฐ๋กœ ํ•˜์˜€๊ณ  ๊ทธ ๊ณผ์ •์„ ๊ธฐ๋กํ•ด๋ณธ๋‹ค.

 

 

์ฐธ๊ณ ๋กœ AWS SDK๋Š” 1.11.483 ์‚ฌ์šฉ์ค‘์ด๋ฉฐ, role ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด access_key, secret_key๋“ฑ์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

์šฐ์„  ์•„๋ž˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

// ES snapshot S3 ์ €์žฅ์„ ์œ„ํ•œ ๋””ํŽœ๋˜์‹œ ์ถ”๊ฐ€
compile 'org.elasticsearch:elasticsearch:7.1.1'
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.1.1'

 

1. ์Šค๋ƒ…์ƒท ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ๋“ฑ๋ก

a. Kibana์—์„œ ์ง์ ‘ ๋“ฑ๋ก

PUT _snapshot/my-snapshot-repo
{
  "type": "s3",
  "settings": {
    "bucket": "๋ฒ„ํ‚ท๋ช…, ์˜ˆ์ œ์—์„œ๋Š” my-bucket ์‚ฌ์šฉ",
    "base_path": "/๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š” ํด๋”๊ฒฝ๋กœ, ๋ฒ„ํ‚ท ๋‚ด ํด๋” ๊ฒฝ๋กœ์ž„, ์˜ˆ์ œ์—์„œ๋Š” base/path/to/save/backup/data ์‚ฌ์šฉ",
    "readonly" : "true",
    "region": "us-west-1",
    "role_arn": "arn:aws:iam::4234:my-role",
    "compress" : "true"
  }
}

๋งŒ์•ฝ ์œ„ ๋ช…๋ น์–ด ์‹คํ–‰์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด role ์„ค์ •์— ์ถ”๊ฐ€ํ•ด์•ผํ•  ์‚ฌํ•ญ์ด ์žˆ๋Š”๋ฐ, ํ•ด๋‹น ํฌ์ŠคํŠธ ์ตœํ•˜๋‹จ์˜ ์ฐธ๊ณ ๋ฌธํ—Œ๋“ค์„ ์ฝ์–ด๋ณด๋ฉฐ ํ•ด๊ฒฐํ•ด๋ณด์‹œ๊ธธ ๋ฐ”๋ž€๋‹ค. 

{
  "Message": "User: anonymous is not authorized to perform: iam:PassRole on resource: arn:aws:iam::4234:my-role"
}

 

๋‚˜๋Š” ์•„๋ž˜ ์„ค๋ช…ํ•  Java ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋“ฑ๋กํ–ˆ๋‹ค.

 

b. Java ์ฝ”๋“œ (1๊ณผ ๋™์ผํ•œ ๋‚ด์šฉ์„ ์ž๋ฐ”์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•œ ๊ฒƒ์ด๋‹ค)

    import com.amazonaws.auth.AWS4Signer;
    import com.amazonaws.auth.AWSCredentialsProvider;
    import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.HttpRequestInterceptor;
    import org.apache.http.entity.ContentType;
    import org.apache.http.nio.entity.NStringEntity;
    import org.elasticsearch.action.get.GetRequest;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.client.*;
    import org.junit.Ignore;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ActiveProfiles;
    import org.springframework.test.context.junit4.SpringRunner;

    import javax.ws.rs.HttpMethod;
    import java.io.IOException;

    @Slf4j
    @ActiveProfiles("${SPRING_PROFILES_ACTIVE:local}")
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class AWSESSnapshotTest {

        private String region = "us-west-1";
        private static String serviceName = "es";
        private static String aesEndpoint = "VPC Endpoint๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”";
        private static String snapshotRepository = "/_snapshot/๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ช…์„ ์จ์ฃผ์„ธ์š”";
        private static String snapshotSettings = "{ \"type\": \"s3\", \"settings\": { \"bucket\": \"๋ฒ„ํ‚ท๋ช…์„ ์จ์ฃผ์„ธ์š”\", \"region\": \"๋ฆฌ์ „์„ ๋ช…์‹œํ•ด์ฃผ์„ธ์š”, ์˜ˆ: us-west-1\", \"base_path\": \"์Šค๋ƒ…์ƒท์„ ์ €์žฅํ•  ๋ฒ„ํ‚ท๋‚ด ํด๋” ๊ฒฝ๋กœ\", "compress":"true", "readonly":"true", \"role_arn\": \"IAM Role์„ ์ ์–ด์ฃผ์„ธ์š”\" } }";

        private static final AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();

        @Test
        public void AWS_ES_์ˆ˜๋™์Šค๋ƒ…์ƒท_๋ฆฌํฌ์ง€ํ† ๋ฆฌ_๋“ฑ๋ก() throws IOException {
            RestClient esClient = esClient(serviceName, region);

            // Register a snapshot repository
            HttpEntity entity = new NStringEntity(snapshotSettings, ContentType.APPLICATION_JSON);
            Request request = new Request(HttpMethod.PUT, snapshotRepository);
            request.setEntity(entity);
            // request.addParameter(name, value); // optional parameters
            Response response = esClient.performRequest(request);
            System.out.println(response.toString());
        }

        // Adds the interceptor to the ES REST client
        public static RestHighLevelClient esClient2(String serviceName, String region) {
            AWS4Signer signer = new AWS4Signer();
            signer.setServiceName(serviceName);
            signer.setRegionName(region);

            // java.lang.NoClassDefFoundError: org/elasticsearch/common/xcontent/DeprecationHandler
            HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor(serviceName, signer, credentialsProvider);

            return new RestHighLevelClient(RestClient.builder(HttpHost.create(aesEndpoint)).setHttpClientConfigCallback(hacb -> hacb.addInterceptorLast(interceptor)));
        }

    }

 

์ž๋ฐ”์ฝ”๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉด ES์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ์„ค์ •์ด ์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

GET _snapshot

"my-repo" : {
  "type" : "s3",
  "settings" : {
    "bucket" : "my-bucket",
    "base_path" : "base/path/to/save/backup/data",
    "readonly" : "false",
    "region" : "us-west-1",
    "role_arn" : "arn:aws:iam::4234:my-role",
    "compress" : "true"
  }
}

 

์ด๋ ‡๊ฒŒ ์ž˜ ๋‚˜์™”๋‹ค๋ฉด ์ด์ œ ์ธ๋ฑ์Šค ๋ฐฑ์—…์œผ๋กœ ๋„˜์–ด๊ฐ€์ž.

 

2. ES ์ธ๋ฑ์Šค๋ฅผ S3๋กœ ๋ฐฑ์—…

ES ์ธ๋ฑ์Šค๋ฅผ S3๋กœ ๋ฐฑ์—…ํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

    public void AWS_ES_์ˆ˜๋™์Šค๋ƒ…์ƒท_S3_์ €์žฅ() throws IOException {

        RestClient esClient = esClient(serviceName, region);

        // Save indexes into S3 repository
        String takeSnapShot = "{\n  \"indices\": \"index-2020-04-*\",\n  \"ignore_unavailable\": true,\n  \"include_global_state\": false\n}";
        HttpEntity entity = new NStringEntity(takeSnapShot, ContentType.APPLICATION_JSON);
        Request request = new Request(HttpMethod.PUT, snapshotRepository + "/snapshot-test-2020-04");
        request.setEntity(entity);
        
        Response response = esClient.performRequest(request);
        
        System.out.println(response.toString());
    }
    
    
    ์ถœ๋ ฅ๊ฒฐ๊ณผ : 
    Response{requestLine=PUT /_snapshot/my-repo/snapshot-test-2020-04 HTTP/1.1, host=${VPC Endpoint}, response=HTTP/1.1 200 OK}

 

์œ„ ์ฝ”๋“œ๋Š” ์ธ๋ฑ์Šค๋ช…์ด "index-2020-04-"๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์ธ๋ฑ์Šค๋ฅผ "snapshot-test-2020-04"๋ผ๋Š” ์ด๋ฆ„์˜ ์Šค๋ƒ…์ƒท์œผ๋กœ S3์— ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์‹ค์ œ๋กœ S3์˜ ํ•ด๋‹น ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋กœ ๊ฐ€์„œ ํ™•์ธํ•ด๋ณด๋ฉด indices ๋””ํ…ํ† ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋˜์–ด์žˆ๊ณ , ๊ทธ ์•ˆ์—๋Š” ๋ฐฑ์—…ํ•˜๋ คํ–ˆ๋˜ ์ธ๋ฑ์Šค์˜ ๊ฐœ์ˆ˜๋งŒํผ uuid๊ฐ™์€ ์ด๋ฆ„์˜ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ๊ณ  ๊ทธ ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์—๋Š” ๋˜ uuid๊ฐ™์€ ์ด๋ฆ„์˜ ํŒŒ์ผ๋“ค์ด ์ž”๋œฉ ์ƒ์„ฑ๋˜์–ด์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ S3 ํŒŒ์ผ๋ช…๋งŒ ๋ณด๊ณ ์„œ๋Š” ์ด๊ฒŒ ์–ด๋Š ์Šค๋ƒ…์ƒท์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐํŒŒ์ผ์ธ์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค ์•„๋†” ใ…œใ…œ ์ถ”ํ›„ S3์—์„œ ํ•ด๋‹น ์Šค๋ƒ…์ƒท์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‹ถ์–ด๋„ ์—ฌ๊ธฐ์„œ(S3์—์„œ) ํŒŒ์ผ๋ช…๋งŒ ๋ณด๊ณ ๋Š” ์ฒ˜๋ฆฌํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค๋Š” ๋œป์ด๋‹ค. 

 

์šฐ์„  ๋ฐฑ์—…๋œ ์ˆ˜๋™ ์Šค๋ƒ…์ƒท์„ ๋‹ค์‹œ ES๋กœ ๋ณต์›ํ•ด์„œ ์ž˜ ์กฐํšŒํ•ด์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€๋ถ€ํ„ฐ ํ™•์ธํ•ด๋ณด์ž.

 

3. S3์— ๋ฐฑ์—…๋œ ์Šค๋ƒ…์ƒท์„ ES๋กœ ๋ณต์›ํ•˜๊ธฐ

๋ณต์›์‹œ์—๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

    public void AWS_ES_์ˆ˜๋™์Šค๋ƒ…์ƒท_S3_๋ณต์›_ํ…Œ์ŠคํŠธ() throws IOException {

        RestClient esClient = esClient(serviceName, region);

        // Restoring snapshot as ES indices
        String takeSnapShot = "{\n  \"indices\": \"index-2020-04-28\",\n  \"ignore_unavailable\": true,\n  \"include_global_state\": false\n}";
        HttpEntity entity = new NStringEntity(takeSnapShot, ContentType.APPLICATION_JSON);
        Request request = new Request(HttpMethod.POST, snapshotRepository + "/snapshot-test-2020-04/_restore");
        request.setEntity(entity);
        
        Response response = esClient.performRequest(request);
        
        System.out.println(response.toString());
    }
    
    ์ถœ๋ ฅ๊ฒฐ๊ณผ:
    Response{requestLine=POST /_snapshot/my-repo/snapshot-test-2020-04/_restore HTTP/1.1, host=${VPC Endpoint}, response=HTTP/1.1 200 OK}

์œ„ ์ฝ”๋“œ๋Š” 4์›”๋‹ฌ ๋กœ๊ทธ์— ๋Œ€ํ•ด์„œ ์ €์žฅํ–ˆ๋˜ ์Šค๋ƒ…์ƒท์—์„œ ํŠน์ •์ผ์ž์˜ ์ธ๋ฑ์Šค ์ฆ‰, index-2020-04-28 ์— ๋Œ€ํ•ด์„œ๋งŒ ๋ณต์›์„ ์ง„ํ–‰ํ•œ๋‹ค.

์œ„ ์ฝ”๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉด ES์— ํ•ด๋‹น ์ธ๋ฑ์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ ๋ณต์›ํ•˜๋ ค๋Š” ์ธ๋ฑ์Šค ๋ช…์ด ์กด์žฌํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

org.elasticsearch.client.ResponseException: method [POST], host [https://vpc-endpoint.amazonaws.com], URI [/_snapshot/my-repo/snapshot-test-2020-04/_restore], status line [HTTP/1.1 500 Server Error]
{"error":{"root_cause":[{"type":"snapshot_restore_exception","reason":"[my-repo:snapshot-test-2020-04/ECzLylZnTsGn8KfBqCvSEw] cannot restore index [index-2020-04-27] because an open index with same name already exists in the cluster. Either close or delete the existing index or restore the index under a different name by providing a rename pattern and replacement name"}],"type":"snapshot_restore_exception","reason":"[my-repo:snapshot-test-2020-04/ECzLylZnTsGn8KfBqCvSEw] cannot restore index [index-2020-04-27] because an open index with same name already exists in the cluster. Either close or delete the existing index or restore the index under a different name by providing a rename pattern and replacement name"},"status":500}

 

์ด๋Ÿด ๊ฒฝ์šฐ ์•„๋ž˜ 3๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋ณต์›์ ˆ์ฐจ๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

 

์ฒซ์งธ, ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ๋™์ผํ•œ ์ด๋ฆ„์˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ญ์ œํ•˜๊ณ  ๋ณต์›ํ•œ๋‹ค.

๋‘˜์งธ, ๋ณต์›ํ•  ๋•Œ ์ธ๋ฑ์Šค๋ช…์„ renameํ•ด์„œ ๋ณต์›ํ•œ๋‹ค.

์…‹์งธ, ๋‹ค๋ฅธ ES๋„๋ฉ”์ธ์œผ๋กœ ๋ณต์›ํ•œ๋‹ค -> ์ด๊ฑด ์ˆ˜๋™์œผ๋กœ ์Šค๋ƒ…์ƒท์„ ์ƒ์„ฑํ•œ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€๋Šฅํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.

 

๋‚˜๋Š” ํ…Œ์ŠคํŠธ ์ค‘์ด๋ฏ€๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ฒซ๋ฒˆ์งธ๋ฅผ ์„ ํƒํ–ˆ๋‹ค.

 

๋งŒ์•ฝ ์Šค๋ƒ…์ƒท์— ์—ฌ๋Ÿฌ ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๊ณ  ๋ชจ๋“  ์ •๋ณด๋ฅผ ๋ณต์›ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋‹จ ํ•˜๋‚˜์˜ ์ธ๋ฑ์Šค๋ผ๋„ ์ด๋ฆ„์ด ๊ฒน์นœ๋‹ค๋ฉด ์œ„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

์œ„ ์ฝ”๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉด ES์—์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์ •๋ณด๋ฅผ ์กฐํšŒํ•ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ํŠน์ • ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹Œ ์—ฌ๋Ÿฌ ์ธ๋ฑ์Šค๋ฅผ ํ•œ๊บผ๋ฒˆ์— ๋ณต์›ํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” takeSnapShot์˜ indices ํ•ญ๋ชฉ์— ์ธ๋ฑ์Šค๋ช…์„ ๋‚˜์—ดํ•ด์ฃผ๋ฉด ๋œ๋‹ค. *๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑด ES ์œ ์ €๋ผ๋ฉด ๋‹น์—ฐํžˆ ์•„์‹คํ…Œ๊ณ ..

"indices":"index-2020-04-*,index-2020-05-0*"

์œ„์ฒ˜๋Ÿผ ํ•ด์ฃผ๋ฉด 4์›”๋‹ฌ ๋ชจ๋“  ์ธ๋ฑ์Šค์™€ 5์›” 1์ผ~5์›” 9์ผ๊นŒ์ง€์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ณต์›ํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.

 

์ž, ๊ทธ๋Ÿผ ๋ณต์›์— ๋Œ€ํ•ด์„œ๋Š” ์ด์ •๋„๋กœ ํ•˜๊ณ , ์Šค๋ƒ…์ƒท๋„ ๋„ˆ๋ฌด ์˜ค๋ž˜ ๋ณด๊ด€ํ•˜๋ฉด ์šฉ๋Ÿ‰๋งŒ ์žก์•„๋จน์„ํ…Œ๋‹ˆ ์‚ญ์ œ๋Š” ์–ด๋–ป๊ฒŒ ํ•  ์ˆ˜ ์žˆ์„์ง€ ๋ณด์ž.

 

4. S3์— ๋ฐฑ์—…ํ•œ ES ์Šค๋ƒ…์ƒท ์‚ญ์ œํ•˜๊ธฐ

์Šค๋ƒ…์ƒท์„ ์‚ญ์ œํ•  ๋•Œ๋Š” ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด๋œ๋‹ค.

    public void AWS_ES_์ˆ˜๋™์Šค๋ƒ…์ƒท_์‚ญ์ œ_ํ…Œ์ŠคํŠธ() throws IOException {

        RestClient esClient = esClient(serviceName, region);

        Request request = new Request(HttpMethod.DELETE, snapshotRepository + "/snapshot-test-2020-04");

        Response response = esClient.performRequest(request);

        System.out.println(response.toString());
    }

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด S3์— ์ƒ์„ฑ๋˜์—ˆ๋˜ ํŒŒ์ผ๋“ค์ด ์‚ญ์ œ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, indices ๋””๋ ‰ํ† ๋ฆฌ๋Š” ์™„์ „ํžˆ ์‚ญ์ œ๋˜๊ณ  ์Šค๋ƒ…์ƒท ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋กœ ์ง€์ •๋œ root ๊ฒฝ๋กœ์—๋Š” ๋ช‡๋ช‡ ์“ฐ๋ ˆ๊ธฐ(??) ํŒŒ์ผ๋“ค์ด ๋‚จ์•„์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋งŒ์•ฝ 2๊ฐœ ์ด์ƒ์˜ ์Šค๋ƒ…์ƒท์„ ์ €์žฅํ–ˆ๋‹ค๋ฉด indices ๋””๋ ‰ํ† ๋ฆฌ๋Š” ๋ชจ๋“  ์Šค๋ƒ…์ƒท์— ๋Œ€ํ•œ ์‚ญ์ œ์š”์ฒญ์„ ํ•˜์ง€ ์•Š๋Š” ์ด์ƒ ์‚ญ์ œ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋˜ํ•œ, ์Šค๋ƒ…์ƒท์„ ์‚ญ์ œํ•  ๋•Œ๋Š” ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ฆฐ๋‹ค. sync ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ์—๋Š” ๋„ˆ๋ฌด๋„ˆ๋ฌด๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์šฉ์—์„œ ์‚ญ์ œ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด async ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธธ ๊ถŒ์žฅํ•œ๋‹ค. ์œ„ ์ฝ”๋“œ๋Œ€๋กœ ์ž‘์„ฑํ–ˆ๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด ์•„๋ž˜ ์†Œ์ผ“ํƒ€์ž„์•„์›ƒ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค.

java.net.SocketTimeoutException: 30,000 milliseconds timeout on connection http-outgoing-0 [ACTIVE]

 

๋งŒ์•ฝ ์Šค๋ƒ…์ƒท ์ด๋ฆ„์ด ๊ธฐ์–ต์ด ๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? S3์—์„œ ์กฐํšŒํ•ด๋ด์•ผ ํŒŒ์ผ๋ช…์ด ์–ด๋–ค ์Šค๋ƒ…์ƒท์ธ์ง€ ์•Œ ์ˆ˜ ์—†๋„๋ก ์ƒ์„ฑ๋˜์–ด์žˆ์œผ๋‹ˆ ์•Œ ๊ธธ์ด ์—†๋‹ค. ํ‚ค๋ฐ”๋‚˜ dev tool์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ด๋–ค ์Šค๋ƒ…์ƒท์ด ์žˆ๋Š”์ง€ ๊ทธ ์Šค๋ƒ…์ƒท์— ์–ด๋–ค ์ธ๋ฑ์Šค๋“ค์ด ์ €์žฅ๋˜์–ด์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

GET _snapshot/my-repo/_all 	<- my-repo์˜ ๋ชจ๋“  ์Šค๋ƒ…์ƒท ์กฐํšŒํ•˜๊ธฐ



{
  "snapshots" : [ {
    "snapshot" : "snapshot-test-2020-04",
    "uuid" : "VPMGlnLTQlqIT7SxPaqCOg",
    "version_id" : 7016199,
    "version" : "7.1.1",
    "indices" : [ "index-2020-04-29", "index-2020-04-26", "index-2020-04-27", "index-2020-04-28" ],
    "include_global_state" : false,
    "state" : "SUCCESS",
    "start_time" : "2020-05-25T07:34:09.804Z",
    "start_time_in_millis" : 1590392049804,
    "end_time" : "2020-05-25T07:34:32.145Z",
    "end_time_in_millis" : 1590392072145,
    "duration_in_millis" : 22341,
    "failures" : [ ],
    "shards" : {
      "total" : 20,
      "failed" : 0,
      "successful" : 20
    }
  } ]
}

 

ํŠน์ • ์Šค๋ƒ…์ƒท์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

GET _snapshot/my-repo/snapshot-test-*

 

 

์ฐธ๊ณ ๋กœ ํ๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ES์˜ ๋ฐ์ดํ„ฐ๋ฅผ rotate ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋งํฌ๋Š” ์ฐธ๊ณ ๋ฌธ์„œ์— ๋„ฃ์–ด๋†“์•˜์œผ๋‹ˆ ๊ด€์‹ฌ์žˆ์œผ์‹œ๋ฉด ์ฝ์–ด๋ณด์„ธ์š”.

 

๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ๋กœ๋Š” ์ด ์Šค๋ƒ…์ƒท ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ES 7 ์ด์ƒ์—์„œ ์ง€์›ํ•˜๋Š” UltraWarm ์„ค์ •์„ ์ด์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ES์ธ์Šคํ„ด์Šค ์‚ฌ์ด์ฆˆ๋„ warm ๋…ธ๋“œ์˜ ๊ฒฝ์šฐ ํ•ซ๋…ธ๋“œ์™€๋Š” ๋ณ„๊ฐœ๋กœ S3๋ฅผ ์ด์šฉํ•จ์œผ๋กœ์จ ๋น„์šฉ์„ ๋งŽ์ด ์ค„์ผ ์ˆ˜ ์žˆ๊ณ  ์ €์žฅ๊ธฐ๊ฐ„ ์—ญ์‹œ ์›ํ•˜๋Š” ๋งŒํผ ๋Š˜๋ฆด ์ˆ˜ ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ ์ƒํ™ฉ์—์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์„ ์ด์—ˆ์Šต๋‹ˆ๋‹ค. UltraWarm์ด ๋ญ”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ์ฐธ๊ณ ๋ฌธ์„œ์˜ ๋งํฌ๋ฅผ ํ™•์ธํ•ด๋ณด์„ธ์š”. ์ฐธ๊ณ ๋กœ ES ๋ฒ„์ „ 6.8์ด์ƒ์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

UltraWarm ์œผ๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•  ๋•Œ๋Š” ์•„๋ž˜ ๋ช…๋ น์–ด๋“ค์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

// ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ž‘์—… ์š”์ฒญ
POST _ultrawarm/migration/index-2020-02-09/_warm

 

// ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ž‘์—… ์ค‘์ธ ์ธ๋ฑ์Šค์˜ ์ƒํƒœ ํ™•์ธ
GET _ultrawarm/migration/index-2020-02-09/_status

-> ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ž‘์—…์ค‘์ด ์•„๋‹Œ๊ฒฝ์šฐ ์ฆ‰, ๋๋‚ฌ๊ฑฐ๋‚˜ ์‹œ์ž‘๋„ ์•ˆํ•œ ์ƒํƒœ๋ฉด ์˜ค๋ฅ˜๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 

// ์ธ๋ฑ์Šค ํŒจํ„ด ๋ชฉ๋ก ์กฐํšŒ
GET _cat/indices/index-2020-02-0*?v&s=index


-> ์ด ๋ช…๋ น์€ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ž‘์—… ์ง„ํ–‰ ์ค‘์— ์ธ๋ฑ์Šค์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ž‘์—…์ด ์‹œ์ž‘๋˜๋ฉด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๋ ค๋Š” ์ธ๋ฑ์Šค์˜ ์›๋ž˜ ์‚ฌ์ด์ฆˆ๋ณด๋‹ค 2๋ฐฐ๊นŒ์ง€ hot data node์˜ ์šฉ๋Ÿ‰์„ ์ฐจ์ง€ํ•˜๊ฒŒ๋œ๋‹ค. ์—ฌ์œ ๋ถ„์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์—†๋‹ค๋ฉด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ž‘์—… ์ž์ฒด๊ฐ€ ์‹œ์ž‘๋˜์ง€ ์•Š๋Š”๋‹ค. (free size๋ฅผ ๊ฒ€์‚ฌํ•œ ๋’ค ์‹œ์ž‘ํ•˜๋„๋ก ๋˜์–ด์žˆ๋‹ค)

 

// ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์ด ์™„๋ฃŒ๋œ ์ธ๋ฑ์Šค ์ƒํƒœ ์กฐํšŒ
GET index-2020-02-09/_settings


-> Ultrawarm ๋…ธ๋“œ๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ์œ„ ๋ช…๋ น์–ด๋กœ ์กฐํšŒ ์‹œ box_type์ด warm ์œผ๋กœ ๋ฐ”๋€Œ์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์ฐธ๊ณ ๋ฌธ์„œ

https://www.elastic.co/guide/en/elasticsearch/plugins/current/repository-s3-repository.html

https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-managedomains-snapshots.html#es-managedomains-snapshot-prerequisites

https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-request-signing.html#es-request-signing-java

https://aws.amazon.com/ko/blogs/database/use-amazon-s3-to-store-a-single-amazon-elasticsearch-service-index/

https://www.elastic.co/guide/en/elasticsearch/reference/7.1/modules-snapshots.html

AWS ํ๋ ˆ์ดํ„ฐ ์ด์šฉํ•˜๊ธฐ

AWS ES Ultrawarm

 

 

์˜ˆ์ „์— ์ทจ๋ฏธ๋กœ ์•ˆ๋“œ๋กœ์ด๋“œ ๊ณต๋ถ€ํ•ด์„œ ์ž๋ฐ”๋กœ ์–ดํ”Œํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์˜ฌ๋ฆฐ ์ ์ด ์žˆ์—ˆ๋‹ค.

๋„ˆ๋ฌด ์—…๋ฐ์ดํŠธ๋ฅผ ์•ˆํ•ด์„œ ๊ตฌ๊ธ€์ •์ฑ…์— ์œ„๋ฐฐ๋˜์–ด ํ•ด๋‹น ์–ดํ”Œ์ด ๋‚ด๋ ค๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ ๊ทธ๊ฒƒ๋„ ๋ชจ๋ฅด๊ณ  ๋†”๋‘๊ณ  ์žˆ์—ˆ๋‹ค.

์ตœ๊ทผ์— ์ˆ˜์ •ํ•ด์„œ ๋‹ค์‹œ ์˜ฌ๋ฆฌ๋Š” ์ž‘์—…์„ ํ–ˆ๋Š”๋ฐ, ์ถ”๊ฐ€๋กœ ๊ฐœ๋ฐœํ•˜๋Š” 1์ธ ํ”„๋กœ์ ํŠธ๋“ค์€ ์ฝ”ํ‹€๋ฆฐ์œผ๋กœ ํ•ด๋ณด๊ณ  ์‹ถ์€ ๋งˆ์Œ์ด ์ƒ๊ฒผ๋‹ค.

์•„์ง ์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์˜ ๋ฉ”์ธ์–ธ์–ด๋Š” ์ž๋ฐ”์ธ ๊ฒƒ ๊ฐ™๊ธด ํ•œ๋ฐ, ์ฝ”ํ‹€๋ฆฐ์€ IntelliJ IDE๋ฅผ ๊ฐœ๋ฐœํ•œ JetBrain์‚ฌ์—์„œ ์ž๋ฐ”์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐœ๋ฐœํ•œ ์–ธ์–ด๋กœ ์ž๋ฐ”์ฒ˜๋Ÿผ JVM์œ„์—์„œ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๊ณ , ์•ˆ๋“œ๋กœ์ด๋“œ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€๋„ ์‰ฝ๊ฒŒ ์—ฐ๋™ํ•  ์ˆ˜ ์žˆ๊ณ  ๋„ค์ดํ‹ฐ๋ธŒ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋„๋ก ๊ฐœ๋ฐœ๋˜์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. (๋” ์ž์„ธํ•œ ์ •๋ณด๋Š” ์ฝ”ํ‹€๋ฆฐ ๊ณต์‹์‚ฌ์ดํŠธ FAQ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.)

 

๋‚˜๋Š” ์šฐ์„  ์ž๋ฐ” ๊ฐœ๋ฐœ์ž์ด๊ธฐ ๋•Œ๋ฌธ์— Kotlin/JVM ์œผ๋กœ ์ฝ”ํ‹€๋ฆฐ์„ ์—ฐ์Šตํ•ด๋ณด๋ ค ํ•œ๋‹ค. (Kotlin/JS๋ฅผ ์ด์šฉํ•˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๊ฐœ๋ฐœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค)

์ฝ”ํ‹€๋ฆฐ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ Hello World ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋Š” ์ฝ”ํ‹€๋ฆฐ ๊ณต์‹์‚ฌ์ดํŠธ์—์„œ ํ™•์ธํ•˜๊ธฐ ๋ฐ”๋ž€๋‹ค.

 

๊ทธ ์ฒซ ๋ฒˆ์งธ ์‹œ๊ฐ„์œผ๋กœ ์ฝ”ํ‹€๋ฆฐ ํŒŒ์ผ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ๋ณ€์ˆ˜ ์ •์˜๋Š” ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€ ์ž๋ฐ”์™€ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ์ง€ ์‚ดํŽด๋ณด์•˜๋‹ค.

 

1. ์ฝ”ํ‹€๋ฆฐ ํŒŒ์ผ ๊ตฌ์กฐ

์ฝ”ํ‹€๋ฆฐ ํ—ฌ๋กœ์›”๋“œ

- ์ฝ”ํ‹€๋ฆฐ ํŒŒ์ผ์€ .kt ํ™•์žฅ์ž๋ฅผ ๊ฐ–๋Š”๋‹ค.

- ์ž๋ฐ”๋Š” class ๋ธ”๋Ÿญ ๋‚ด์— main๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ์ฝ”ํ‹€๋ฆฐ์˜ ๋ฉ”์ธํ•จ์ˆ˜๋Š” ํด๋ž˜์Šค ๋ธ”๋Ÿญ๋‚ด์— ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

- package ์„ ์–ธ๋ถ€ ๋ฐ import ๋ฌธ์€ ์ž๋ฐ”์™€ ๋™์ผํ•˜๋‹ค.

- ์ฝ”ํ‹€๋ฆฐ์—์„œ ํ•จ์ˆ˜๋Š” function์˜ ์ถ•์•ฝ์–ด์ธ fun ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋ฆฌํ„ดํƒ€์ž…์€ ํ•จ์ˆ˜๋ช…() ๋’ค์ชฝ์— ์œ„์น˜ํ•œ๋‹ค.

- ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” statement๋ฅผ ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ์ข…๋ฃŒํ•˜์ง€ ์•Š๋Š”๋‹ค.

2. ๋ณ€์ˆ˜์˜ ํƒ€์ž…, ์ •์˜ ๋ฐ ์ดˆ๊ธฐํ™”

์ฝ”ํ‹€๋ฆฐ์—์„œ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์€ ์ž๋ฐ”์˜ ๊ทธ๊ฒƒ๊ณผ ๋™์ผํ•˜์ง€๋งŒ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค.

๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๊ด€๊ณ„์—†์ด ์ƒ์ˆ˜๋Š” val, ๋ณ€์ˆ˜๋Š” var๋กœ ์„ ์–ธํ•œ๋‹ค.

๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ฃผ์ง€ ์•Š์•„๋„ ํ• ๋‹นํ•ด์ฃผ๋Š” ๊ฐ’์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.

์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” val์„ read-only variable๋กœ ์„ค๋ช…์„ ํ•œ๋‹ค.

์ผ๋ฐ˜์ ์ธ ๋ณ€์ˆ˜๋Š” var๋ฅผ ์ด์šฉํ•˜๊ณ  read-only ๋ณ€์ˆ˜๋Š” val์„ ์‚ฌ์šฉํ•œ๋‹ค

val์€ ์ž๋ฐ”์˜ final๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๊ฐ’์„ ์žฌํ• ๋‹น ํ•˜๋ คํ•  ๊ฒฝ์šฐ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

val ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์— ๋‹ค๋ฅธ ๊ฐ’์„ ์žฌํ• ๋‹น ํ•˜๋ คํ•  ๊ฒฝ์šฐ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

var ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜์˜ ๊ฒฝ์šฐ ํƒ€์ž…์ด ํ•œ๋ฒˆ ์ •ํ•ด์ง€๋ฉด ๋‹ค๋ฅธ ํƒ€์ž…์„ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.

ํƒ€์ž…์ด ์ •ํ•ด์ง„(Int) var ํ‚ค์›Œ๋“œ๋กœ ์ •์˜ํ•œ ๋ณ€์ˆ˜์— ๋‹ค๋ฅธ ํƒ€์ž…(String)์˜ ๊ฐ’ ์žฌํ• ๋‹น ์‹œ๋„ ์‹œ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

2-1. ๋ณ€์ˆ˜ ์ •์˜

์ฝ”ํ‹€๋ฆฐ์—์„œ ๋ณ€์ˆ˜์˜ ์ •์˜(define)์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•œ๋‹ค. ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ์ •์˜๋งŒ ํ•  ๊ฒฝ์šฐ ํƒ€์ž…์„ ํ•„์ˆ˜๋กœ ๋ช…์‹œํ•ด์ค˜์•ผ ํ•œ๋‹ค.

    fun main() {
        var i:Int
        var s:String
        var l:Long
        var f:Float
        var d:Double
    }

 

2-2. ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐํ™”๋Š” ๊ฐ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์— ๋งž๋Š” ๊ฐ’์„ ํ• ๋‹นํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

    fun main() {
        var i:Int
        var s:String
        var l:Long
        var f:Float
        var d:Double

        i = 10
        s = "string"
        l = 10L
        f = 10.2F
        d = 10.5

        var v = "string variable"	// String ํƒ€์ž… ๋ณ€์ˆ˜
        var n = 123		// Int ํƒ€์ž… ๋ณ€์ˆ˜
    }

 

 

3. ํ•จ์ˆ˜ ์ •์˜

๋‘ ๊ฐœ์˜ Intํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ Int ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

    fun sum(a: Int, b: Int): Int { return a + b}

 

์ง์ ‘ ๊ฐ’์„ ํ• ๋‹นํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ (์œ„ ํ•จ์ˆ˜์™€ ๋™์ผํ•œ ํ•จ์ˆ˜์ด๋‹ค)

    fun sum(a: Int, b: Int) = a + b

 

๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ํ•จ์ˆ˜๋Š” Unit (์ž๋ฐ”์˜ void) ์œผ๋กœ ์ •์˜

    fun printSum(a: Int, b: Int): Unit { println("sum of $a and $b is ${a + b}")}

 

Unit ๋ฆฌํ„ดํƒ€์ž…์€ ์•ˆ์จ์ค˜๋„ ๋œ๋‹ค.

    fun printSum(a: Int, b: Int) { println("sum of $a and $b is ${a + b}")}

 

4. ์ŠคํŠธ๋ง ํ…œํ”Œ๋ฆฟ

    var a = 1
    // simple name in template:
    val s1 = "a is $a" 

    a = 2
    // arbitrary expression in template:
    val s2 = "${s1.replace("is", "was")}, but now is $a"

์ž๋ฐ”์—์„œ String ํด๋ž˜์Šค๊ฐ€ ๊ฐ–๊ณ ์žˆ๋Š” format ๋ฉ”์„œ๋“œ์™€ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ String์˜ ๋‚ด์šฉ์ด ๊ฐ€๋ณ€์ ์ผ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

5. ์กฐ๊ฑด๋ฌธ

์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉ๋ฒ•์€ ์ž๋ฐ”์™€ ๋‹ค๋ฅผ๊ฒŒ ์—†๋‹ค.

    fun maxOf(a: Int, b: Int): Int {
        if (a > b) {
            return a
        } else {
            return b
        }
    }

ํ•˜์ง€๋งŒ ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” if๋ฌธ์„ ํ•˜๋‚˜์˜ ํ‘œํ˜„์‹์œผ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค.

    fun maxOf(a: Int, b: Int) = if (a > b) a else b

if๋ฌธ์˜ ๊ฒฐ๊ณผ๊ฐ’์ด maxOfํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’์ด๋ผ๊ณ  ์ •์˜ํ•œ ๊ฒƒ์œผ๋กœ, ๋ฐ”๋กœ ์œ„์— maxOfํ•จ์ˆ˜์™€ ๋™์ผํ•œ ํ•จ์ˆ˜๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

6. Null

์ฝ”ํ‹€๋ฆฐ์—์„œ ๋ฐ˜ํ™˜๊ฐ’์ด nullable์ธ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

    fun parseInt(str: String): Int? {
        // ๋ฆฌํ„ดํƒ€์ž…์ด ๊ธฐ๋ณธ์ ์œผ๋กœ Int์ด๋‚˜ null์ผ ์ˆ˜๋„ ์žˆ๋Š” ํ•จ์ˆ˜
    }

๊ทธ๋ฆฌ๊ณ  ์œ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ณด๋ฉด.

    fun printProduct(arg1: String, arg2: String) {
        val x = parseInt(arg1)
        val y = parseInt(arg2)

        // ์—ฌ๊ธฐ์„œ `x * y` ์—ฐ์‚ฐ์„ ๋ฐ”๋กœ ํ•ด๋ฒ„๋ฆฌ๋ฉด x๋˜๋Š” y๊ฐ€ null์ผ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
        // ๋”ฐ๋ผ์„œ null ์ฒดํฌ๋ฅผ ๋จผ์ € ํ•ด์•ผํ•œ๋‹ค.
        if (x != null && y != null) {
            // null ์ฒดํฌ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด x ์™€ y ๋Š” ์ž๋™์œผ๋กœ non-nullable๋กœ ์บ์ŠคํŒ… ๋œ๋‹ค 
            // (์ฝ”ํ‹€๋ฆฐ ๊ณต์‹ ๋ฌธ์„œ์— ์ด๋ ‡๊ฒŒ ๋‚˜์™€์žˆ๋Š”๋ฐ non-nullable๋กœ ์บ์ŠคํŒ… ๋œ๋‹ค๋ผ๋Š”๊ฒŒ ์•„์ง ์ž˜ ์ดํ•ด๋Š” ์•ˆ๋œ๋‹ค. ๊ทธ๋Ÿฐ ํƒ€์ž…์ด ์žˆ๋‹ค๋Š”๊ฑด์ง€..)
            println(x * y)
        }
        else {
            println("'$arg1' or '$arg2' is not a number")
        }    
    }

์ด๋ ‡๊ฒŒ null ์ฒดํฌ๋ฅผ ํ•˜๊ณ ๋‚˜๋ฉด non-nullable๋กœ ์บ์ŠคํŒ…์ด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋Š” ์ผ๋ฐ˜ ํƒ€์ž…์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.

 

7. Type Check and Automatic Casts

    fun getStringLength(obj: Any): Int? {
        if (obj is String) {
            // ์ด ๋ธ”๋Ÿญ ๋‚ด์—์„œ `obj`๋Š” ์ž๋™์œผ๋กœ String ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋˜๋ฉฐ String์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ
            return obj.length
        }

        // `obj`๋Š” ํƒ€์ž… ์ฒดํฌ ๋ธ”๋Ÿญ ๋ฐ–์—์„œ๋Š” ์—ฌ์ „ํžˆ ์–ด๋–ค ํƒ€์ž…๋„ ๋  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค
        return null
    }

ํƒ€์ž… ์ฒดํฌ ์‹œ ๋ฐ˜๋Œ€๋กœ(not์„ ์ด์šฉ) ํ•  ๊ฒฝ์šฐ 

    fun getStringLength(obj: Any): Int? {
        if (obj !is String) return null

        // ์œ„ if๋ฌธ์—์„œ `obj`๊ฐ€ String์ด ์•„๋‹ˆ๋ฉด null์„ ๋ฆฌํ„ดํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๊ธฐ๋กœ ๋‚ด๋ ค์™”๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด String์ผ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.
        // ๋”ฐ๋ผ์„œ ์ž๋™์œผ๋กœ String ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋˜๋ฉฐ, String์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
        return obj.length
    }

๋˜๋Š” ์ด๋ ‡๊ฒŒ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

    fun getStringLength(obj: Any): Int? {
        // `&&`์—ฐ์‚ฐ์˜ ์˜ค๋ฅธ์ชฝ์—์„œ๋Š” obj๊ฐ€ String์ด๋ผ๋Š” ๊ฒƒ์„ ํ™•์ธํ•œ ๋’ค์ด๋ฏ€๋กœ,
        // String ์œผ๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜๋˜์–ด String ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
        if (obj is String && obj.length > 0) {
            return obj.length
        }

        return null
    }

 

8. For-Loop, While-Loop

loop๋Š” ์–ด๋Š ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋‚˜ ๋‹ค ๋น„์Šทํ•˜๋‹ค. ์ฝ”ํ‹€๋ฆฐ์—์„œ๋„ ์ž๋ฐ”์˜ foreach๋ฌธ ์ฒ˜๋Ÿผ for-loop๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    val items = listOf("apple", "banana", "kiwifruit")
    for (item in items) {
        println(item)
    }

์ธ๋ฑ์Šค ์ •๋ณด๋ฅผ ์ด์šฉํ•ด์•ผ ๊ฒฝ์šฐ ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

    val items = listOf("apple", "banana", "kiwifruit")
    for (index in items.indices) {
        println("item at $index is ${items[index]}")
    }

while-loop๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    val items = listOf("apple", "banana", "kiwifruit")
    var index = 0
    while (index < items.size) {
        println("item at $index is ${items[index]}")
        index++
    }

 

9. when ํ‘œํ˜„์‹

when expression์€ ์ž๋ฐ”์˜ switch-case๋ฌธ๊ณผ ์œ ์‚ฌํ•œ๋ฐ, ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ๋งž๊ฒŒ ๋ณ€ํ˜•์‹œ์ผœ๋†“์€ ๊ฒƒ ๊ฐ™๋‹ค

    fun describe(obj: Any): String =
        when (obj) {
            1          -> "One"
            "Hello"    -> "Greeting"
            is Long    -> "Long"
            !is String -> "Not a string"
            else       -> "Unknown"
        }

 

10. Range (๋ฒ”์œ„)

์ž๋ฐ”์—์„œ๋Š” loop๋ฅผ ๋Œ๋ฆด ๋•Œ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค

    for (int i = 0; i < x; i++) {
        // ...
    }

๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์ฝ”ํ‹€๋ฆฐ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค

    for (z in 1..10) {
        println(z)
    }

๊ทธ๋Ÿผ ๋งŒ์•ฝ ๋งค loop์‹œ๋งˆ๋‹ค i๊ฐ’์˜ ์ฆ๋ถ„์„ i++๊ฐ€ ์•„๋‹ˆ๋ผ i=i+2 ์ฒ˜๋Ÿผ ์จ์•ผํ•œ๋‹ค๋ฉด??? ์–ด๋–ป๊ฒŒ ์“ธ ์ˆ˜ ์žˆ์„๊นŒ?

    for (x in 1..10 step 2) {
    	// 1๋ถ€ํ„ฐ 10๊นŒ์ง€ 2์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ์ถœ๋ ฅ
        // ๊ฒฐ๊ณผ: 1~10 ์ค‘์—์„œ ํ™€์ˆ˜ ์ถœ๋ ฅ => 13579
        print(x)
    }
    println()
    for (x in 9 downTo 0 step 3) {
    	// ํฐ ์ˆซ์ž์—์„œ ์ž‘์€ ์ˆซ์ž๋กœ ๋‚ด๋ ค๊ฐ€๋ฉด์„œ loop๋ฅผ ๋„๋Š” ์ผ€์ด์Šค์—์„œ๋Š” downTo ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
        // ๊ฒฐ๊ณผ: 9๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ 3์”ฉ ๋‚ด๋ ค๊ฐ€๋ฉด์„œ x๊ฐ€ 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ ๊นŒ์ง€ ์ˆซ์ž ์ถœ๋ ฅ => 9630
        print(x)
    }

์ด๋ ‡๊ฒŒ ๋’ค์— step ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ผ๋งˆ์”ฉ ์ฆ๊ฐ€์‹œํ‚ฌ ๊ฒƒ์ธ์ง€๋ฅผ ์จ์ฃผ๋ฉด ๋œ๋‹ค.

 

๋˜ํ•œ ์ธ๋ฑ์Šค๊ฐ€ ํŠน์ • ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ๋Š”์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ! ์—ฐ์‚ฐ์ž๋ฅผ in๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค

    val list = listOf("a", "b", "c")

    if (-1 !in 0..list.lastIndex) {
        println("-1 is out of range")
    }
    if (list.size !in list.indices) {
        println("list size is out of valid list indices range, too")
    }

 

 

 

 

referene: ์ฝ”ํ‹€๋ฆฐ ๊ณต์‹๋ฌธ์„œ - ๊ธฐ๋ณธ๋ฌธ๋ฒ•

 

Basic Syntax - Kotlin Programming Language

 

kotlinlang.org

 

 

์ž๋ฐ” ์ŠคํŠธ๋ฆผ์„ ์ด์šฉํ•œ ์œ ์šฉํ•œ ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•

ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์€ java์˜ stream์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๊ฐ€๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌ ํ•ด๋†“์€ ๋ฌธ์„œ๋กœ

์œ ์šฉํ•œ ์ผ€์ด์Šค๋ฅผ ๋ฐœ๊ฒฌํ•  ๋•Œ๋งˆ๋‹ค ๊ณ„์† ์—…๋ฐ์ดํŠธํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

 

 

1. String๋ฐฐ์—ด์„ int๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

int[] intArray = Stream.of(stringArray).mapToInt(Integer::parseInt).toArray();

2. Integer๋ฐฐ์—ด์„ String๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

String[] stringArray = Stream.of(arr).map(String::valueOf).toArray(String[]::new);

3. String๋ฐฐ์—ด์„ String๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

String[] stringArray = new String[]{"!", "@", "#"};
List<String> stringList = Stream.of(stringArray).collect(Collectors.toList());

4. String๋ฆฌ์ŠคํŠธ๋ฅผ String๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

List<String> stringList = new ArrayList<>(Arrays.asList("!","@","#"));
String[] stringArray = stringList.toArray(new String[0]);

5. stream().map()์„ ์ด์šฉํ•œ ๊ฐ์ฒด(object) ๋ณ€ํ™˜ํ•˜๊ธฐ

// ๋ณ€ํ™˜ ์ „ ํด๋ž˜์Šค
@Getter
class Soldier {
  String name;
  int age;

  public Soldier(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

// ๋ณ€ํ™˜ ๋Œ€์ƒ ํด๋ž˜์Šค
@Getter
class King {
  String name;
  int age;
  public King(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

// ์ƒ˜ํ”Œ Soldier stream ์ƒ์„ฑ
Stream<Soldier> s = Stream.of(new Soldier("A", 18), new Soldier("B", 22), new Soldier("C", 19));

// map()์„ ์ด์šฉํ•˜์—ฌ ๋ณ€ํ™˜ํ•˜๊ธฐ
List<King> k = s.map(soldier -> new King(soldier.getName(), soldier.getAge()))
		.collect(Collectors.toList());

 

6. filter()๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•„ํ„ฐ๋งํ•˜๊ธฐ

// ๋ณ€ํ™˜ ์ „ ํด๋ž˜์Šค
@Getter
class Soldier {
  String name;
  int age;

  public Soldier(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

// ๋ณ€ํ™˜ ๋Œ€์ƒ ํด๋ž˜์Šค
@Getter
class King {
  String name;
  int age;
  public King(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

// ์ƒ˜ํ”Œ Soldier stream ์ƒ์„ฑ
Stream<Soldier> s = Stream.of(new Soldier("Arthur", 18), new Soldier("Belisarius", 22), new Soldier("Caesar", 19));

// filter()๋ฅผ ์ด์šฉํ•˜์—ฌ Arthur๋งŒ ๊ณจ๋ผ์„œ map()์„ ์ด์šฉํ•˜์—ฌ King์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
List<King> k = s.filter(soldier -> "Arthur".equals(soldier.getName()))
    .map(soldier -> new King(soldier.getName(), soldier.getAge()))
    .collect(Collectors.toList());

 

7. min() / max() ๋ฅผ ์ด์šฉํ•˜์—ฌ comparator ๊ธฐ์ค€ ์ตœ์†Œ / ์ตœ๋Œ€ ๊ฐ’์„ ๊ฐ–๋Š” ๊ฐ์ฒด ์ถ”์ถœํ•˜๊ธฐ

    @Getter
    @Setter
    class Datapoint {
        private Double maximum;
    }
    
    public void sortStream() {
        List<Datapoint> datapoints = new ArrayList<>();
        int max = 10;
        datapoints.add(new Datapoint().withMaximum(Math.random() * max));
        datapoints.add(new Datapoint().withMaximum(Math.random() * max));
        datapoints.add(new Datapoint().withMaximum(Math.random() * max));
        log.debug("data points: {}", datapoints);

        // ๊ฐ์ฒด ๋ชฉ๋ก ์ค‘ ํŠน์ • ํ•„๋“œ(maximum)๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ตœ๋Œ€๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด ์ถ”์ถœ (stream.max() ์ด์šฉ)
        Datapoint datapointMax = datapoints.stream()
                .max(Comparator.comparing(Datapoint::getMaximum))
                .orElse(new Datapoint().withMaximum(0D));
        log.debug("data point MAX: {}", datapointMax);

        // ๊ฐ์ฒด ๋ชฉ๋ก ์ค‘ ํŠน์ • ํ•„๋“œ(maximum)๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ตœ์†Œ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด ์ถ”์ถœ (stream.min() ์ด์šฉ)
        Datapoint datapointMin = datapoints.stream()
                .min(Comparator.comparing(Datapoint::getMaximum))
                .orElse(new Datapoint().withMaximum(0D));
        log.debug("data point MIN: {}", datapointMin);
    }
    
    
    
    // ์‹คํ–‰ ๋กœ๊ทธ
    data points: [{Maximum: 5.471390124016474,}, {Maximum: 2.7559360370945916,}, {Maximum: 0.5778257233234019,}]
    data point MAX: {Maximum: 5.471390124016474,}
    data point MIN: {Maximum: 0.5778257233234019,}

 

§ ์“ฐ๋ ˆ๋“œ ๊ธฐ๋ณธ ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ๋Š” [Java] ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

 

ThreadPoolExecutor ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

java.util.concurrent ํŒจํ‚ค์ง€๋Š” ๋™์‹œ์„ฑ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ด€๋ จํ•˜์—ฌ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ๋“ค์„ ๋ชจ์•„๋‘” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์—๋Š” ThreadPoolExecutor๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๊ฒƒ์„ ์ด์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ์„  ์ƒ์„ฑ์ž์— ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory)

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)


๊ธฐ๋ณธ์ ์œผ๋กœ corePoolSize, maximumPoolSize, keepAliveTime, timeUnit ๊ทธ๋ฆฌ๊ณ  workQueue๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ์žˆ๋„ค์š”.

๊ฐ ํ•ญ๋ชฉ์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๊ฐ€ ๋ญ”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. java doc์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

* @param corePoolSize the number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set

* @param maximumPoolSize the maximum number of threads to allow in the pool

* @param keepAliveTime when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

* @param unit the time unit for the {@code keepAliveTime} argument

* @param workQueue the queue to use for holding tasks before they are executed.


๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด corePoolSize๋Š” ์“ฐ๋ ˆ๋“œ ํ’€์˜ ๊ธฐ๋ณธ ์‚ฌ์ด์ฆˆ๋กœ ๋ช‡ ๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ pool์— ๊ฐ€์ง€๊ณ  ์žˆ์„์ง€์— ๋Œ€ํ•œ ๊ฐ’์ด๊ณ , maximumPoolSize๋Š” ์“ฐ๋ ˆ๋“œ ํ’€์˜ max size๋กœ ์ด ์‚ฌ์ด์ฆˆ๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด RejectedExecutionException์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. keepAliveTime์€ idle ์“ฐ๋ ˆ๋“œ์˜ keep alive time์ด๋ฉฐ, timeUnit์€ keepAliveTime์˜ ์‹œ๊ฐ„๋‹จ์œ„๋ฅผ, ๊ทธ๋ฆฌ๊ณ  workQueue๋Š” corePoolSize๋ฅผ ๋„˜์–ด์„œ๋Š” ์“ฐ๋ ˆ๋“œ๋“ค์„ queueing ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์ด์ œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

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

        BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(1);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100, 100, 10, TimeUnit.SECONDS, blockingQueue);

        Runnable task = new Task();
        for (int i = 0; i < 100; i++) {
            threadPoolExecutor.execute(task);
        }
        System.out.println("์“ฐ๋ ˆ๋“œ ์ฝœ ์ข…๋ฃŒ");
    }
    
    static class Task implements Runnable {

        int num = 0;

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + ", num=" + num++);
            }
        }
    }

 

์œ„ ์ฝ”๋“œ์—์„œ ThreadPoolExecutor์˜ corePoolSize์™€ maximumPoolSize๋ฅผ ๊ฐ๊ฐ 100, 100 ์œผ๋กœ ์„ค์ •์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด 100๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๊ฐ๊ฐ task๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. (๋”๋ณด๊ธฐ ํด๋ฆญํ•ด์„œ ๋ณด์„ธ์š”)

๋”๋ณด๊ธฐ

pool-1-thread-3, num=1
pool-1-thread-3, num=4
pool-1-thread-4, num=3
pool-1-thread-2, num=0
pool-1-thread-2, num=8
pool-1-thread-2, num=9
pool-1-thread-1, num=2
pool-1-thread-6, num=11
pool-1-thread-6, num=13
pool-1-thread-6, num=14
pool-1-thread-7, num=15
pool-1-thread-7, num=17
pool-1-thread-7, num=18
pool-1-thread-7, num=19
pool-1-thread-7, num=20
pool-1-thread-7, num=21
pool-1-thread-2, num=10
pool-1-thread-2, num=24
pool-1-thread-2, num=25
pool-1-thread-2, num=26
pool-1-thread-2, num=27
pool-1-thread-2, num=28
pool-1-thread-2, num=29
pool-1-thread-4, num=7
pool-1-thread-4, num=31
pool-1-thread-4, num=32
pool-1-thread-4, num=33
pool-1-thread-4, num=34
pool-1-thread-4, num=35
pool-1-thread-4, num=36
pool-1-thread-4, num=37
pool-1-thread-4, num=38
pool-1-thread-3, num=6
pool-1-thread-3, num=39
pool-1-thread-3, num=40
pool-1-thread-3, num=41
pool-1-thread-3, num=42
pool-1-thread-3, num=43
pool-1-thread-3, num=44
pool-1-thread-3, num=45
pool-1-thread-10, num=46
pool-1-thread-10, num=47
pool-1-thread-5, num=5
pool-1-thread-5, num=49
pool-1-thread-11, num=50
pool-1-thread-11, num=52
pool-1-thread-11, num=53
pool-1-thread-11, num=54
pool-1-thread-11, num=56
pool-1-thread-11, num=57
pool-1-thread-11, num=58
pool-1-thread-11, num=59
pool-1-thread-11, num=60
pool-1-thread-11, num=61
pool-1-thread-13, num=62
pool-1-thread-13, num=63
pool-1-thread-13, num=64
pool-1-thread-14, num=65
pool-1-thread-14, num=67
pool-1-thread-10, num=48
pool-1-thread-15, num=69
pool-1-thread-15, num=71
pool-1-thread-15, num=72
pool-1-thread-15, num=73
pool-1-thread-15, num=74
pool-1-thread-15, num=75
pool-1-thread-15, num=76
pool-1-thread-9, num=30
pool-1-thread-9, num=79
pool-1-thread-9, num=80
pool-1-thread-9, num=81
pool-1-thread-9, num=82
pool-1-thread-9, num=83
pool-1-thread-9, num=84
pool-1-thread-9, num=85
pool-1-thread-9, num=86
pool-1-thread-9, num=87
pool-1-thread-7, num=23
pool-1-thread-7, num=89
pool-1-thread-7, num=90
pool-1-thread-7, num=91
pool-1-thread-8, num=22
pool-1-thread-8, num=92
pool-1-thread-8, num=93
pool-1-thread-8, num=94
pool-1-thread-8, num=96
pool-1-thread-8, num=97
pool-1-thread-8, num=98
pool-1-thread-8, num=99
pool-1-thread-8, num=100
pool-1-thread-6, num=16
pool-1-thread-1, num=12
pool-1-thread-1, num=104
pool-1-thread-1, num=105
pool-1-thread-1, num=106
pool-1-thread-1, num=108
pool-1-thread-1, num=109
pool-1-thread-1, num=110
pool-1-thread-1, num=111
pool-1-thread-6, num=103
pool-1-thread-6, num=113
pool-1-thread-6, num=114
pool-1-thread-6, num=115
pool-1-thread-6, num=116
pool-1-thread-6, num=117
pool-1-thread-8, num=102
pool-1-thread-19, num=101
pool-1-thread-19, num=120
pool-1-thread-18, num=95
pool-1-thread-18, num=123
pool-1-thread-18, num=124
pool-1-thread-18, num=125
pool-1-thread-18, num=126
pool-1-thread-18, num=127
pool-1-thread-18, num=128
pool-1-thread-18, num=129
pool-1-thread-18, num=130
pool-1-thread-18, num=131
pool-1-thread-17, num=88
pool-1-thread-25, num=133
pool-1-thread-25, num=135
pool-1-thread-25, num=136
pool-1-thread-25, num=137
pool-1-thread-26, num=138
pool-1-thread-26, num=140
pool-1-thread-26, num=141
pool-1-thread-15, num=78
pool-1-thread-15, num=143
pool-1-thread-15, num=144
pool-1-thread-27, num=145
pool-1-thread-27, num=146
pool-1-thread-16, num=77
pool-1-thread-10, num=70
pool-1-thread-10, num=150
pool-1-thread-10, num=151
pool-1-thread-10, num=152
pool-1-thread-10, num=154
pool-1-thread-10, num=155
pool-1-thread-10, num=156
pool-1-thread-14, num=68
pool-1-thread-14, num=157
pool-1-thread-14, num=159
pool-1-thread-14, num=160
pool-1-thread-14, num=161
pool-1-thread-14, num=162
pool-1-thread-13, num=66
pool-1-thread-13, num=164
pool-1-thread-13, num=165
pool-1-thread-32, num=166
pool-1-thread-12, num=55
pool-1-thread-33, num=169
pool-1-thread-5, num=51
pool-1-thread-5, num=172
pool-1-thread-5, num=174
pool-1-thread-5, num=175
pool-1-thread-5, num=176
pool-1-thread-5, num=177
pool-1-thread-5, num=179
pool-1-thread-5, num=180
pool-1-thread-33, num=171
pool-1-thread-33, num=181
pool-1-thread-33, num=182
pool-1-thread-33, num=183
pool-1-thread-12, num=170
pool-1-thread-12, num=185
pool-1-thread-12, num=186
pool-1-thread-12, num=188
pool-1-thread-12, num=189
pool-1-thread-12, num=190
pool-1-thread-12, num=191
pool-1-thread-12, num=192
pool-1-thread-12, num=194
pool-1-thread-38, num=195
pool-1-thread-32, num=168
pool-1-thread-32, num=197
pool-1-thread-32, num=198
pool-1-thread-32, num=199
pool-1-thread-32, num=201
pool-1-thread-32, num=202
pool-1-thread-32, num=203
pool-1-thread-13, num=167
pool-1-thread-40, num=205
pool-1-thread-40, num=207
pool-1-thread-40, num=208
pool-1-thread-40, num=209
pool-1-thread-40, num=210
pool-1-thread-40, num=212
pool-1-thread-40, num=213
pool-1-thread-40, num=214
pool-1-thread-14, num=163
pool-1-thread-14, num=217
pool-1-thread-43, num=218
pool-1-thread-43, num=219
pool-1-thread-43, num=220
pool-1-thread-43, num=221
pool-1-thread-43, num=222
pool-1-thread-31, num=162
pool-1-thread-30, num=158
pool-1-thread-30, num=226
pool-1-thread-30, num=227
pool-1-thread-30, num=228
pool-1-thread-30, num=229
pool-1-thread-45, num=230
pool-1-thread-45, num=232
pool-1-thread-45, num=233
pool-1-thread-45, num=234
pool-1-thread-45, num=235
pool-1-thread-45, num=236
pool-1-thread-45, num=237
pool-1-thread-45, num=239
pool-1-thread-45, num=240
pool-1-thread-29, num=153
pool-1-thread-29, num=242
pool-1-thread-29, num=243
pool-1-thread-29, num=244
pool-1-thread-29, num=245
pool-1-thread-29, num=247
pool-1-thread-29, num=248
pool-1-thread-29, num=249
pool-1-thread-29, num=250
pool-1-thread-29, num=251
pool-1-thread-16, num=149
pool-1-thread-16, num=252
pool-1-thread-16, num=253
pool-1-thread-16, num=254
pool-1-thread-16, num=255
pool-1-thread-16, num=256
pool-1-thread-16, num=257
pool-1-thread-16, num=258
pool-1-thread-16, num=259
pool-1-thread-48, num=260
pool-1-thread-27, num=148
pool-1-thread-27, num=262
pool-1-thread-27, num=263
pool-1-thread-27, num=264
pool-1-thread-27, num=265
pool-1-thread-27, num=266
pool-1-thread-27, num=267
pool-1-thread-27, num=268
pool-1-thread-28, num=147
pool-1-thread-28, num=269
pool-1-thread-28, num=270
pool-1-thread-28, num=271
pool-1-thread-28, num=272
pool-1-thread-28, num=273
pool-1-thread-28, num=274
pool-1-thread-28, num=275
pool-1-thread-28, num=276
pool-1-thread-28, num=278
pool-1-thread-26, num=142
pool-1-thread-26, num=279
pool-1-thread-26, num=280
pool-1-thread-26, num=281
pool-1-thread-26, num=282
pool-1-thread-25, num=139
pool-1-thread-25, num=284
pool-1-thread-25, num=286
pool-1-thread-25, num=287
pool-1-thread-25, num=288
pool-1-thread-25, num=290
pool-1-thread-17, num=134
pool-1-thread-52, num=291
pool-1-thread-52, num=293
pool-1-thread-24, num=132
pool-1-thread-53, num=295
pool-1-thread-53, num=297
pool-1-thread-53, num=298
pool-1-thread-53, num=299
pool-1-thread-53, num=300
pool-1-thread-53, num=302
pool-1-thread-53, num=303
pool-1-thread-53, num=304
pool-1-thread-53, num=305
pool-1-thread-53, num=306
pool-1-thread-55, num=307
pool-1-thread-55, num=308
pool-1-thread-55, num=309
pool-1-thread-55, num=310
pool-1-thread-55, num=311
pool-1-thread-55, num=312
pool-1-thread-55, num=313
pool-1-thread-55, num=314
pool-1-thread-55, num=315
pool-1-thread-55, num=316
pool-1-thread-23, num=122
pool-1-thread-56, num=317
pool-1-thread-56, num=318
pool-1-thread-56, num=319
pool-1-thread-56, num=320
pool-1-thread-56, num=321
pool-1-thread-23, num=322
pool-1-thread-23, num=324
pool-1-thread-23, num=325
pool-1-thread-23, num=326
pool-1-thread-19, num=121
pool-1-thread-19, num=328
pool-1-thread-19, num=329
pool-1-thread-19, num=330
pool-1-thread-19, num=332
pool-1-thread-19, num=333
pool-1-thread-19, num=334
pool-1-thread-19, num=335
pool-1-thread-22, num=119
pool-1-thread-22, num=336
pool-1-thread-22, num=337
pool-1-thread-21, num=118
pool-1-thread-21, num=340
pool-1-thread-21, num=341
pool-1-thread-21, num=342
pool-1-thread-21, num=343
pool-1-thread-1, num=112
pool-1-thread-59, num=345
pool-1-thread-20, num=107
pool-1-thread-59, num=346
pool-1-thread-59, num=348
pool-1-thread-59, num=349
pool-1-thread-59, num=350
pool-1-thread-21, num=344
pool-1-thread-21, num=353
pool-1-thread-21, num=354
pool-1-thread-21, num=355
pool-1-thread-21, num=356
pool-1-thread-22, num=339
pool-1-thread-22, num=358
pool-1-thread-58, num=338
pool-1-thread-58, num=360
pool-1-thread-58, num=361
pool-1-thread-58, num=362
pool-1-thread-58, num=363
pool-1-thread-58, num=364
pool-1-thread-62, num=365
pool-1-thread-57, num=331
pool-1-thread-57, num=368
pool-1-thread-57, num=369
pool-1-thread-57, num=371
pool-1-thread-57, num=372
pool-1-thread-57, num=373
pool-1-thread-57, num=375
pool-1-thread-57, num=376
pool-1-thread-57, num=377
pool-1-thread-57, num=378
pool-1-thread-23, num=327
pool-1-thread-65, num=379
pool-1-thread-65, num=380
pool-1-thread-65, num=382
pool-1-thread-65, num=383
pool-1-thread-65, num=384
pool-1-thread-56, num=323
pool-1-thread-56, num=387
pool-1-thread-56, num=388
pool-1-thread-56, num=389
pool-1-thread-56, num=390
pool-1-thread-54, num=301
pool-1-thread-54, num=391
pool-1-thread-54, num=392
pool-1-thread-54, num=394
pool-1-thread-54, num=395
pool-1-thread-54, num=396
pool-1-thread-54, num=397
pool-1-thread-54, num=398
pool-1-thread-54, num=399
pool-1-thread-54, num=401
pool-1-thread-24, num=296
pool-1-thread-24, num=403
pool-1-thread-52, num=294
pool-1-thread-52, num=406
pool-1-thread-52, num=407
pool-1-thread-52, num=408
pool-1-thread-52, num=410
pool-1-thread-52, num=411
pool-1-thread-52, num=412
pool-1-thread-52, num=413
pool-1-thread-17, num=292
pool-1-thread-17, num=415
pool-1-thread-17, num=416
pool-1-thread-17, num=418
pool-1-thread-74, num=419
pool-1-thread-50, num=289
pool-1-thread-50, num=422
pool-1-thread-75, num=423
pool-1-thread-75, num=425
pool-1-thread-75, num=426
pool-1-thread-51, num=285
pool-1-thread-51, num=428
pool-1-thread-51, num=431
pool-1-thread-51, num=432
pool-1-thread-77, num=430
pool-1-thread-77, num=434
pool-1-thread-77, num=435
pool-1-thread-77, num=436
pool-1-thread-77, num=437
pool-1-thread-77, num=439
pool-1-thread-77, num=440
pool-1-thread-26, num=283
pool-1-thread-79, num=442
pool-1-thread-79, num=444
pool-1-thread-49, num=277
pool-1-thread-49, num=446
pool-1-thread-49, num=447
pool-1-thread-49, num=448
pool-1-thread-49, num=449
pool-1-thread-49, num=450
pool-1-thread-49, num=451
pool-1-thread-49, num=452
pool-1-thread-48, num=261
pool-1-thread-47, num=246
pool-1-thread-47, num=456
pool-1-thread-47, num=457
pool-1-thread-47, num=459
pool-1-thread-82, num=460
pool-1-thread-45, num=241
pool-1-thread-83, num=463
pool-1-thread-83, num=464
pool-1-thread-46, num=238
pool-1-thread-83, num=465
pool-1-thread-83, num=466
pool-1-thread-83, num=467
pool-1-thread-83, num=469
pool-1-thread-83, num=471
pool-1-thread-83, num=472
pool-1-thread-83, num=473
pool-1-thread-83, num=474
pool-1-thread-30, num=231
pool-1-thread-30, num=475
pool-1-thread-30, num=476
pool-1-thread-30, num=477
pool-1-thread-30, num=478
pool-1-thread-31, num=225
pool-1-thread-43, num=224
pool-1-thread-43, num=481
pool-1-thread-43, num=482
pool-1-thread-43, num=483
pool-1-thread-43, num=484
pool-1-thread-86, num=485
pool-1-thread-86, num=486
pool-1-thread-86, num=487
pool-1-thread-86, num=488
pool-1-thread-86, num=489
pool-1-thread-86, num=490
pool-1-thread-86, num=491
pool-1-thread-86, num=492
pool-1-thread-86, num=493
pool-1-thread-86, num=494
pool-1-thread-44, num=223
pool-1-thread-44, num=495
pool-1-thread-44, num=496
pool-1-thread-44, num=497
pool-1-thread-44, num=498
pool-1-thread-44, num=499
pool-1-thread-44, num=500
pool-1-thread-44, num=501
pool-1-thread-44, num=502
pool-1-thread-44, num=503
pool-1-thread-42, num=216
pool-1-thread-42, num=504
pool-1-thread-42, num=505
pool-1-thread-42, num=506
pool-1-thread-42, num=507
pool-1-thread-42, num=508
pool-1-thread-42, num=509
pool-1-thread-42, num=510
pool-1-thread-42, num=511
pool-1-thread-42, num=512
pool-1-thread-40, num=215
pool-1-thread-40, num=514
pool-1-thread-41, num=211
pool-1-thread-41, num=515
pool-1-thread-41, num=516
pool-1-thread-41, num=517
pool-1-thread-41, num=519
pool-1-thread-41, num=520
pool-1-thread-41, num=521
pool-1-thread-41, num=522
pool-1-thread-41, num=523
pool-1-thread-41, num=524
pool-1-thread-13, num=206
pool-1-thread-13, num=525
pool-1-thread-13, num=526
pool-1-thread-32, num=204
pool-1-thread-32, num=527
pool-1-thread-89, num=528
pool-1-thread-89, num=529
pool-1-thread-89, num=530
pool-1-thread-89, num=531
pool-1-thread-89, num=532
pool-1-thread-89, num=533
pool-1-thread-89, num=534
pool-1-thread-89, num=535
pool-1-thread-89, num=536
pool-1-thread-89, num=537
pool-1-thread-39, num=200
pool-1-thread-39, num=538
pool-1-thread-39, num=539
pool-1-thread-39, num=540
pool-1-thread-39, num=541
pool-1-thread-39, num=542
pool-1-thread-39, num=543
pool-1-thread-39, num=544
pool-1-thread-39, num=545
pool-1-thread-39, num=546
pool-1-thread-38, num=196
pool-1-thread-38, num=547
pool-1-thread-38, num=548
pool-1-thread-38, num=549
pool-1-thread-38, num=550
pool-1-thread-38, num=551
pool-1-thread-38, num=552
pool-1-thread-90, num=553
pool-1-thread-90, num=555
pool-1-thread-90, num=556
pool-1-thread-90, num=557
pool-1-thread-90, num=558
pool-1-thread-90, num=559
pool-1-thread-90, num=560
pool-1-thread-90, num=561
pool-1-thread-90, num=562
pool-1-thread-90, num=563
pool-1-thread-37, num=193
pool-1-thread-37, num=564
pool-1-thread-37, num=565
pool-1-thread-37, num=566
pool-1-thread-37, num=567
pool-1-thread-37, num=568
pool-1-thread-37, num=569
pool-1-thread-37, num=570
pool-1-thread-37, num=571
pool-1-thread-37, num=572
pool-1-thread-36, num=187
pool-1-thread-36, num=573
pool-1-thread-36, num=574
pool-1-thread-36, num=575
pool-1-thread-36, num=576
pool-1-thread-36, num=577
pool-1-thread-36, num=578
pool-1-thread-36, num=579
pool-1-thread-36, num=580
pool-1-thread-36, num=581
pool-1-thread-91, num=582
pool-1-thread-91, num=583
pool-1-thread-91, num=584
pool-1-thread-91, num=585
pool-1-thread-91, num=586
pool-1-thread-91, num=587
pool-1-thread-91, num=588
pool-1-thread-91, num=589
pool-1-thread-91, num=590
pool-1-thread-91, num=591
pool-1-thread-33, num=184
pool-1-thread-33, num=592
pool-1-thread-33, num=593
pool-1-thread-33, num=594
pool-1-thread-33, num=595
pool-1-thread-35, num=178
pool-1-thread-35, num=596
pool-1-thread-35, num=597
pool-1-thread-35, num=598
pool-1-thread-35, num=599
pool-1-thread-35, num=600
pool-1-thread-35, num=601
pool-1-thread-35, num=602
pool-1-thread-35, num=603
pool-1-thread-35, num=604
pool-1-thread-92, num=605
pool-1-thread-92, num=606
pool-1-thread-92, num=607
pool-1-thread-92, num=608
pool-1-thread-92, num=609
pool-1-thread-92, num=610
pool-1-thread-92, num=611
pool-1-thread-92, num=612
pool-1-thread-92, num=613
pool-1-thread-92, num=614
pool-1-thread-34, num=173
pool-1-thread-34, num=615
pool-1-thread-34, num=616
pool-1-thread-34, num=617
pool-1-thread-34, num=618
pool-1-thread-34, num=619
pool-1-thread-34, num=620
pool-1-thread-34, num=621
pool-1-thread-34, num=622
pool-1-thread-34, num=623
pool-1-thread-38, num=554
pool-1-thread-38, num=624
pool-1-thread-88, num=518
pool-1-thread-88, num=625
pool-1-thread-88, num=626
pool-1-thread-88, num=627
pool-1-thread-88, num=628
pool-1-thread-88, num=629
pool-1-thread-88, num=630
pool-1-thread-88, num=631
pool-1-thread-88, num=632
pool-1-thread-88, num=633
pool-1-thread-93, num=634
pool-1-thread-93, num=635
pool-1-thread-93, num=636
pool-1-thread-93, num=637
pool-1-thread-93, num=638
pool-1-thread-93, num=639
pool-1-thread-93, num=640
pool-1-thread-93, num=641
pool-1-thread-93, num=642
pool-1-thread-93, num=643
pool-1-thread-87, num=513
pool-1-thread-87, num=644
pool-1-thread-87, num=645
pool-1-thread-87, num=646
pool-1-thread-87, num=647
pool-1-thread-87, num=648
pool-1-thread-87, num=649
pool-1-thread-87, num=650
pool-1-thread-87, num=651
pool-1-thread-87, num=652
pool-1-thread-31, num=480
pool-1-thread-31, num=653
pool-1-thread-31, num=654
pool-1-thread-31, num=655
pool-1-thread-31, num=656
pool-1-thread-31, num=657
pool-1-thread-31, num=658
pool-1-thread-31, num=659
pool-1-thread-94, num=660
pool-1-thread-94, num=661
pool-1-thread-94, num=662
pool-1-thread-94, num=663
pool-1-thread-94, num=664
pool-1-thread-94, num=665
pool-1-thread-94, num=666
pool-1-thread-94, num=667
pool-1-thread-94, num=668
pool-1-thread-94, num=669
pool-1-thread-85, num=479
pool-1-thread-85, num=670
pool-1-thread-85, num=671
pool-1-thread-85, num=672
pool-1-thread-85, num=673
pool-1-thread-85, num=674
pool-1-thread-95, num=675
pool-1-thread-95, num=676
pool-1-thread-95, num=677
pool-1-thread-95, num=678
pool-1-thread-95, num=679
pool-1-thread-95, num=680
pool-1-thread-95, num=681
pool-1-thread-95, num=682
pool-1-thread-95, num=683
pool-1-thread-95, num=684
pool-1-thread-46, num=470
pool-1-thread-46, num=686
pool-1-thread-46, num=687
pool-1-thread-46, num=688
pool-1-thread-46, num=689
pool-1-thread-46, num=690
pool-1-thread-46, num=691
pool-1-thread-46, num=692
pool-1-thread-46, num=693
pool-1-thread-84, num=468
pool-1-thread-84, num=694
pool-1-thread-84, num=695
pool-1-thread-84, num=696
pool-1-thread-84, num=697
pool-1-thread-84, num=698
pool-1-thread-84, num=699
pool-1-thread-84, num=700
pool-1-thread-82, num=462
pool-1-thread-82, num=703
pool-1-thread-47, num=461
pool-1-thread-47, num=705
pool-1-thread-47, num=706
pool-1-thread-47, num=707
pool-1-thread-47, num=708
pool-1-thread-47, num=709
pool-1-thread-81, num=458
pool-1-thread-81, num=710
pool-1-thread-81, num=712
pool-1-thread-81, num=713
pool-1-thread-81, num=714
pool-1-thread-81, num=715
pool-1-thread-81, num=716
pool-1-thread-48, num=455
pool-1-thread-49, num=454
pool-1-thread-49, num=720
pool-1-thread-80, num=453
pool-1-thread-80, num=721
pool-1-thread-80, num=722
pool-1-thread-80, num=723
pool-1-thread-80, num=724
pool-1-thread-80, num=725
pool-1-thread-80, num=726
pool-1-thread-80, num=727
pool-1-thread-80, num=728
pool-1-thread-79, num=445
pool-1-thread-79, num=731
pool-1-thread-26, num=443
pool-1-thread-77, num=441
pool-1-thread-77, num=733
pool-1-thread-77, num=734
pool-1-thread-78, num=438
pool-1-thread-78, num=735
pool-1-thread-78, num=737
pool-1-thread-78, num=738
pool-1-thread-51, num=433
pool-1-thread-51, num=740
pool-1-thread-51, num=741
pool-1-thread-51, num=742
pool-1-thread-51, num=743
pool-1-thread-51, num=744
pool-1-thread-76, num=429
pool-1-thread-76, num=745
pool-1-thread-76, num=746
pool-1-thread-76, num=747
pool-1-thread-75, num=427
pool-1-thread-75, num=749
pool-1-thread-75, num=750
pool-1-thread-75, num=751
pool-1-thread-75, num=752
pool-1-thread-75, num=753
pool-1-thread-50, num=424
pool-1-thread-74, num=421
pool-1-thread-17, num=420
pool-1-thread-17, num=757
pool-1-thread-17, num=758
pool-1-thread-17, num=759
pool-1-thread-73, num=417
pool-1-thread-73, num=760
pool-1-thread-73, num=761
pool-1-thread-73, num=762
pool-1-thread-73, num=763
pool-1-thread-73, num=764
pool-1-thread-73, num=765
pool-1-thread-73, num=766
pool-1-thread-73, num=767
pool-1-thread-73, num=768
pool-1-thread-72, num=414
pool-1-thread-72, num=769
pool-1-thread-72, num=770
pool-1-thread-72, num=771
pool-1-thread-72, num=772
pool-1-thread-72, num=773
pool-1-thread-72, num=774
pool-1-thread-72, num=775
pool-1-thread-72, num=776
pool-1-thread-72, num=777
pool-1-thread-71, num=409
pool-1-thread-71, num=778
pool-1-thread-71, num=779
pool-1-thread-71, num=780
pool-1-thread-24, num=405
pool-1-thread-24, num=782
pool-1-thread-24, num=783
pool-1-thread-24, num=784
pool-1-thread-24, num=785
pool-1-thread-24, num=786
pool-1-thread-24, num=787
pool-1-thread-70, num=404
pool-1-thread-70, num=788
pool-1-thread-70, num=789
pool-1-thread-70, num=790
pool-1-thread-70, num=791
pool-1-thread-70, num=792
pool-1-thread-70, num=793
pool-1-thread-70, num=794
pool-1-thread-70, num=795
pool-1-thread-69, num=402
pool-1-thread-69, num=797
pool-1-thread-68, num=400
pool-1-thread-68, num=799
pool-1-thread-68, num=800
pool-1-thread-68, num=801
pool-1-thread-68, num=802
pool-1-thread-68, num=803
pool-1-thread-68, num=804
pool-1-thread-67, num=393
pool-1-thread-67, num=806
pool-1-thread-67, num=807
pool-1-thread-67, num=808
pool-1-thread-65, num=386
pool-1-thread-67, num=809
pool-1-thread-67, num=810
pool-1-thread-67, num=811
pool-1-thread-67, num=812
pool-1-thread-67, num=813
pool-1-thread-67, num=814
pool-1-thread-65, num=815
pool-1-thread-65, num=816
pool-1-thread-65, num=817
pool-1-thread-65, num=818
pool-1-thread-66, num=385
pool-1-thread-23, num=381
pool-1-thread-66, num=819
pool-1-thread-66, num=820
pool-1-thread-66, num=821
pool-1-thread-66, num=822
pool-1-thread-66, num=823
pool-1-thread-66, num=824
pool-1-thread-66, num=825
pool-1-thread-66, num=826
pool-1-thread-66, num=827
pool-1-thread-23, num=828
pool-1-thread-23, num=829
pool-1-thread-23, num=830
pool-1-thread-64, num=374
pool-1-thread-63, num=370
pool-1-thread-64, num=831
pool-1-thread-64, num=832
pool-1-thread-64, num=833
pool-1-thread-64, num=834
pool-1-thread-64, num=835
pool-1-thread-64, num=836
pool-1-thread-64, num=837
pool-1-thread-64, num=838
pool-1-thread-64, num=839
pool-1-thread-63, num=840
pool-1-thread-63, num=841
pool-1-thread-63, num=842
pool-1-thread-63, num=843
pool-1-thread-63, num=844
pool-1-thread-63, num=845
pool-1-thread-63, num=846
pool-1-thread-63, num=847
pool-1-thread-63, num=848
pool-1-thread-62, num=367
pool-1-thread-58, num=366
pool-1-thread-62, num=849
pool-1-thread-62, num=850
pool-1-thread-62, num=851
pool-1-thread-62, num=852
pool-1-thread-62, num=853
pool-1-thread-62, num=854
pool-1-thread-62, num=855
pool-1-thread-62, num=856
pool-1-thread-58, num=857
pool-1-thread-58, num=858
pool-1-thread-58, num=859
pool-1-thread-22, num=359
pool-1-thread-61, num=357
pool-1-thread-22, num=860
pool-1-thread-22, num=861
pool-1-thread-22, num=862
pool-1-thread-22, num=863
pool-1-thread-61, num=864
pool-1-thread-61, num=865
pool-1-thread-61, num=866
pool-1-thread-61, num=867
pool-1-thread-61, num=868
pool-1-thread-61, num=869
pool-1-thread-61, num=870
pool-1-thread-61, num=871
pool-1-thread-61, num=872
pool-1-thread-59, num=352
pool-1-thread-60, num=351
pool-1-thread-59, num=873
pool-1-thread-59, num=874
pool-1-thread-59, num=875
pool-1-thread-59, num=876
pool-1-thread-60, num=877
pool-1-thread-60, num=878
pool-1-thread-60, num=879
pool-1-thread-60, num=880
pool-1-thread-60, num=881
pool-1-thread-60, num=882
pool-1-thread-60, num=883
pool-1-thread-60, num=884
pool-1-thread-60, num=885
pool-1-thread-20, num=347
pool-1-thread-20, num=886
pool-1-thread-20, num=887
pool-1-thread-20, num=888
pool-1-thread-20, num=889
pool-1-thread-20, num=890
pool-1-thread-20, num=891
pool-1-thread-20, num=892
pool-1-thread-20, num=893
pool-1-thread-68, num=805
pool-1-thread-69, num=798
pool-1-thread-68, num=894
pool-1-thread-68, num=895
pool-1-thread-69, num=896
pool-1-thread-69, num=897
pool-1-thread-69, num=898
pool-1-thread-69, num=899
pool-1-thread-70, num=796
pool-1-thread-71, num=781
pool-1-thread-71, num=901
pool-1-thread-71, num=902
pool-1-thread-71, num=903
pool-1-thread-71, num=904
pool-1-thread-71, num=905
pool-1-thread-74, num=756
pool-1-thread-74, num=906
pool-1-thread-74, num=907
pool-1-thread-74, num=908
pool-1-thread-74, num=909
pool-1-thread-74, num=910
pool-1-thread-74, num=911
pool-1-thread-74, num=912
pool-1-thread-50, num=755
pool-1-thread-50, num=913
pool-1-thread-50, num=914
pool-1-thread-50, num=915
pool-1-thread-50, num=916
pool-1-thread-50, num=917
pool-1-thread-50, num=918
pool-1-thread-75, num=754
pool-1-thread-76, num=748
pool-1-thread-76, num=919
pool-1-thread-76, num=920
pool-1-thread-76, num=921
pool-1-thread-76, num=922
pool-1-thread-76, num=923
pool-1-thread-78, num=739
pool-1-thread-100, num=736
pool-1-thread-100, num=925
pool-1-thread-100, num=926
pool-1-thread-100, num=927
pool-1-thread-100, num=928
pool-1-thread-100, num=929
pool-1-thread-100, num=930
pool-1-thread-100, num=931
pool-1-thread-100, num=932
pool-1-thread-100, num=933
pool-1-thread-79, num=732
์“ฐ๋ ˆ๋“œ ์ฝœ ์ข…๋ฃŒ
pool-1-thread-79, num=934
pool-1-thread-79, num=935
pool-1-thread-79, num=936
pool-1-thread-79, num=937
pool-1-thread-79, num=938
pool-1-thread-80, num=730
pool-1-thread-99, num=729
pool-1-thread-99, num=939
pool-1-thread-99, num=940
pool-1-thread-99, num=941
pool-1-thread-99, num=942
pool-1-thread-99, num=943
pool-1-thread-99, num=944
pool-1-thread-99, num=945
pool-1-thread-99, num=946
pool-1-thread-99, num=947
pool-1-thread-48, num=719
pool-1-thread-48, num=948
pool-1-thread-48, num=949
pool-1-thread-48, num=950
pool-1-thread-48, num=951
pool-1-thread-48, num=952
pool-1-thread-48, num=953
pool-1-thread-81, num=717
pool-1-thread-81, num=954
pool-1-thread-81, num=955
pool-1-thread-98, num=718
pool-1-thread-98, num=956
pool-1-thread-98, num=957
pool-1-thread-98, num=958
pool-1-thread-98, num=959
pool-1-thread-98, num=960
pool-1-thread-98, num=961
pool-1-thread-98, num=962
pool-1-thread-98, num=963
pool-1-thread-98, num=964
pool-1-thread-97, num=711
pool-1-thread-97, num=965
pool-1-thread-97, num=966
pool-1-thread-97, num=967
pool-1-thread-97, num=968
pool-1-thread-97, num=969
pool-1-thread-97, num=970
pool-1-thread-97, num=971
pool-1-thread-97, num=972
pool-1-thread-97, num=973
pool-1-thread-82, num=704
pool-1-thread-82, num=974
pool-1-thread-82, num=975
pool-1-thread-82, num=976
pool-1-thread-82, num=977
pool-1-thread-82, num=978
pool-1-thread-82, num=979
pool-1-thread-84, num=702
pool-1-thread-84, num=980
pool-1-thread-96, num=701
pool-1-thread-96, num=981
pool-1-thread-96, num=982
pool-1-thread-96, num=983
pool-1-thread-96, num=984
pool-1-thread-96, num=985
pool-1-thread-96, num=986
pool-1-thread-96, num=987
pool-1-thread-96, num=988
pool-1-thread-96, num=989
pool-1-thread-85, num=685
pool-1-thread-85, num=990
pool-1-thread-85, num=991
pool-1-thread-85, num=992
pool-1-thread-78, num=924
pool-1-thread-78, num=993
pool-1-thread-78, num=994
pool-1-thread-78, num=995
pool-1-thread-78, num=996
pool-1-thread-69, num=900
pool-1-thread-69, num=997
pool-1-thread-69, num=998
executor has been terminated

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

 

์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์ž์„ธํžˆ ํ™•์ธํ•ด๋ณด๋ฉด thread๋Š” thread-1 ~ thread-100 ๊นŒ์ง€ ์ด 100๊ฐœ๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์‹คํ–‰์ด ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

์ด๋ฒˆ์—๋Š” corePoolSize๋ฅผ 1๋กœ ์„ค์ •์„ ํ•ด์„œ ์‹คํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (๋”๋ณด๊ธฐ ํด๋ฆญํ•ด์„œ ๋ณด์„ธ์š”)

๋”๋ณด๊ธฐ

pool-1-thread-2, num=0
pool-1-thread-3, num=0
pool-1-thread-1, num=1
pool-1-thread-3, num=3
pool-1-thread-3, num=5
pool-1-thread-2, num=2
pool-1-thread-2, num=7
pool-1-thread-3, num=6
pool-1-thread-1, num=4
pool-1-thread-3, num=9
pool-1-thread-2, num=8
pool-1-thread-3, num=11
pool-1-thread-1, num=10
pool-1-thread-3, num=13
pool-1-thread-2, num=12
pool-1-thread-3, num=15
pool-1-thread-1, num=14
pool-1-thread-1, num=18
pool-1-thread-3, num=17
pool-1-thread-2, num=16
pool-1-thread-3, num=20
pool-1-thread-1, num=19
pool-1-thread-2, num=21
pool-1-thread-3, num=23
pool-1-thread-1, num=22
pool-1-thread-1, num=26
pool-1-thread-3, num=25
pool-1-thread-2, num=24
pool-1-thread-2, num=29
pool-1-thread-2, num=30
pool-1-thread-3, num=28
pool-1-thread-1, num=27
pool-1-thread-3, num=31
pool-1-thread-3, num=33
pool-1-thread-3, num=34
pool-1-thread-3, num=35
pool-1-thread-3, num=36
pool-1-thread-1, num=32
pool-1-thread-3, num=37
pool-1-thread-3, num=38
pool-1-thread-4, num=39
pool-1-thread-4, num=40
pool-1-thread-4, num=41
pool-1-thread-4, num=42
pool-1-thread-4, num=43
pool-1-thread-4, num=44
pool-1-thread-4, num=45
pool-1-thread-4, num=46
pool-1-thread-4, num=47
pool-1-thread-4, num=48
pool-1-thread-4, num=49
pool-1-thread-4, num=50
pool-1-thread-2, num=51
pool-1-thread-4, num=52
pool-1-thread-4, num=54
pool-1-thread-2, num=53
pool-1-thread-4, num=55
pool-1-thread-4, num=57
pool-1-thread-4, num=58
pool-1-thread-2, num=56
pool-1-thread-4, num=59
pool-1-thread-2, num=60
pool-1-thread-4, num=61
pool-1-thread-2, num=62
pool-1-thread-2, num=64
pool-1-thread-4, num=63
pool-1-thread-2, num=65
pool-1-thread-2, num=66
pool-1-thread-2, num=67
pool-1-thread-2, num=68
pool-1-thread-5, num=69
pool-1-thread-5, num=70
pool-1-thread-5, num=71
pool-1-thread-5, num=72
pool-1-thread-5, num=73
pool-1-thread-5, num=74
pool-1-thread-5, num=75
pool-1-thread-5, num=76
pool-1-thread-5, num=77
pool-1-thread-5, num=78
pool-1-thread-5, num=79
pool-1-thread-5, num=80
pool-1-thread-5, num=81
pool-1-thread-5, num=82
pool-1-thread-5, num=83
pool-1-thread-3, num=84
pool-1-thread-5, num=85
pool-1-thread-3, num=86
pool-1-thread-5, num=87
pool-1-thread-3, num=88
pool-1-thread-3, num=90
pool-1-thread-5, num=89
pool-1-thread-3, num=91
pool-1-thread-5, num=92
pool-1-thread-3, num=93
pool-1-thread-5, num=94
pool-1-thread-3, num=95
pool-1-thread-3, num=96
pool-1-thread-3, num=97
pool-1-thread-3, num=98
pool-1-thread-6, num=99
pool-1-thread-6, num=100
pool-1-thread-6, num=101
pool-1-thread-6, num=102
pool-1-thread-6, num=103
pool-1-thread-6, num=104
pool-1-thread-6, num=105
pool-1-thread-6, num=106
pool-1-thread-6, num=107
pool-1-thread-6, num=108
pool-1-thread-4, num=109
pool-1-thread-4, num=110
pool-1-thread-4, num=111
pool-1-thread-4, num=112
pool-1-thread-4, num=113
pool-1-thread-4, num=114
pool-1-thread-4, num=115
pool-1-thread-4, num=116
pool-1-thread-4, num=117
pool-1-thread-4, num=118
pool-1-thread-2, num=119
pool-1-thread-2, num=120
pool-1-thread-2, num=121
pool-1-thread-2, num=122
pool-1-thread-2, num=123
pool-1-thread-2, num=124
pool-1-thread-2, num=125
pool-1-thread-2, num=126
pool-1-thread-2, num=127
pool-1-thread-2, num=128
pool-1-thread-1, num=129
pool-1-thread-1, num=130
pool-1-thread-1, num=131
pool-1-thread-1, num=132
pool-1-thread-1, num=133
pool-1-thread-1, num=134
pool-1-thread-1, num=135
pool-1-thread-1, num=136
pool-1-thread-1, num=137
pool-1-thread-1, num=138
pool-1-thread-7, num=139
pool-1-thread-7, num=140
pool-1-thread-7, num=141
pool-1-thread-7, num=142
pool-1-thread-7, num=143
pool-1-thread-7, num=144
pool-1-thread-7, num=145
pool-1-thread-7, num=146
pool-1-thread-7, num=147
pool-1-thread-7, num=148
pool-1-thread-5, num=149
pool-1-thread-5, num=150
pool-1-thread-5, num=151
pool-1-thread-5, num=152
pool-1-thread-5, num=153
pool-1-thread-5, num=154
pool-1-thread-5, num=155
pool-1-thread-5, num=156
pool-1-thread-5, num=157
pool-1-thread-5, num=158
pool-1-thread-3, num=159
pool-1-thread-3, num=160
pool-1-thread-3, num=161
pool-1-thread-3, num=162
pool-1-thread-3, num=163
pool-1-thread-3, num=164
pool-1-thread-3, num=165
pool-1-thread-3, num=166
pool-1-thread-3, num=167
pool-1-thread-3, num=168
pool-1-thread-8, num=169
pool-1-thread-8, num=170
pool-1-thread-8, num=171
pool-1-thread-8, num=172
pool-1-thread-8, num=173
pool-1-thread-8, num=174
pool-1-thread-8, num=175
pool-1-thread-8, num=176
pool-1-thread-8, num=177
pool-1-thread-8, num=178
pool-1-thread-6, num=179
pool-1-thread-6, num=180
pool-1-thread-6, num=181
pool-1-thread-6, num=182
pool-1-thread-6, num=183
pool-1-thread-6, num=184
pool-1-thread-6, num=185
pool-1-thread-6, num=186
pool-1-thread-6, num=187
pool-1-thread-6, num=188
pool-1-thread-4, num=189
pool-1-thread-9, num=190
pool-1-thread-9, num=192
pool-1-thread-9, num=193
pool-1-thread-9, num=194
pool-1-thread-4, num=191
pool-1-thread-9, num=195
pool-1-thread-4, num=196
pool-1-thread-9, num=197
pool-1-thread-4, num=198
pool-1-thread-4, num=200
pool-1-thread-9, num=199
pool-1-thread-9, num=202
pool-1-thread-4, num=201
pool-1-thread-9, num=203
pool-1-thread-9, num=205
pool-1-thread-4, num=204
pool-1-thread-4, num=206
pool-1-thread-4, num=207
pool-1-thread-4, num=208
pool-1-thread-2, num=209
pool-1-thread-2, num=210
pool-1-thread-10, num=211
pool-1-thread-10, num=213
pool-1-thread-10, num=214
pool-1-thread-10, num=215
pool-1-thread-10, num=216
pool-1-thread-2, num=212
pool-1-thread-10, num=217
pool-1-thread-2, num=218
pool-1-thread-2, num=220
pool-1-thread-10, num=219
pool-1-thread-2, num=221
pool-1-thread-10, num=222
pool-1-thread-10, num=224
pool-1-thread-2, num=223
pool-1-thread-2, num=226
pool-1-thread-10, num=225
pool-1-thread-2, num=227
pool-1-thread-2, num=228
pool-1-thread-11, num=229
pool-1-thread-11, num=230
pool-1-thread-11, num=231
pool-1-thread-11, num=232
pool-1-thread-11, num=233
pool-1-thread-11, num=234
pool-1-thread-11, num=235
pool-1-thread-1, num=236
pool-1-thread-11, num=237
pool-1-thread-1, num=238
pool-1-thread-11, num=239
pool-1-thread-1, num=240
pool-1-thread-1, num=241
pool-1-thread-1, num=242
pool-1-thread-1, num=243
pool-1-thread-1, num=244
pool-1-thread-1, num=245
pool-1-thread-1, num=246
pool-1-thread-1, num=247
pool-1-thread-11, num=248
pool-1-thread-7, num=249
pool-1-thread-7, num=250
pool-1-thread-7, num=251
pool-1-thread-12, num=252
pool-1-thread-7, num=253
pool-1-thread-12, num=254
pool-1-thread-12, num=257
pool-1-thread-12, num=258
pool-1-thread-12, num=259
pool-1-thread-12, num=260
pool-1-thread-5, num=256
pool-1-thread-7, num=255
pool-1-thread-5, num=262
pool-1-thread-5, num=264
pool-1-thread-5, num=265
pool-1-thread-12, num=261
pool-1-thread-5, num=266
pool-1-thread-5, num=268
pool-1-thread-5, num=269
pool-1-thread-5, num=270
pool-1-thread-5, num=271
pool-1-thread-5, num=272
pool-1-thread-7, num=263
pool-1-thread-7, num=273
pool-1-thread-12, num=267
pool-1-thread-12, num=275
pool-1-thread-12, num=276
pool-1-thread-7, num=274
pool-1-thread-7, num=277
pool-1-thread-7, num=278
pool-1-thread-3, num=279
pool-1-thread-3, num=280
pool-1-thread-3, num=281
pool-1-thread-3, num=282
pool-1-thread-3, num=283
pool-1-thread-3, num=284
pool-1-thread-3, num=285
pool-1-thread-3, num=286
pool-1-thread-3, num=287
pool-1-thread-3, num=288
pool-1-thread-8, num=289
pool-1-thread-8, num=290
pool-1-thread-8, num=291
pool-1-thread-8, num=292
pool-1-thread-8, num=293
pool-1-thread-8, num=294
pool-1-thread-8, num=295
pool-1-thread-8, num=296
pool-1-thread-8, num=297
pool-1-thread-8, num=298
pool-1-thread-6, num=299
pool-1-thread-6, num=300
pool-1-thread-6, num=301
pool-1-thread-6, num=302
pool-1-thread-6, num=303
pool-1-thread-6, num=304
pool-1-thread-6, num=305
pool-1-thread-6, num=306
pool-1-thread-6, num=307
pool-1-thread-6, num=308
pool-1-thread-13, num=309
pool-1-thread-13, num=310
pool-1-thread-13, num=311
pool-1-thread-13, num=312
pool-1-thread-13, num=313
pool-1-thread-13, num=314
pool-1-thread-13, num=315
pool-1-thread-13, num=316
pool-1-thread-13, num=317
pool-1-thread-13, num=318
pool-1-thread-9, num=319
pool-1-thread-9, num=320
pool-1-thread-9, num=321
pool-1-thread-9, num=322
pool-1-thread-9, num=323
pool-1-thread-9, num=324
pool-1-thread-9, num=325
pool-1-thread-9, num=326
pool-1-thread-9, num=327
pool-1-thread-9, num=328
pool-1-thread-14, num=329
pool-1-thread-14, num=330
pool-1-thread-14, num=331
pool-1-thread-14, num=332
pool-1-thread-14, num=333
pool-1-thread-14, num=334
pool-1-thread-14, num=335
pool-1-thread-14, num=336
pool-1-thread-14, num=337
pool-1-thread-14, num=338
pool-1-thread-4, num=339
pool-1-thread-4, num=340
pool-1-thread-4, num=341
pool-1-thread-4, num=342
pool-1-thread-4, num=343
pool-1-thread-4, num=344
pool-1-thread-4, num=345
pool-1-thread-4, num=346
pool-1-thread-4, num=347
pool-1-thread-4, num=348
pool-1-thread-10, num=349
pool-1-thread-10, num=350
pool-1-thread-10, num=351
pool-1-thread-10, num=352
pool-1-thread-10, num=353
pool-1-thread-10, num=354
pool-1-thread-10, num=355
pool-1-thread-10, num=356
pool-1-thread-10, num=357
pool-1-thread-10, num=358
pool-1-thread-2, num=359
pool-1-thread-2, num=360
pool-1-thread-2, num=361
pool-1-thread-2, num=362
pool-1-thread-2, num=363
pool-1-thread-2, num=364
pool-1-thread-2, num=365
pool-1-thread-2, num=366
pool-1-thread-2, num=367
pool-1-thread-2, num=368
pool-1-thread-1, num=369
pool-1-thread-1, num=370
pool-1-thread-1, num=371
pool-1-thread-1, num=372
pool-1-thread-1, num=373
pool-1-thread-1, num=374
pool-1-thread-1, num=375
pool-1-thread-1, num=376
pool-1-thread-1, num=377
pool-1-thread-1, num=378
pool-1-thread-15, num=379
pool-1-thread-15, num=380
pool-1-thread-15, num=381
pool-1-thread-15, num=382
pool-1-thread-15, num=383
pool-1-thread-15, num=384
pool-1-thread-15, num=385
pool-1-thread-15, num=386
pool-1-thread-15, num=387
pool-1-thread-15, num=388
pool-1-thread-11, num=389
pool-1-thread-11, num=390
pool-1-thread-11, num=391
pool-1-thread-11, num=392
pool-1-thread-11, num=393
pool-1-thread-11, num=394
pool-1-thread-11, num=395
pool-1-thread-11, num=396
pool-1-thread-11, num=397
pool-1-thread-11, num=398
pool-1-thread-5, num=399
pool-1-thread-5, num=400
pool-1-thread-5, num=401
pool-1-thread-5, num=402
pool-1-thread-5, num=403
pool-1-thread-5, num=404
pool-1-thread-5, num=405
pool-1-thread-5, num=406
pool-1-thread-5, num=407
pool-1-thread-5, num=408
pool-1-thread-12, num=409
pool-1-thread-12, num=410
pool-1-thread-12, num=411
pool-1-thread-12, num=412
pool-1-thread-12, num=413
pool-1-thread-12, num=414
pool-1-thread-12, num=415
pool-1-thread-12, num=416
pool-1-thread-12, num=417
pool-1-thread-12, num=418
pool-1-thread-7, num=419
pool-1-thread-7, num=420
pool-1-thread-7, num=421
pool-1-thread-7, num=422
pool-1-thread-7, num=423
pool-1-thread-7, num=424
pool-1-thread-7, num=425
pool-1-thread-7, num=426
pool-1-thread-7, num=427
pool-1-thread-7, num=428
pool-1-thread-16, num=429
pool-1-thread-16, num=430
pool-1-thread-16, num=431
pool-1-thread-16, num=432
pool-1-thread-16, num=433
pool-1-thread-16, num=434
pool-1-thread-16, num=435
pool-1-thread-16, num=436
pool-1-thread-16, num=437
pool-1-thread-16, num=438
pool-1-thread-3, num=439
pool-1-thread-3, num=440
pool-1-thread-3, num=441
pool-1-thread-3, num=442
pool-1-thread-3, num=443
pool-1-thread-3, num=444
pool-1-thread-3, num=445
pool-1-thread-3, num=446
pool-1-thread-3, num=447
pool-1-thread-3, num=448
pool-1-thread-8, num=449
pool-1-thread-8, num=450
pool-1-thread-8, num=451
pool-1-thread-8, num=452
pool-1-thread-8, num=453
pool-1-thread-8, num=454
pool-1-thread-8, num=455
pool-1-thread-8, num=456
pool-1-thread-8, num=457
pool-1-thread-8, num=458
pool-1-thread-6, num=459
pool-1-thread-6, num=460
pool-1-thread-6, num=461
pool-1-thread-6, num=462
pool-1-thread-6, num=463
pool-1-thread-6, num=464
pool-1-thread-6, num=465
pool-1-thread-6, num=466
pool-1-thread-6, num=467
pool-1-thread-6, num=468
pool-1-thread-6, num=469
pool-1-thread-6, num=470
pool-1-thread-6, num=471
pool-1-thread-6, num=472
pool-1-thread-6, num=473
pool-1-thread-6, num=474
pool-1-thread-6, num=475
pool-1-thread-6, num=476
pool-1-thread-6, num=477
pool-1-thread-6, num=478
pool-1-thread-9, num=479
pool-1-thread-9, num=480
pool-1-thread-9, num=481
pool-1-thread-9, num=482
pool-1-thread-9, num=483
pool-1-thread-9, num=484
pool-1-thread-9, num=485
pool-1-thread-9, num=486
pool-1-thread-9, num=487
pool-1-thread-9, num=488
pool-1-thread-17, num=489
pool-1-thread-17, num=490
pool-1-thread-17, num=491
pool-1-thread-17, num=492
pool-1-thread-17, num=493
pool-1-thread-17, num=494
pool-1-thread-17, num=495
pool-1-thread-17, num=496
pool-1-thread-17, num=497
pool-1-thread-17, num=498
pool-1-thread-14, num=499
pool-1-thread-14, num=500
pool-1-thread-14, num=501
pool-1-thread-14, num=502
pool-1-thread-14, num=503
pool-1-thread-14, num=504
pool-1-thread-14, num=505
pool-1-thread-14, num=506
pool-1-thread-14, num=507
pool-1-thread-14, num=508
pool-1-thread-4, num=509
pool-1-thread-4, num=510
pool-1-thread-4, num=511
pool-1-thread-4, num=512
pool-1-thread-4, num=513
pool-1-thread-4, num=514
pool-1-thread-4, num=515
pool-1-thread-4, num=516
pool-1-thread-4, num=517
pool-1-thread-4, num=518
pool-1-thread-10, num=519
pool-1-thread-10, num=520
pool-1-thread-10, num=521
pool-1-thread-10, num=522
pool-1-thread-10, num=523
pool-1-thread-10, num=524
pool-1-thread-10, num=525
pool-1-thread-10, num=526
pool-1-thread-10, num=527
pool-1-thread-10, num=528
pool-1-thread-2, num=529
pool-1-thread-2, num=530
pool-1-thread-2, num=531
pool-1-thread-2, num=532
pool-1-thread-2, num=533
pool-1-thread-2, num=534
pool-1-thread-2, num=535
pool-1-thread-2, num=536
pool-1-thread-2, num=537
pool-1-thread-2, num=538
pool-1-thread-1, num=539
pool-1-thread-1, num=540
pool-1-thread-1, num=541
pool-1-thread-1, num=542
pool-1-thread-1, num=543
pool-1-thread-1, num=544
pool-1-thread-1, num=545
pool-1-thread-1, num=546
pool-1-thread-1, num=547
pool-1-thread-1, num=548
pool-1-thread-15, num=549
pool-1-thread-15, num=550
pool-1-thread-15, num=551
pool-1-thread-15, num=552
pool-1-thread-15, num=553
pool-1-thread-15, num=554
pool-1-thread-15, num=555
pool-1-thread-15, num=556
pool-1-thread-15, num=557
pool-1-thread-15, num=558
pool-1-thread-18, num=559
pool-1-thread-18, num=560
pool-1-thread-18, num=561
pool-1-thread-18, num=562
pool-1-thread-18, num=563
pool-1-thread-18, num=564
pool-1-thread-18, num=565
pool-1-thread-18, num=566
pool-1-thread-18, num=567
pool-1-thread-18, num=568
pool-1-thread-11, num=569
pool-1-thread-11, num=570
pool-1-thread-11, num=571
pool-1-thread-11, num=572
pool-1-thread-11, num=573
pool-1-thread-11, num=574
pool-1-thread-11, num=575
pool-1-thread-11, num=576
pool-1-thread-11, num=577
pool-1-thread-11, num=578
pool-1-thread-5, num=579
pool-1-thread-5, num=580
pool-1-thread-5, num=581
pool-1-thread-5, num=582
pool-1-thread-5, num=583
pool-1-thread-5, num=584
pool-1-thread-5, num=585
pool-1-thread-5, num=586
pool-1-thread-5, num=587
pool-1-thread-5, num=588
pool-1-thread-12, num=589
pool-1-thread-12, num=590
pool-1-thread-12, num=591
pool-1-thread-12, num=592
pool-1-thread-12, num=593
pool-1-thread-12, num=594
pool-1-thread-12, num=595
pool-1-thread-12, num=596
pool-1-thread-12, num=597
pool-1-thread-12, num=598
pool-1-thread-7, num=599
pool-1-thread-7, num=600
pool-1-thread-7, num=601
pool-1-thread-7, num=602
pool-1-thread-7, num=603
pool-1-thread-7, num=604
pool-1-thread-7, num=605
pool-1-thread-7, num=606
pool-1-thread-7, num=607
pool-1-thread-7, num=608
pool-1-thread-19, num=609
pool-1-thread-19, num=610
pool-1-thread-19, num=611
pool-1-thread-19, num=612
pool-1-thread-19, num=613
pool-1-thread-19, num=614
pool-1-thread-19, num=615
pool-1-thread-19, num=616
pool-1-thread-19, num=617
pool-1-thread-19, num=618
pool-1-thread-16, num=619
pool-1-thread-16, num=620
pool-1-thread-16, num=621
pool-1-thread-16, num=622
pool-1-thread-16, num=623
pool-1-thread-16, num=624
pool-1-thread-16, num=625
pool-1-thread-16, num=626
pool-1-thread-16, num=627
pool-1-thread-16, num=628
pool-1-thread-3, num=629
pool-1-thread-3, num=630
pool-1-thread-3, num=631
pool-1-thread-3, num=632
pool-1-thread-3, num=633
pool-1-thread-3, num=634
pool-1-thread-3, num=635
pool-1-thread-3, num=636
pool-1-thread-3, num=637
pool-1-thread-3, num=638
pool-1-thread-8, num=639
pool-1-thread-8, num=640
pool-1-thread-8, num=641
pool-1-thread-8, num=642
pool-1-thread-8, num=643
pool-1-thread-8, num=644
pool-1-thread-8, num=645
pool-1-thread-8, num=646
pool-1-thread-8, num=647
pool-1-thread-8, num=648
pool-1-thread-6, num=649
pool-1-thread-6, num=650
pool-1-thread-6, num=651
pool-1-thread-6, num=652
pool-1-thread-6, num=653
pool-1-thread-6, num=654
pool-1-thread-6, num=655
pool-1-thread-6, num=656
pool-1-thread-6, num=657
pool-1-thread-6, num=658
pool-1-thread-13, num=659
pool-1-thread-13, num=660
pool-1-thread-13, num=661
pool-1-thread-13, num=662
pool-1-thread-13, num=663
pool-1-thread-13, num=664
pool-1-thread-13, num=665
pool-1-thread-13, num=666
pool-1-thread-13, num=667
pool-1-thread-13, num=668
pool-1-thread-9, num=669
pool-1-thread-9, num=670
pool-1-thread-9, num=671
pool-1-thread-9, num=672
pool-1-thread-9, num=673
pool-1-thread-9, num=674
pool-1-thread-9, num=675
pool-1-thread-9, num=676
pool-1-thread-9, num=677
pool-1-thread-9, num=678
pool-1-thread-20, num=679
pool-1-thread-20, num=680
pool-1-thread-20, num=681
pool-1-thread-20, num=682
pool-1-thread-20, num=683
pool-1-thread-20, num=684
pool-1-thread-20, num=685
pool-1-thread-20, num=686
pool-1-thread-20, num=687
pool-1-thread-20, num=688
pool-1-thread-17, num=689
pool-1-thread-17, num=690
pool-1-thread-17, num=691
pool-1-thread-17, num=692
pool-1-thread-17, num=693
pool-1-thread-17, num=694
pool-1-thread-17, num=695
pool-1-thread-17, num=696
pool-1-thread-17, num=697
pool-1-thread-17, num=698
pool-1-thread-14, num=699
pool-1-thread-14, num=700
pool-1-thread-14, num=701
pool-1-thread-14, num=702
pool-1-thread-14, num=703
pool-1-thread-14, num=704
pool-1-thread-14, num=705
pool-1-thread-14, num=706
pool-1-thread-14, num=707
pool-1-thread-14, num=708
pool-1-thread-4, num=709
pool-1-thread-4, num=710
pool-1-thread-4, num=711
pool-1-thread-4, num=712
pool-1-thread-4, num=713
pool-1-thread-4, num=714
pool-1-thread-4, num=715
pool-1-thread-4, num=716
pool-1-thread-4, num=717
pool-1-thread-4, num=718
pool-1-thread-10, num=719
pool-1-thread-10, num=720
pool-1-thread-10, num=721
pool-1-thread-10, num=722
pool-1-thread-10, num=723
pool-1-thread-10, num=724
pool-1-thread-10, num=725
pool-1-thread-10, num=726
pool-1-thread-10, num=727
pool-1-thread-10, num=728
pool-1-thread-2, num=729
pool-1-thread-2, num=730
pool-1-thread-2, num=731
pool-1-thread-2, num=732
pool-1-thread-2, num=733
pool-1-thread-2, num=734
pool-1-thread-2, num=735
pool-1-thread-2, num=736
pool-1-thread-2, num=737
pool-1-thread-2, num=738
pool-1-thread-1, num=739
pool-1-thread-1, num=740
pool-1-thread-1, num=741
pool-1-thread-1, num=742
pool-1-thread-1, num=743
pool-1-thread-1, num=744
pool-1-thread-1, num=745
pool-1-thread-1, num=746
pool-1-thread-1, num=747
pool-1-thread-1, num=748
pool-1-thread-21, num=749
pool-1-thread-21, num=750
pool-1-thread-21, num=751
pool-1-thread-21, num=752
pool-1-thread-21, num=753
pool-1-thread-21, num=754
pool-1-thread-21, num=755
pool-1-thread-21, num=756
pool-1-thread-21, num=757
pool-1-thread-21, num=758
pool-1-thread-15, num=759
pool-1-thread-15, num=760
pool-1-thread-15, num=761
pool-1-thread-15, num=762
pool-1-thread-15, num=763
pool-1-thread-15, num=764
pool-1-thread-15, num=765
pool-1-thread-15, num=766
pool-1-thread-15, num=767
pool-1-thread-15, num=768
pool-1-thread-18, num=769
pool-1-thread-18, num=770
pool-1-thread-18, num=771
pool-1-thread-18, num=772
pool-1-thread-18, num=773
pool-1-thread-18, num=774
pool-1-thread-18, num=775
pool-1-thread-18, num=776
pool-1-thread-18, num=777
pool-1-thread-18, num=778
pool-1-thread-11, num=779
pool-1-thread-11, num=780
pool-1-thread-11, num=781
pool-1-thread-11, num=782
pool-1-thread-11, num=783
pool-1-thread-11, num=784
pool-1-thread-11, num=785
pool-1-thread-11, num=786
pool-1-thread-11, num=787
pool-1-thread-11, num=788
pool-1-thread-22, num=789
pool-1-thread-22, num=790
pool-1-thread-22, num=791
pool-1-thread-22, num=792
pool-1-thread-22, num=793
pool-1-thread-22, num=794
pool-1-thread-22, num=795
pool-1-thread-22, num=796
pool-1-thread-22, num=797
pool-1-thread-22, num=798
pool-1-thread-5, num=799
pool-1-thread-5, num=800
pool-1-thread-5, num=801
pool-1-thread-5, num=802
pool-1-thread-5, num=803
pool-1-thread-5, num=804
pool-1-thread-5, num=805
pool-1-thread-5, num=806
pool-1-thread-5, num=807
pool-1-thread-5, num=808
pool-1-thread-23, num=809
pool-1-thread-23, num=810
pool-1-thread-23, num=811
pool-1-thread-23, num=812
pool-1-thread-23, num=813
pool-1-thread-23, num=814
pool-1-thread-23, num=815
pool-1-thread-23, num=816
pool-1-thread-23, num=817
pool-1-thread-23, num=818
pool-1-thread-12, num=819
pool-1-thread-12, num=820
pool-1-thread-12, num=821
pool-1-thread-12, num=822
pool-1-thread-12, num=823
pool-1-thread-12, num=824
pool-1-thread-12, num=825
pool-1-thread-12, num=826
pool-1-thread-12, num=827
pool-1-thread-12, num=828
pool-1-thread-7, num=829
pool-1-thread-7, num=830
pool-1-thread-7, num=831
pool-1-thread-7, num=832
pool-1-thread-7, num=833
pool-1-thread-7, num=834
pool-1-thread-7, num=835
pool-1-thread-7, num=836
pool-1-thread-7, num=837
pool-1-thread-7, num=838
pool-1-thread-24, num=839
pool-1-thread-24, num=840
pool-1-thread-24, num=841
pool-1-thread-24, num=842
pool-1-thread-24, num=843
pool-1-thread-24, num=844
pool-1-thread-24, num=845
pool-1-thread-24, num=846
pool-1-thread-24, num=847
pool-1-thread-24, num=848
pool-1-thread-24, num=849
pool-1-thread-24, num=850
pool-1-thread-24, num=851
pool-1-thread-24, num=852
pool-1-thread-24, num=853
pool-1-thread-24, num=854
pool-1-thread-24, num=855
pool-1-thread-24, num=856
pool-1-thread-24, num=857
pool-1-thread-24, num=858
pool-1-thread-19, num=859
pool-1-thread-19, num=860
pool-1-thread-19, num=861
pool-1-thread-19, num=862
pool-1-thread-19, num=863
pool-1-thread-19, num=864
pool-1-thread-19, num=865
pool-1-thread-19, num=866
pool-1-thread-19, num=867
pool-1-thread-19, num=868
pool-1-thread-3, num=869
pool-1-thread-3, num=870
pool-1-thread-3, num=871
pool-1-thread-3, num=872
pool-1-thread-3, num=873
pool-1-thread-3, num=874
pool-1-thread-3, num=875
pool-1-thread-3, num=876
pool-1-thread-3, num=877
pool-1-thread-3, num=878
pool-1-thread-25, num=879
pool-1-thread-25, num=880
pool-1-thread-25, num=881
pool-1-thread-25, num=882
pool-1-thread-25, num=883
pool-1-thread-25, num=884
pool-1-thread-25, num=885
pool-1-thread-25, num=886
pool-1-thread-25, num=887
pool-1-thread-25, num=888
pool-1-thread-8, num=889
pool-1-thread-8, num=890
pool-1-thread-8, num=891
pool-1-thread-8, num=892
pool-1-thread-8, num=893
pool-1-thread-8, num=894
pool-1-thread-8, num=895
pool-1-thread-8, num=896
pool-1-thread-8, num=897
pool-1-thread-8, num=898
pool-1-thread-6, num=899
pool-1-thread-6, num=900
pool-1-thread-6, num=901
pool-1-thread-6, num=902
pool-1-thread-6, num=903
pool-1-thread-6, num=904
pool-1-thread-6, num=905
pool-1-thread-6, num=906
pool-1-thread-6, num=907
pool-1-thread-6, num=908
pool-1-thread-13, num=909
pool-1-thread-13, num=910
pool-1-thread-13, num=911
pool-1-thread-26, num=912
pool-1-thread-26, num=914
pool-1-thread-26, num=915
pool-1-thread-26, num=916
pool-1-thread-26, num=917
pool-1-thread-26, num=918
pool-1-thread-26, num=919
pool-1-thread-26, num=920
pool-1-thread-26, num=921
pool-1-thread-26, num=922
pool-1-thread-13, num=913
pool-1-thread-13, num=923
pool-1-thread-13, num=924
pool-1-thread-13, num=925
pool-1-thread-13, num=926
pool-1-thread-13, num=927
pool-1-thread-13, num=928
pool-1-thread-9, num=929
pool-1-thread-9, num=930
pool-1-thread-9, num=931
pool-1-thread-9, num=932
pool-1-thread-9, num=933
pool-1-thread-9, num=934
pool-1-thread-9, num=935
pool-1-thread-9, num=936
pool-1-thread-9, num=937
pool-1-thread-9, num=938
pool-1-thread-20, num=939
pool-1-thread-20, num=940
pool-1-thread-20, num=941
pool-1-thread-20, num=942
pool-1-thread-20, num=943
pool-1-thread-20, num=944
pool-1-thread-20, num=945
pool-1-thread-20, num=946
pool-1-thread-20, num=947
pool-1-thread-20, num=948
pool-1-thread-17, num=949
pool-1-thread-17, num=950
pool-1-thread-17, num=951
pool-1-thread-17, num=952
pool-1-thread-17, num=953
pool-1-thread-17, num=954
pool-1-thread-17, num=955
pool-1-thread-17, num=956
pool-1-thread-17, num=957
pool-1-thread-17, num=958
pool-1-thread-27, num=959
pool-1-thread-27, num=960
pool-1-thread-27, num=961
pool-1-thread-27, num=962
pool-1-thread-27, num=963
pool-1-thread-27, num=964
pool-1-thread-27, num=965
pool-1-thread-27, num=966
pool-1-thread-27, num=967
pool-1-thread-27, num=968
pool-1-thread-14, num=969
pool-1-thread-14, num=970
pool-1-thread-14, num=971
pool-1-thread-14, num=972
pool-1-thread-14, num=973
pool-1-thread-14, num=974
pool-1-thread-14, num=975
pool-1-thread-14, num=976
pool-1-thread-14, num=977
pool-1-thread-14, num=978
pool-1-thread-4, num=979
pool-1-thread-4, num=980
pool-1-thread-4, num=981
pool-1-thread-4, num=982
pool-1-thread-4, num=983
pool-1-thread-4, num=984
pool-1-thread-4, num=985
pool-1-thread-4, num=986
pool-1-thread-4, num=987
pool-1-thread-4, num=988
pool-1-thread-10, num=989
pool-1-thread-10, num=990
pool-1-thread-10, num=991
pool-1-thread-10, num=992
pool-1-thread-10, num=993
pool-1-thread-10, num=994
pool-1-thread-10, num=995
pool-1-thread-10, num=996
pool-1-thread-10, num=997
pool-1-thread-10, num=998
์“ฐ๋ ˆ๋“œ ์ฝœ ์ข…๋ฃŒ
executor has been terminated

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

 

thread๊ฐ€ 1~27 ๊นŒ์ง€ 27๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋งŒ ์‹คํ–‰์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์™œ ์ด๋Ÿฐ ๊ฑธ๊นŒ์š”?

corePoolSize๋ฅผ 1๋กœ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์“ฐ๋ ˆ๋“œํ’€์€ 1๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋กœ ์‹œ์ž‘์„ ํ•˜๊ฒŒ๋˜๊ณ  ํ•ด๋‹น ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋๋‚˜๊ธฐ ์ „์— ์ƒˆ๋กœ์šด task์˜ ์‹คํ–‰ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์ƒˆ๋กœ์šด ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ƒˆ๋กœ์šด task์˜ ์‹คํ–‰ ์š”์ฒญ์ด ์ด์ „ task๊ฐ€ ์ข…๋ฃŒ๋œ ์ดํ›„์— ๋“ค์–ด์˜ค๊ฒŒ๋˜๋ฉด ์ƒˆ๋กœ์šด ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ์“ฐ๋ ˆ๋“œ ํ’€์„ ์‚ฌ์šฉํ•˜๋Š” ์žฅ์ ์ด์ฃ . ์“ฐ๋ ˆ๋“œ ํ’€์— ํ•„์š”ํ•œ ๊ฐœ์ˆ˜๋งŒํผ์˜ ์“ฐ๋ ˆ๋“œ๋งŒ ์ƒ์„ฑํ•˜๊ณ  ์žฌ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋ถˆํ•„์š”ํ•œ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“ฌ์œผ๋กœ์จ ์†Œ๋ชจ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ ThreadPoolExecutor๋ฅผ ํ™œ์šฉํ•œ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„์— ๋Œ€ํ•ด ๊ฐ„๋žตํ•œ ํฌ์ŠคํŒ…์„ ๋งˆ์นฉ๋‹ˆ๋‹ค.

 

Reference: ์˜ค๋ผํด Java 8 ๊ณต์‹ ๋ฌธ์„œ

 

์ถ”๊ฐ€๋กœ ํ˜„์žฌ Thread ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•œ Task์—์„œ num ๋ณ€์ˆ˜๋ฅผ 1์”ฉ ์ฆ๊ฐ€ํ•ด์„œ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค๋งŒ, ์‹ค์ œ๋กœ ๋งˆ์ง€๋ง‰ ์ˆซ์ž๋ฅผ ๋ณด๋ฉด 999๊ฐ€ ์•„๋‹Œ 998์ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•ด๋ณด๋ฉด ๊ฐ€์žฅ ํฐ ์ˆซ์ž๊ฐ€ 99X~999๊ฐ€ ๋žœ๋คํ•˜๊ฒŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ num์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋ชจ๋“  ์“ฐ๋ ˆ๋“œ๊ฐ€ ๊ณต์œ ํ•˜๋Š”๋ฐ ๊ฐ’์„ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ์˜คํผ๋ ˆ์ด์…˜์ด thread safeํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ณ  ์žˆ์ง€ ์•Š์Œ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด volatileํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ž๋ฐ” ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

์ž๋ฐ”์—์„œ ์“ฐ๋ ˆ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ฐ„๋žตํ•˜๊ฒŒ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค.

 

์ž๋ฐ”์—์„œ ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ๋Š” Runnable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ Thread ์ƒ์„ฑ์ž๋กœ ํ•ด๋‹น ๊ตฌํ˜„์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๊ณ ,

๋‘ ๋ฒˆ์งธ๋Š” ์ง์ ‘ Thread ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์šฐ์„  ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ  ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        Runnable task = new Task();
        Thread thread = new Thread(task);
        thread.start();
    }
    static class Task implements Runnable {

        int num = 0;

        @Override
        public void run() {
        	for(int i = 0; i < 10; i++) {
	            System.out.println(num++);
            }
        }
    }

 

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค.

	0
	1
	2
	3
	4
	5
	6
	7
	8
	9

 

์ด๋ฒˆ์—๋Š” Thread ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜์—ฌ ๋™์ผํ•œ ์ž‘์—…(task)์„ ํ•˜๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        ThreadTask task = new ThreadTask();
        task.run();        
    }
    static class ThreadTask extends Thread {

        int num = 0;

        @Override
        public void run() {
        	for(int i = 0; i < 10; i++) {
	            System.out.println(num++);
    	    }
        }
    }

 

์‹คํ–‰ํ•˜๋ฉด Runnable๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ์™€ ๋™์ผํ•˜๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค.

	0
	1
	2
	3
	4
	5
	6
	7
	8
	9

 

์ฐธ๊ณ ๋กœ ์‹ค์ œ Thread ํด๋ž˜์Šค๋ฅผ ์—ด์–ด๋ณด๋ฉด Runnable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ implementํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

public class Thread implements Runnable {
    /* Make sure registerNatives is the first thing <clinit> does. */
    private static native void registerNatives();
    static {
        registerNatives();
    }

    private volatile String name;
    private int            priority;
    private Thread         threadQ;
    private long           eetop;
.
.
.

 

๊ทธ๋Ÿผ ์ด๋ฒˆ์—๋Š” ์“ฐ๋ ˆ๋“œ ๋‘ ๊ฐœ๋ฅผ ๋™์‹œ์— ๋Œ๋ ค๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        Runnable task = new Task();

        Thread thread1 = new Thread(task);
        thread1.start();
        System.out.println("thread 1 executed.");

        Thread thread2 = new Thread(task);
        thread2.start();
        System.out.println("thread 2 executed.");

        System.out.println("์“ฐ๋ ˆ๋“œ ์ฝœ ์ข…๋ฃŒ");
    }

    static class Task implements Runnable {

        int num = 0;

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + ", num=" + num++);
            }
        }
    }

 

์ค‘๊ฐ„์ค‘๊ฐ„์— ์–ด๋–ป๊ฒŒ ์‹คํ–‰์ด ๋˜๋Š”์ง€ ํ™•์ธ์„ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ System.out.println์œผ๋กœ ๋กœ๊น…์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

thread 1 executed.
Thread-0, num=0
Thread-0, num=1
Thread-0, num=2
Thread-0, num=3
Thread-0, num=4
Thread-0, num=5
thread 2 executed.
์“ฐ๋ ˆ๋“œ ์ฝœ ์ข…๋ฃŒ
Thread-0, num=6
Thread-1, num=7
Thread-1, num=9
Thread-0, num=8
Thread-1, num=10
Thread-0, num=11
Thread-1, num=12
Thread-1, num=13
Thread-1, num=14
Thread-1, num=16
Thread-1, num=17
Thread-1, num=18
Thread-1, num=19
Thread-0, num=15

BUILD SUCCESSFUL in 0s
2 actionable tasks: 1 executed, 1 up-to-date

 

์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋จผ์ € ์‹œ์ž‘๋˜์ง€๋งŒ "task1 executed"๊ฐ€ ๋จผ์ € ์ถœ๋ ฅ์ด ๋˜์—ˆ๊ณ  ๊ทธ ๋’ค์— ์ฒซ ๋ฒˆ์งธ ์“ฐ๋ ˆ๋“œ์ธ Thread-0์ด ์ถœ๋ ฅ์„ ์‹œ์ž‘ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ค‘๊ฐ„์— "task2 executed"๊ฐ€ ์ถœ๋ ฅ๋œ ๊ฑธ ๋ณด๋‹ˆ ๋‘ ๋ฒˆ์งธ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰์ด ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์ฃ . ๋‘ ๋ฒˆ์งธ ์“ฐ๋ ˆ๋“œ๋Š” Thread-1 ์ด๋ฉฐ ์ฒซ ๋ฒˆ์งธ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์ผ์„ ๋งˆ์น˜๊ธฐ ์ „์— ์‹œ์ž‘๋˜์–ด "Thread-1, num=0"์„ ์ถœ๋ ฅํ•œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค(main ํ”„๋กœ์„ธ์Šค) ์•ˆ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์—…์„ ๋™์‹œ์— ๋‚˜๋ˆ„์–ด ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์ด ์“ฐ๋ ˆ๋“œ์ž…๋‹ˆ๋‹ค.

์“ฐ๋ ˆ๋“œ๋กœ ์ž‘์—…์„ ํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ง•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์—ฌ๋Ÿฌ ์ž‘์—…์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด ์ž‘์—…์„ ์™„๋ฃŒํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ด ์†Œ์š” ์‹œ๊ฐ„์ด ์ค„์–ด๋“ ๋‹ค. (๋ฉ”์ธ์“ฐ๋ ˆ๋“œ 1๊ฐœ๋กœ ์ž‘์—…ํ–ˆ์„ ๋•Œ์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ)
  • ๋จผ์ € ์‹œ์ž‘ํ•œ ์“ฐ๋ ˆ๋“œ๊ฐ€ ํ•ญ์ƒ ๋จผ์ € ์ผ์„ ๋๋‚ด์ง€๋Š” ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ, ์ž‘์—…์˜ ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•  ๋•Œ์—๋Š” ์“ฐ๋ ˆ๋“œ๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

 

์ž, ์ด์ œ ์‹ฑ๊ธ€ ์“ฐ๋ ˆ๋“œ๋กœ 10์‹œ๊ฐ„ ๊ฑธ๋ฆด ์ผ์„ 100๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋Œ๋ ค์„œ 10๋ถ„๋งŒ์— ๋๋‚ด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”?

์“ฐ๋ ˆ๋“œ๋ฅผ 100๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”๋ฐ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฌด์‹ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ• ๊นŒ์š”?

        Thread thread1 = new Thread(task);
        Thread thread2 = new Thread(task);
        Thread thread3 = new Thread(task);
        Thread thread4 = new Thread(task);
        Thread thread5 = new Thread(task);
        .
        .
        .

 

์•„๋‹™๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์•ผ๋˜๋ฉด ์“ฐ๋ ˆ๋“œ ๋งŒ๋“ค๋‹ค๊ฐ€ ํ‡ด๊ทผํ•ด์•ผ๋ฉ๋‹ˆ๋‹ค.

์ž๋ฐ”์—์„œ๋Š” java.util.concurrentํŒจํ‚ค์ง€์— ๋™์‹œ์ž‘์—…์„ ์œ„ํ•ด ์œ ์šฉํ•œ ํด๋ž˜์Šค๋“ค์„ ๋ชจ์•„๋†จ๋Š”๋ฐ ๊ทธ์ค‘์— ThreadPoolExecutor๋ผ๋Š” ๋…€์„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋…€์„์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‘ ์„ธ ์ค„์ด๋ฉด 100๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋Œ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์ฃ .

ThreadPoolExecutor์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๋ ค๋ฉด ํฌ์ŠคํŠธ๊ฐ€ ๊ธธ์–ด์ง€๋‹ˆ ThreadPoolExecutor๋ฅผ ์ด์šฉํ•œ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„์„œ ๊ณ„์† ์ด์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.

 

AWS Beanstalk

AWS ๋นˆ์Šคํ†ก์„ ์ด์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋„์šฐ๊ณ 

๋ถ„์‚ฐ์‹œ์Šคํ…œ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด ๊ฐœ๋ฐœ์„ ํ•  ๋•Œ

๊ฐ ๋นˆ์Šคํ†ก ์ธ์Šคํ„ด์Šค์˜ private IP๊ฐ€ ํ•„์š”ํ•  ๊ฒฝ์šฐ 

AWS SDK๋ฅผ ์ด์šฉํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

public List<String> getBeanstalkIps() {
	return findIpsByIds(findIds(getBeanstalkResources()));
}

private DescribeEnvironmentResourcesResult getBeanstalkResources() {
	DescribeEnvironmentResourcesRequest request =
		new DescribeEnvironmentResourcesRequest()
        		.withEnvironmentName("my-beanstalk-app");
	return beanstalk.describeEnvironmentResources(request);
}

private List<String> findIds(DescribeEnvironmentResourcesResult resources) {
	return resources.getEnvironmentResources().getInstances().stream()
			.map(Instance::getId)
			.collect(toList());
}

private List<String> findIpsByIds(List<String> ids) {
	DescribeInstancesRequest request = 
    	new DescribeInstancesRequest().withInstanceIds(ids);
	DescribeInstancesResult result = ec2.describeInstances(request);
	return result.getReservations().stream()
			.flatMap(n -> n.getInstances().stream())
			.map(com.amazonaws.services.ec2.model.Instance::getPrivateIpAddress)
			.sorted()
			.collect(toList());
}

 

์—ฌ๊ธฐ์„œ ํ•„์š”ํ•œ ๊ฒƒ์€ "my-beanstalk-app"์„ ๋Œ€์ฒดํ•  ๋นˆ์Šคํ†ก ํ™˜๊ฒฝ๋ช…์ธ๋ฐ

์ด ์ด๋ฆ„์€ AWS Console > Elastic Beanstalk > (์• ํ”Œ๋ฆฌ์ผ€์ด์…˜) > Environment ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

AWS SDK๋Š” 2018๋…„ 11์›” 2.x ๋ฒ„์ „์ด ๋‚˜์™”์œผ๋‚˜ ์•„์ง 1.11 ๋ฒ„์ „์„ ์‚ฌ์šฉ ์ค‘์ด๋‹ค.

SDK 2.x๋กœ ๊ตฌํ˜„์‹œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

-- TODO --

๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์‹œ ๋กœ๊น… uuid ์„ค์ •

ํŠธ๋ž˜ํ”ฝ์ด ๋งŽ์€ ํ™˜๊ฒฝ์—์„œ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๊ฐ ์“ฐ๋ ˆ๋“œ์— uuid ์„ค์ •์ด ์•ˆ๋˜์–ด ์žˆ์–ด ๋กœ๊ทธ์ƒ์œผ๋กœ ํ™•์ธ์ด ๋ถˆ๊ฐ€ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ฒผ๋‹ค. ๊ธฐ์กด์—๋Š” ์ž˜ ๋˜๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ์ด์—ˆ๊ณ  ๋ญ”๊ฐ€ ์ˆ˜์ •์ž‘์—…์„ ํ•˜๋ฉด์„œ ๋นผ๋œจ๋ฆฐ ๊ฒƒ ๊ฐ™์•„ ๋‹ค๋ฅธ ์“ฐ๋ ˆ๋“œ๋“ค์˜ ์„ค์ •์„ ๋ณด๋‹ค๊ฐ€ TaskDecorator๋ฅผ ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.

 

package com.keichee;

import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;

import java.util.Map;

public class LoggingTaskDecorator implements TaskDecorator {

    @Override
    public Runnable decorate(Runnable task) {
        Map<String, String> callerThreadContext = MDC.getCopyOfContextMap();
        return () -> {
            MDC.setContextMap(callerThreadContext);
            task.run();
        };
    }
}

 

LoggingTaskDecorator๋Š” TaskDecorator๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š”๋ฐ decorate(Runnable task) ๋ฉ”์„œ๋“œ๋งŒ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์—ฌ๊ธฐ์„œ TaskDecorator๋Š” ์–ด๋–ค task(์ž‘์—…)์„ ๊พธ๋ฉฐ์ค€๋‹ค๋Š” ์˜๋ฏธ๋กœ ์ง€์–ด์ง„ ์ด๋ฆ„์ด๋ฉฐ ์Šคํ”„๋ง์˜ ์ฝ”์–ด ํŒจํ‚ค์ง€์— ๋“ค์–ด์žˆ์„ ๋งŒํผ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋…€์„์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์Šคํ”„๋ง ๊ณต์‹ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด TaskDecorator๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์ด ๋˜์–ด์žˆ๋‹ค.

 

Functional Interface : This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.@FunctionalInterface public interface TaskDecorator

A callback interface for a decorator to be applied to any Runnable about to be executed.

Note that such a decorator is not necessarily being applied to the user-supplied Runnable/Callable but rather to the actual execution callback (which may be a wrapper around the user-supplied task).

The primary use case is to set some execution context around the task's invocation, or to provide some monitoring/statistics for task execution.

Since:4.3

 

์ž, ๊ทธ๋Ÿผ TaskDecorator๋ฅผ ๊ตฌํ˜„ํ•œ ์ด LoggingTaskDecorator๋Š” ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ• ๊นŒ?

๊ฐ„๋‹จํ•˜๋‹ค. ์Šคํ”„๋ง์—์„œ ์ œ๊ณตํ•˜๋Š” ThreadPoolTaskExecutor๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด setTaskDecorator() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์„ธํŒ…ํ•ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
        t.setTaskDecorator(new LoggingTaskDecorator());
        return t;
}

 

 

async(๋น„๋™๊ธฐ) ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ThreadPoolTaskExecutor

ThreadPoolTaskExecutor๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ThreadPoolTaskExecutor๋Š” ์Šคํ”„๋ง์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋กœ org.springframework.scheduling.concurrent ํŒจํ‚ค์ง€์— ์†ํ•ด์žˆ์Šต๋‹ˆ๋‹ค.

์ƒ์„ฑ์ž๋„ ๊ธฐ๋ณธ์ƒ์„ฑ์ž ํ•˜๋‚˜๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ์“ฐ๋ ˆ๋“œํ’€์„ ์ด์šฉํ•˜์—ฌ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„์„ ์‰ฝ๊ฒŒ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

	public static void main(String[] args) {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.initialize();
	}

ThreadPoolTaskExecutor ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก initialize()๋ฅผ ํ˜ธ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค. ์™œ๋ƒ๋ฉด ์ด๋‹ˆ์…œ๋ผ์ด์ฆˆํ•˜๊ธฐ ์ „์—๋Š” executor๋ฅผ ์‚ฌ์šฉ์„ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด๋‹ˆ์…œ๋ผ์ด์ฆˆ ํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

Exception in thread "main" java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized

 

๊ทธ๋Ÿผ ์ด์ œ ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ์ข€ ๋” ์ถ”๊ฐ€ํ•œ ๋’ค ์‹ค์ œ๋กœ ์“ฐ๋ ˆ๋“œ๋ฅผ ์‹คํ–‰์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

	public static void main(String[] args) {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.initialize();
		
		log.info("executing threads....");
		Runnable r = () -> {
			try {
				log.info(Thread.currentThread().getName() + ", Now sleeping 10 seconds...");
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		};

		for (int i = 0; i < 10; i++) {
			executor.execute(r);
		}
	}

 

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ•œ๋ฒˆ ๋ณผ๊นŒ์š”?

07:42:09.450 [main] INFO com.keichee.test.service.TestService - executing threads....
07:42:09.460 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:19.464 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:29.465 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:39.470 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:49.472 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:42:59.477 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:43:09.483 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:43:19.489 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:43:29.491 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
07:43:39.496 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...

๋กœ๊ทธ๊ฐ€ ์ถœ๋ ฅ๋œ ์‹œ๊ฐ„์„ ๋ณด๋ฉด 10์ดˆ๋งˆ๋‹ค ์ถœ๋ ฅ์ด ๋˜๊ณ ์žˆ๊ณ  ์“ฐ๋ ˆ๋“œ๋„ ThreadPoolTaskExecutor-1 ํ•˜๋‚˜๊ฐ€ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ง€๊ธˆ ์œ„ ์ฝ”๋“œ๋Š” ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋กœ ๋Œ์•„๊ฐ„๊ฒŒ ์•„๋‹ˆ๋ž€ ์–˜๊ธฐ์ฃ . ThreadPoolTaskExecutor๋Š” ๋ช‡ ๊ฐ€์ง€ ์„ค์ •๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ค‘ corePoolSize ๊ฐ’์ด ๋™์‹œ์— ์‹คํ–‰ํ•  ์“ฐ๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ ์ด ์„ค์ •์˜ default ๊ฐ’์ด 1๋กœ ์„ธํŒ…๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ์ฒ˜๋Ÿผ corePoolSize ์„ค์ •์—†์ด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์‹ฑ๊ธ€์“ฐ๋ ˆ๋“œ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

์„ค์ •๊ฐ’

๊ทธ๋Ÿผ ์„ค์ •๊ฐ’๋“ค์— ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€ ๊ทธ๊ฒƒ๋“ค์ด ์˜๋ฏธํ•˜๋Š”๊ฒŒ ๋ฌด์—‡์ธ์ง€ setter ๋ฉ”์„œ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ค‘์š”ํ•œ ๊ฐ’๋“ค๋งŒ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฉ”์„œ๋“œ

์„ค๋ช…

๊ธฐ๋ณธ๊ฐ’

setCorePoolSize

corePoolSize ๊ฐ’์„ ์„ค์ •ํ•จ. corePoolSize๋Š” ๋™์‹œ์— ์‹คํ–‰์‹œํ‚ฌ ์“ฐ๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•จ

1

setAllowCoreThreadTimeOut

์ฝ”์–ด ์“ฐ๋ ˆ๋“œ์˜ ํƒ€์ž„์•„์›ƒ์„ ํ—ˆ์šฉํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์„ธํ„ฐ ๋ฉ”์„œ๋“œ. true๋กœ ์„ค์ •ํ•  ๊ฒฝ์šฐ ์ฝ”์–ด ์“ฐ๋ ˆ๋“œ๋ฅผ 10์œผ๋กœ ์„ค์ •ํ–ˆ์–ด๋„ ์ผ์ • ์‹œ๊ฐ„(keepAliveSeconds)์ด ์ง€๋‚˜๋ฉด ์ฝ”์–ด ์“ฐ๋ ˆ๋“œ ๊ฐœ์ˆ˜๊ฐ€ ์ค„์–ด๋“ฆ.

false

setKeepAliveSeconds

์ฝ”์–ด ์“ฐ๋ ˆ๋“œ ํƒ€์ž„์•„์›ƒ์„ ํ—ˆ์šฉํ–ˆ์„ ๊ฒฝ์šฐ ์‚ฌ์šฉ๋˜๋Š” ์„ค์ •๊ฐ’์œผ๋กœ, ์—ฌ๊ธฐ ์„ค์ •๋œ ์‹œ๊ฐ„์ด ์ง€๋‚  ๋•Œ๊นŒ์ง€ ์ฝ”์–ด ์“ฐ๋ ˆ๋“œ ํ’€์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ํ•ด๋‹น ์“ฐ๋ ˆ๋“œ๋Š” terminate ๋œ๋‹ค.

60์ดˆ

setMaxPoolSize

์“ฐ๋ ˆ๋“œ ํ’€์˜ ์ตœ๋Œ€ ์‚ฌ์ด์ฆˆ

Integer.MAX

setQueueCapacity

์“ฐ๋ ˆ๋“œ ํ’€ ํ์˜ ์‚ฌ์ด์ฆˆ. corePoolSize ๊ฐœ์ˆ˜๋ฅผ ๋„˜์–ด์„œ๋Š” task๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ queue์— ํ•ด๋‹น task๋“ค์ด ์Œ“์ด๊ฒŒ ๋œ๋‹ค. ์ตœ๋Œ€๋กœ maxPoolSize ๊ฐœ์ˆ˜ ๋งŒํผ ์Œ“์ผ ์ˆ˜ ์žˆ๋‹ค.

Integer.MAX

 

์—ฌ๊ธฐ์„œ corePoolSize, maxPoolSize, queueCapacity ์ด ์„ธ ๊ฐ€์ง€ ์„ค์ •๊ฐ’์ด ๊ฐ€์žฅ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

์šฐ์„  ์œ„์—์„œ ๋ดค๋˜ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ๋Š” ์ด ์„ธ ๊ฐ€์ง€ ๊ฐ’์— ๋Œ€ํ•ด์„œ ๋ณ„๋„๋กœ ์„ค์ •์„ ํ•˜์ง€ ์•Š์•˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‹ฑ๊ธ€ ์“ฐ๋ ˆ๋“œ๋กœ ์ž‘์—…์ด ์ด๋ฃจ์–ด์กŒ์ฃ .

 

corePoolSize

์ด๋ฒˆ์—๋Š” corePoolSize๋ฅผ ์˜ฌ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.initialize();

        log.info("executing threads....");
        Runnable r = () -> {
            try {
                log.info(Thread.currentThread().getName() + ", Now sleeping 10 seconds...");
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 10; i++) {
            executor.execute(r);
        }
    }

 

corePoolSize๋ฅผ 5๋กœ ์„ค์ • ํ›„ ์‹คํ–‰ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. (queueCapacity์™€ maxPoolSize๊ฐ’์€ ํ˜„์žฌ ๊ธฐ๋ณธ๊ฐ’์ธ Integer.MAX ์ž…๋‹ˆ๋‹ค)

08:52:50.423 [main] INFO com.keichee.test.service.TestService - executing threads....
08:52:50.456 [ThreadPoolTaskExecutor-3] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-3, Now sleeping 10 seconds...
08:52:50.456 [ThreadPoolTaskExecutor-2] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-2, Now sleeping 10 seconds...
08:52:50.456 [ThreadPoolTaskExecutor-5] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-5, Now sleeping 10 seconds...
08:52:50.456 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
08:52:50.456 [ThreadPoolTaskExecutor-4] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-4, Now sleeping 10 seconds...
08:53:00.460 [ThreadPoolTaskExecutor-1] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-1, Now sleeping 10 seconds...
08:53:00.460 [ThreadPoolTaskExecutor-2] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-2, Now sleeping 10 seconds...
08:53:00.461 [ThreadPoolTaskExecutor-3] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-3, Now sleeping 10 seconds...
08:53:00.461 [ThreadPoolTaskExecutor-4] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-4, Now sleeping 10 seconds...
08:53:00.461 [ThreadPoolTaskExecutor-5] INFO com.keichee.test.service.TestService - ThreadPoolTaskExecutor-5, Now sleeping 10 seconds...

์‹คํ–‰๋˜์ž๋งˆ์ž 5๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ณ  10์ดˆ ํ›„์— ๋˜ ๋‹ค๋ฅธ 5๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, queueCapacity์™€ maxPoolSize๊ฐ’์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ํ•ด๋†“์œผ๋ฉด corePoolSize์˜ ๊ฐ’๋งŒํผ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

corePoolSize์™€ queueCapacity

๊ทธ๋Ÿผ ์ด๋ฒˆ์—๋Š” corePoolSize๋Š” default ๊ฐ’์ธ 1๋กœ ๋†”๋‘๊ณ  queueCapacity์™€ maxPoolSize ๊ฐ’์„ 5๋กœ ์„ค์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  10๊ฐœ์˜ task๊ฐ€ ์‹คํ–‰๋  ๋•Œ poolSize, activeSize, queueSize ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ถœ๋ ฅํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋˜, ์ถœ๋ ฅ์„ ์ข€ ์งง๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด์„œ ์“ฐ๋ ˆ๋“œ๋ช… prefix๋ฅผ "my-"๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("my-");
        executor.setQueueCapacity(5);
        executor.setMaxPoolSize(5);
        executor.initialize();

        log.info("executing threads....");
        Runnable r = () -> {
            try {
                log.info(Thread.currentThread().getName() + ", Now sleeping 10 seconds...");
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 10; i++) {
            executor.execute(r);
            System.out.print("poolSize:" + executor.getPoolSize());
            System.out.print(", active:" + executor.getActiveCount());
            System.out.println(", queue:" + executor.getThreadPoolExecutor().getQueue().size());
        }
    }

 

์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ ๊ฐ™์€์ง€ ํ•œ๋ฒˆ ์ƒ๊ฐ์„ ํ•ด๋ณด๊ณ  ์•„๋ž˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

09:02:22.864 [main] INFO com.keichee.test.service.TestService - executing threads....
poolSize:1, active:1, queue:0
poolSize:1, active:1, queue:1
poolSize:1, active:1, queue:2
poolSize:1, active:1, queue:3
poolSize:1, active:1, queue:4
poolSize:1, active:1, queue:5
poolSize:2, active:2, queue:5
09:02:22.886 [my-1] INFO com.keichee.test.service.TestService - my-1, Now sleeping 10 seconds...
09:02:22.887 [my-2] INFO com.keichee.test.service.TestService - my-2, Now sleeping 10 seconds...
poolSize:3, active:3, queue:5
09:02:22.887 [my-3] INFO com.keichee.test.service.TestService - my-3, Now sleeping 10 seconds...
poolSize:4, active:4, queue:5
09:02:22.887 [my-4] INFO com.keichee.test.service.TestService - my-4, Now sleeping 10 seconds...
poolSize:5, active:5, queue:5
09:02:22.887 [my-5] INFO com.keichee.test.service.TestService - my-5, Now sleeping 10 seconds...
09:02:32.888 [my-1] INFO com.keichee.test.service.TestService - my-1, Now sleeping 10 seconds...
09:02:32.890 [my-2] INFO com.keichee.test.service.TestService - my-2, Now sleeping 10 seconds...
09:02:32.890 [my-4] INFO com.keichee.test.service.TestService - my-4, Now sleeping 10 seconds...
09:02:32.890 [my-5] INFO com.keichee.test.service.TestService - my-5, Now sleeping 10 seconds...
09:02:32.890 [my-3] INFO com.keichee.test.service.TestService - my-3, Now sleeping 10 seconds...

 

 

์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด 10๊ฐœ์˜ task๋ฅผ ์‹คํ–‰ํ•  ๋•Œ queue ์‚ฌ์ด์ฆˆ๊ฐ€ 0์—์„œ 5๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ€๊ณ  ๊ทธ ์ดํ›„์— poolSize์™€ active ์‚ฌ์ด์ฆˆ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. corePoolSize๊ฐ€ 1 ์ด๋ผ์„œ 2๋ฒˆ์งธ task๋ถ€ํ„ฐ 6๋ฒˆ์งธ task๊นŒ์ง€๋Š” queue์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  7๋ฒˆ์งธ task๋ถ€ํ„ฐ 10๋ฒˆ์งธ task๊นŒ์ง€ 4๊ฐœ์˜ task๋Š” maxPoolSize ๋ฅผ ๋„˜์–ด์„œ์ง€ ์•Š๋Š” ํ•œ ์ถ”๊ฐ€๋กœ ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ pool์— ๋„ฃ๊ณ  ํ•ด๋‹น ์“ฐ๋ ˆ๋“œ๋กœ ๊ฐ task๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

๊ทธ๋Ÿผ ๋งŒ์•ฝ maxPoolSize๋ฅผ ๋„˜์–ด์„ค ๋งŒํผ ๋งŽ์€ ์–‘์˜ task๋“ค์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

Exception in thread "main" org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@32eff876[Running, pool size = 5, active threads = 5, queued tasks = 5, completed tasks = 0]] did not accept task: com.keichee.test.service.TestService$$Lambda$22/0x00000008000ce840@5e0826e7
	at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:324)

TaskRejectedException ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

๋”ฐ๋ผ์„œ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์–‘์˜ task๋ฅผ ์†Œํ™”ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ •ํ™•ํžˆ ์•Œ๊ณ  corePoolSize, queueCapacity, maxPoolSize์— ์ ์ ˆํ•œ ๊ฐ’์„ ์„ธํŒ…ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด TaskRejectedException์„ ๋ณผ ์ผ์€ ๊ฑฐ์˜ ์—†๊ฒ ์ง€๋งŒ ๊ทธ ๋Œ€์‹  queue์— ์–ด๋งˆ์–ด๋งˆํ•œ ์–‘์˜ task๊ฐ€ ์Œ“์ด๊ฒ ์ฃ . ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฐฐํฌ๋‚˜ ์–ด๋–ค ์ด์œ ์— ์˜ํ•ด์„œ ์žฌ๊ธฐ๋™์ด ํ•„์š”ํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น queue์— ์Œ“์—ฌ์žˆ๋˜ task๋“ค์€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ์‚ฌ์šฉํ•˜์‹ค ๋ถ„๋“ค์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ThreadPoolTaskExecutor๋ฅผ ์„ค์ • ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

 

์Šคํ”„๋ง RestTemplate ํƒ€์ž„์•„์›ƒ ์„ค์ •์„ ํ•˜๋Š”๋ฐ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์ด ์„ค์ •ํ•œ๋Œ€๋กœ ์ ์šฉ๋˜์ง€ ์•Š๋Š” ๋“ฏ ํ•˜์—ฌ ํ…Œ์ŠคํŠธ ํ•ด๋ด„..

๋ณดํ†ต HttpComponentsClientHttpRequestFactory ์™€ SimpleClientHttpRequestFactory ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์ •์„ ํ•จ

์Šคํ”„๋ง์˜ RestTemplate ๊ธฐ๋ณธ์ ์œผ๋กœ SimpleClientHttpRequestFactory๋ฅผ ์‚ฌ์šฉํ•จ

SimpleClientHttpRequestFactory๋ฅผ ์ด์šฉํ•ด์„œ ์„ค์ •์„ ํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ์„ธํŒ…ํ•œ ๊ฐ’์— ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•จ.

ํ•˜์ง€๋งŒ HttpComponentsClientHttpRequestFactory ๋ฅผ ์ด์šฉํ•˜๋ฉด ์„ค์ •ํ•œ ์‹œ๊ฐ„๋ณด๋‹ค 4๋ฐฐ ๊ธด ์‹œ๊ฐ„์ด ํ๋ฅธ ๋’ค์—์•ผ ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•˜์˜€์Œ

@Test
public void ํƒ€์ž„์•„์›ƒ_ํ…Œ์ŠคํŠธ() {

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout((int) TimeUnit.SECONDS.toMillis(10));
factory.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(5)); // 4๋ฐฐ์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ ๋’ค์—์•ผ ํƒ€์ž„์•„์›ƒ ๋ฐœ์ƒ
factory.setConnectionRequestTimeout(5 * 1000);

// SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
// factory.setReadTimeout((int) TimeUnit.SECONDS.toMillis(10));
// factory.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(5)); // ์„ธํŒ…ํ•œ ์‹œ๊ฐ„๋Œ€๋กœ ํƒ€์ž„์•„์›ƒ ๋ฐœ์ƒ

// RequestConfig config = RequestConfig.custom()
// .setSocketTimeout((int) TimeUnit.SECONDS.toMillis(1))
// .setConnectTimeout((int) TimeUnit.SECONDS.toMillis(5))
// .setConnectionRequestTimeout((int) TimeUnit.SECONDS.toMillis(10))
// .build();
// CloseableHttpClient client = HttpClientBuilder
// .create()
// .setDefaultRequestConfig(config)
// .build();
// HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);

RestTemplate restTemplate = new RestTemplate(factory);

// 1. connect timeout ํ…Œ์ŠคํŠธ
long start = System.currentTimeMillis();
try {
ResponseEntity<String> result = restTemplate.getForEntity("https://abc.com:81/test", String.class);
} catch (ResourceAccessException e) {
log.error("ํƒ€์ž„์•„์›ƒ!! {}", TimeUtils.printDuration(System.currentTimeMillis() - start), e);
}

}


์•„์ง ์›์ธ์€ ํ™•์ธํ•˜์ง€ ๋ชปํ•จ.


push๊นŒ์ง€ ํ•ด๋ฒ„๋ฆฐ git commit ์›๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•


๊ณต๋™ repository์— push๊นŒ์ง€ ํ•ด๋ฒ„๋ฆฐ commit์„ ๋˜๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์€ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ปค๋ฐ‹ ๊ฐœ์ˆ˜๊ฐ€ ํ•œ ๋‘ ๊ฑด ์ผ ๋•Œ git revert๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ณ , ์ปค๋ฐ‹ ๊ฐœ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„ ํ•˜๋‚˜์”ฉ revertํ•˜๊ธฐ ํž˜๋“  ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” git revert๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์›๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค.

1. commit log ํ™•์ธ

2. reset (์›ํ•˜๋Š” ์‹œ์ ์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๊ธฐ)

3. revert (ํŠน์ • ์‹œ์  ์ดํ›„์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ ๋˜๋Œ๋ฆฌ๊ธฐ)

4. force push (๋˜๋Œ๋ฆฐ ๋‚ด์šฉ์„ ๊ณต๋™ repo์— ๋ฐ˜์˜ํ•˜๊ธฐ)


1. commit history์—์„œ ์›๋ณต ์‹œ์ ์˜ ์ปค๋ฐ‹ ํ™•์ธ

keichee$ git log -5 --pretty=format:"%h - %an, %ar : %s"

148444a6a - keichee, 4 days ago : Merge branch 'dev-sentry' into stage

eebdd9202 - keichee, 4 days ago : dev ํ™˜๊ฒฝ ๋กœ๊น… sentry ์—ฐ๋™

1c74ca53e - john, 5 days ago : Merge pull request #1238 in test/repo from release/200202 to stage

6544cd10a - john, 5 days ago : Merge pull request #1237 in test/repo from feature/200202 to release

754046d47 - tom, 5 days ago : test commit 1


git log ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด์„œ commit ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ ์˜ต์…˜์œผ๋กœ -5 ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ์ตœ์‹ ์ˆœ์œผ๋กœ 5๊ฐœ ๊นŒ์ง€๋งŒ ์กฐํšŒ๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ณ  

์˜ต์…˜์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ๋˜์–ด ๊ณ„์† ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋’ค์— pretty ์˜ต์…˜์„ ์ฃผ๋ฉด ์œ„ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ ์กฐํšŒ๊ฐ€ ๋˜๋Š”๋ฐ pretty ์˜ต์…˜์„ ์•ˆ์ฃผ๋ฉด commit ํ•˜๋‚˜์˜ ๋‚ด์šฉ์ด ์ด 6์ค„์— ๊ฑธ์ณ์„œ ํ‘œ์‹œ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ˆˆ์— ๋ณด๊ธฐ๊ฐ€ ํž˜๋“ง๋‹ˆ๋‹ค.


2. ์›ํ•˜๋Š” ์‹œ์ ์œผ๋กœ Reset 

์œ„์—์„œ ์กฐํšŒํ•œ 5๊ฐœ์˜ ์ปค๋ฐ‹๋“ค ์ค‘์— john์ด 5์ผ ์ „์— ์ปค๋ฐ‹ํ•œ ๋‚ด์šฉ๊นŒ์ง€๋งŒ ์ ์šฉํ•˜๊ณ  keichee๊ฐ€ ์ปค๋ฐ‹ํ•œ ๋‚ด์šฉ์„ revert์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

commit it๊ฐ€ 1c74ca53e์ธ ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด reset ํ•ฉ๋‹ˆ๋‹ค.

keichee$ git reset 1c74ca53e

Unstaged changes after reset:

M       src/main/resources/logback-spring.xml


3. Revert

reset์„ ํ•˜๋ฉด 1c74ca53e ์ปค๋ฐ‹๊นŒ์ง€ ์™„๋ฃŒ๋œ ์ƒํƒœ๋กœ ๋˜๋Œ์•„ ๊ฐ‘๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ดํ›„์˜ ์ปค๋ฐ‹์— ๋Œ€ํ•ด์„œ๋Š” ์ˆ˜์ •์ƒํƒœ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ git status๋กœ ํ™•์ธ์„ ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

keichee$ git status

On branch test-revert

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git checkout -- <file>..." to discard changes in working directory)


        modified:   src/main/resources/logback-spring.xml

์ด์ œ ์ด๋ ‡๊ฒŒ ์ˆ˜์ •๋œ ๋‚ด์šฉ๋“ค์— ๋Œ€ํ•ด์„œ ์›๋ณตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

keichee$ git checkout -- src/main/resources/logback-spring.xml

ํŒŒ์ผ๋“ค์ด ๋„ˆ๋ฌด ๋งŽ์•„ ์ด๋ ‡๊ฒŒ ํ•˜๊ธฐ๊ฐ€ ๋ถˆํŽธํ•˜๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

# Revert changes to modified files.

git reset --hard


# Remove all untracked files and directories.

# '-f' is force, '-d' is remove directories.

git clean -fd

์ถœ์ฒ˜ : https://stackoverflow.com/questions/5807137/how-to-revert-uncommitted-changes-including-files-and-folders


4. Force push

keichee$ git push -f

Total 0 (delta 0), reused 0 (delta 0)

remote: 

remote: Create pull request for test-revert:

remote:   https://git.repository/compare/commits?sourceBranch=refs/heads/test-revert

remote: 

To https://git.repository

 + 61495b45b...1c74ca53e test-revert -> test-revert (forced update)



์ด์ƒ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜๋„ ์ฆ์ฝ”๋”ฉํ•˜์„ธ์š”~


Converting timestamp to date time string format and vice versa

 

์‹ค์ œ ์—…๋ฌด์—์„œ java๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•  ๋•Œ ๋‚ ์งœ/์‹œ๊ฐ„์„ ๋‹ค๋ค„์•ผ ํ•  ๋•Œ๊ฐ€ ์ฐธ ๋งŽ์Šต๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•˜๊ฑฐ๋‚˜ ๋กœ๊น…์„ ํ•˜๊ฑฐ๋‚˜ ๋“ฑ๋“ฑ ๋ง์ด์ฃ .

๊ทธ๋ž˜์„œ ์˜ค๋Š˜์€ ์‹œ๊ฐ„ ๋ณ€ํ™˜์— ๋Œ€ํ•ด์„œ ์งง๊ฒŒ ํ•œ๊ฐ€์ง€๋งŒ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค.

์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‹œ์ž‘ํ•œ์ง€ ์–ผ๋งˆ ์•ˆ๋˜์—ˆ์„ ๋•Œ System.currentTimemillis()๋ฅผ ์ด์šฉํ•ด์„œ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์–ผ๋งˆ๋‚˜ ๋นจ๋ฆฌ ๋Œ์•„๊ฐ€๋Š”์ง€ ํ™•์ธ์„ ํ–ˆ์—ˆ๋Š”๋ฐ์š”, ์ € ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋Š” longํ˜•์ž…๋‹ˆ๋‹ค. 

1970๋…„ 1์›” 1์ผ UTC ์ž์ • ์ดํ›„๋กœ ๋ช‡ ๋ฐ€๋ฆฌ์ดˆ๊ฐ€ ์ง€๋‚ฌ๋Š”์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์ฃ .

๋ฐ˜ํ™˜๊ฐ’์— ๋Œ€ํ•œ ์ •ํ™•ํ•œ ์ •์˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

   * @return  the difference, measured in milliseconds, between
   *          the current time and midnight, January 1, 1970 UTC.

 

๊ทธ๋Ÿฐ๋ฐ ์ด longํ˜• ์ˆซ์ž๋Š” ๊ธธ์ด๋„ ๊ธธ๊ณ  ์ด๊ฒŒ ๋„๋Œ€์ฒด ๊ทธ๋ž˜์„œ ๋ช‡ ์ผ ๋ช‡ ์‹œ ๋ผ๋Š” ๊ฑด์ง€๋ฅผ ์•Œ์•„๋ณด๊ธฐ๊ฐ€ ํž˜๋“ค์ฃ 

๊ทธ๋ž˜์„œ ์ด longํ˜• ์ˆซ์ž๋ฅผ ๊ฐ€๋…์„ฑ์žˆ๊ฒŒ ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์‹œ๊ฐ„ ํฌ๋งท์— ๋งž๊ฒŒ String ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ ‡๊ฒŒ ์–ป์€ String ํƒ€์ž… ์‹œ๊ฐ„๊ฐ’์„ ๋‹ค์‹œ longํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด ๋˜ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”?

 

์šฐ์„  longํƒ€์ž… ์‹œ๊ฐ„์„ ์›ํ•˜๋Š” ์ผ์‹œ ํฌ๋งท์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ Date์™€ SimpleDateFormat์„ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ง์ด์ฃ .

 public static void main(String[] args) {
     Date d = new Date(vv);
     DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
     String bb = format.format(d);
     System.out.println(bb);
 }

 

๋ฐ˜๋Œ€๋กœ String์œผ๋กœ ๋œ ์ผ์‹œ๋ฅผ long์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด java.sql.Timestamp๋ฅผ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

public static void main(String[] args) {
    String prdt = "2020-01-10 10:38:09.419";
    long vv = Timestamp.valueOf(prdt).getTime();
    System.out.println(vv);
}

 

ํ•˜์ง€๋งŒ Timestamp.valueOf(String) ๋ฉ”์„œ๋“œ๋Š” "yyyy-mm-dd hh:mm:ss[.fffffffff]" ํ˜•ํƒœ์˜ string๋งŒ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ผ์ž๋งŒ ์žˆ๊ฑฐ๋‚˜ ์‹œ๊ฐ„๊ฐ’๋งŒ ์žˆ์œผ๋ฉด ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋‹ค๋Š” ๊ฒƒ ๋ช…์‹ฌํ•˜์„ธ์š”.

๋งŒ์•ฝ ์ผ์‹œ ํฌ๋งท์„ ์ž˜๋ชป ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

Exception in thread "main" java.lang.IllegalArgumentException: 
		Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

 

java.sql.Timestamp ๋Œ€์‹ ์— LocalDateTime์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {

    String prdt2 = "2020-01-11 00:00:00";
    System.out.println(Timestamp.valueOf(prdt2).getTime());
    System.out.println(LocalDateTime.parse("2020-01-11T00:00:00")
        .atZone(ZoneId.of("Asia/Seoul"))
        .toInstant()
        .toEpochMilli());
}

---์ถœ๋ ฅ๊ฒฐ๊ณผ---
1578668400000
1578668400000

 

ํ•˜์ง€๋งŒ LocalDateTime์˜ ๊ฒฝ์šฐ parseํ•  ๋•Œ ๋“ค์–ด๊ฐ€๋Š” stringํ˜•ํƒœ์˜ ์ผ์‹œ๊ฐ’์— 'T'๊ตฌ๋ถ„์ž๋ฅผ ํฌํ•จํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ํŒŒ์‹ฑ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

Exception in thread "main" java.time.format.DateTimeParseException: 
		Text '2020-01-11 00:00:00' could not be parsed at index 10

 

 

Summary

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž๋ฐ”์—์„œ longํƒ€์ž…์˜ timestampe๋ฅผ String ํƒ€์ž…์˜ ๊ฐ€๋…์„ฑ ์ข‹์€ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…๊ณผ ๊ทธ๋ ‡๊ฒŒ ๋ณ€ํ™˜๋œ String์„ ๋‹ค์‹œ long์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

long -> String -> long ๋ณ€๊ฒฝ์„ ํ–ˆ์„ ๋•Œ ๋™์ผํ•œ ๊ฐ’์ด ๋‚˜์˜ค๋Š”์ง€๋„ ์•„๋ž˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•ด์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {

    // String to long conversion 1
    String prdt = "2020-01-11 10:24:09.419";
    long l1 = Timestamp.valueOf(prdt).getTime();
    System.out.println(l1);

    // String to long conversion 2
    String prdt2 = "2020-01-11T10:24:09.419";
    long l2 = LocalDateTime.parse(prdt2)
        .atZone(ZoneId.of("Asia/Seoul"))
        .toInstant()
        .toEpochMilli();
    System.out.println(l2);

    // long to String conversion
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    System.out.println(format.format(new Date(l1)));
}

---์ถœ๋ ฅ๊ฒฐ๊ณผ---
1578705849419
1578705849419 <-- Timestamp๋ฅผ ์ด์šฉํ•ด์„œ ์–ป์€ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•จ์„ ํ™•์ธ 
2020-01-11 10:24:09.419 <-- long์œผ๋กœ ๋ณ€ํ™˜ํ–ˆ๋˜ String ํ˜•ํƒœ์˜ ์‹œ๊ฐ„๊ณผ ๋™์ผํ•˜๊ฒŒ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธ

 

์ด์ƒ์œผ๋กœ Java์—์„œ datetime์„ long์—์„œ String์œผ๋กœ ๋‹ค์‹œ String์—์„œ long์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์œ ์šฉํ–ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๊พน ๋ถ€ํƒํ•ด์š”~

 

๐Ÿ’ป Programming

[AWS DynamoDB] Conditional Check Failed Exception

ํ˜„์žฌ AWS ๋‹ค์ด๋‚˜๋ชจ DB๋ฅผ ์ด์šฉํ•ด์„œ ์ž‘์—…ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ 

๋ ˆ๊ฐ€์‹œ ์ฝ”๋“œ์˜ dynamoDB์— ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„์—์„œ 

ConditionalCheckFailedException ์˜ˆ์™ธ๋ฅผ ์žก์•„์„œ debug ๋กœ๊น…์„ ํ•˜๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค. 

 

์ด๊ฑธ ์™œ error ๋ ˆ๋ฒจ๋กœ ๋กœ๊น…ํ•˜์ง€ ์•Š๊ณ  debug ๋ ˆ๋ฒจ๋กœ ๋กœ๊น…์„ ํ•˜๊ณ  ๋ฌด์‹œํ•˜๊ณ  ์žˆ์„๊นŒ? 

์‹ค์ œ๋กœ ์–ผ๋งˆ๋‚˜ ํ•ด๋‹น ๋กœ๊ทธ๊ฐ€ ๋‚จ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๋‹ˆ ๋งค์ผ ๊ฐ™์ด 10๋ฒˆ ์ด์ƒ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. 

์šฐ์„  throughput์€ ์ „ํ˜€ ์ƒ๊ด€์ด ์—†์–ด๋ณด์˜€๋‹ค.

stackoverflow๋ฅผ ์ฐพ์•„๋ด๋„ ๋”ฑํžˆ ์‹œ์›ํ•œ ๋‹ต์„ ์–ป์„ ์ˆ˜๋Š” ์—†์—ˆ๋Š”๋ฐ

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ AWS ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐพ๊ฒŒ ๋˜์—ˆ๋‹ค.

AWS DynamoDB Conditional Constraints

 

ํ‚คํฌ์ธํŠธ ํ•œ์ค„๋งŒ ๋ฐœ์ทŒํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

You could specify a version attribute for your mapped objects, and the mapper would automatically apply conditional constraints to give you optimistic locking, but you couldn’t explicitly specify your own custom conditional constraints with the mapper.

 

์œ„ ํŽ˜์ด์ง€๋ฅผ ์ฝ์–ด๋ณด๋ฉด 

dynamo DB๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ version์„ ๊ธฐ์ค€์œผ๋กœ logical condition์„ ๊ฒ€์‚ฌํ•œ ๋’ค 

ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค๊ณ  ๋‚˜์™€์žˆ๋‹ค. 

์‹ค์ œ legacy์ฝ”๋“œ์—๋„ ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ด์„œ version์„ ์ฝ์–ด์™€์„œ 

์ƒˆ ๋ฐ์ดํ„ฐ ์ž…๋ ฅํ•  ๋•Œ ์„ธํŒ…ํ•ด์ฃผ๊ณ  ์žˆ์—ˆ๋‹ค. 

ok ๊ทธ๋Ÿผ ๋ญ”๊ฐ€ version์ถฉ๋Œ๋กœ ์ธํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์ง€ ๋ชปํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ถ”์ธก์„ ํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

์™œ ์ถฉ๋Œ์ด ๋‚ ๊นŒ? 

๋ถ„์‚ฐ์‹œ์Šคํ…œ์—์„œ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ์˜ ์—…๋ฐ์ดํŠธ ์š”์ฒญ์„ ๋™์‹œ์— ์—ฌ๋Ÿฌ ๊ฐœ ๋ฐ›๊ฒŒ ๋˜๋ฉด?

dynamo ํ…Œ์ด๋ธ”์˜ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์‹œ์— ์—…๋ฐ์ดํŠธ๋ฅผ ํ•˜๋ ค๊ณ  ์‹œ๋„๋ฅผ ํ•˜๊ฒŒ ๋  ํ…๋ฐ,

์‹ค์ œ๋กœ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์ „์— ๊ธฐ์กด version์„ ์กฐํšŒํ•ด์™€์„œ ์‹ ๊ทœ ๋ฐ์ดํ„ฐ์— version ์„ธํŒ…์„ ํ•ด์ฃผ๊ณ 

์—…๋ฐ์ดํŠธ๋ฅผ ์‹œ๋„ํ•˜๋Š”๋ฐ ์ฒ˜์Œ ์—…๋ฐ์ดํŠธ ์‹œ๋„๋Š” ์„ฑ๊ณต! (์ด๋•Œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” version์ด ์˜ฌ๋ผ๊ฐ€๊ฒŒ ๋œ๋‹ค)

๊ทธ ์ดํ›„๋Š” version์ถฉ๋Œ๋กœ ConditionalCheckFailedException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

 

 

์ดํด๋ฆฝ์Šค์—์„œ ํ”„๋กœ์ ํŠธ import๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import๋ฅผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋ฅผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹น์—ฐํžˆ ๊ธฐ์กด์— ์ƒ์„ฑํ–ˆ๋˜ ํ”„๋กœ์ ํŠธ๋‚˜ ํŒŒ์ผ์ด ์žˆ์–ด์•ผ๊ฒ ์ฃ 

์ด๋ฒˆ ์‹œ์—ฐ์— ์‚ฌ์šฉ๋œ ์ดํด๋ฆฝ์Šค ๋ฒ„์ „์€ oomph 2019-06 (4.12.0)์ž…๋‹ˆ๋‹ค.

์ดํด๋ฆฝ์Šค ๋ฒ„์ „


ํ”„๋กœ์ ํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋Š” ์•„๋ž˜ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

1. ์ดํด๋ฆฝ์Šค ์ขŒ์ธก์˜ package explorer์—์„œ Import projects...๋ฅผ ์„ ํƒ

2. ์ดํด๋ฆฝ์Šค ๋„๊ตฌ๋ฉ”๋‰ด(์ƒ๋‹จ๋ฉ”๋‰ด)์˜ File > Import ๋ฅผ ์„ ํƒ

ํŒจํ‚ค์ง€ํƒ์ƒ‰๊ธฐ

์ดํด๋ฆฝ์Šค ํŒŒ์ผ ๋ฉ”๋‰ด


์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜๋˜์ง€ ๋™์ผํ•œ ์•„๋ž˜ ์ฐฝ์ด ๋œจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ํŒ์—…์ฐฝ - ์ข…๋ฅ˜์„ ํƒ

์œ„ ์ฐฝ์—์„œ ๋‚ด๊ฐ€ ๋งŒ๋“ค์—ˆ๋˜ ํ”„๋กœ์ ํŠธ์˜ ์ข…๋ฅ˜๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” Existing Projects into Workspace๋ฅผ ์„ ํƒํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•  ๊ฒฝ์šฐ ํ˜„์žฌ workspace๊ฐ€ ์•„๋‹Œ 

๋‹ค๋ฅธ ๊ฒฝ๋กœ์— ์œ„์น˜ํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ํ˜„์žฌ workspace๋กœ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


ํ”„๋กœ์ ํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ํŒ์—…์ฐฝ - ๊ฒฝ๋กœํƒ์ƒ‰

์—ฌ๊ธฐ์„œ๋Š” ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.


ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ ์„ ํƒ์ฐฝ

importํ•  ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„์„œ ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์„ ํƒํ•ด์ค๋‹ˆ๋‹ค.

์ €๋Š” SampleProject๋ผ๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์„ ํƒํ•˜๊ณ  Open์„ ํด๋ฆญํ•˜์˜€์Šต๋‹ˆ๋‹ค.


ํ”„๋กœ์ ํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ํŒ์—…์ฐฝ 3

ํ”„๋กœ์ ํŠธ์˜ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ๊ฐ€ ํ‘œ์‹œ๋จ๊ฐ€ ๋™์‹œ์— 

Projects ๋ชฉ๋ก์— SampleProject๊ฐ€ ํ‘œ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ Finish ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.


ํ”„๋กœ์ ํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์™„๋ฃŒ ๋ชจ์Šต

Package Explorer์— ์œ„์™€ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ๊ฐ€ import๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


์ด์ƒ์œผ๋กœ ์ดํด๋ฆฝ์Šค IDE์—์„œ ํ”„๋กœ์ ํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋ฅผ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.




์Šคํ”„๋ง ํ”„๋กœ์ ํŠธ์—์„œ logback์„ ์ด์šฉํ•˜์—ฌ ํ™˜๊ฒฝ๋ณ„๋กœ ๋กœ๊ทธ ์„ค์ •์„ ๋‹ค๋ฅด๊ฒŒ ํ•  ๋•Œ 

<if>, <elseif> ๋“ฑ์˜ ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ค„ ์•Œ๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ์„ค์ •์„ ํ•ด๋ณด์•˜๋‹ค.

<if condition="property('myproject.profile').equals('live')">
<logger name="com.myproject" level="debug">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<root level="info"/>
</if>
<elseif condition="property('myproject.profile').equals('dev')">
<logger name="com.myproject" level="debug">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<root level="info"/>
</elseif>
<else condition="property('myproject.profile').equals('local')">
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</else>

์œ„ ์ฒ˜๋Ÿผ ์„ค์ •ํ–ˆ๋Š”๋ฐ ์•ฑ ๊ธฐ๋™ ์‹œ ์•„๋ž˜์ฒ˜๋Ÿผ logback์—์„œ ์—๋Ÿฌ๋กœ๊ทธ๋ฅผ ์ฐ์—ˆ๋‹ค.


14:46:24,662 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]

14:46:24,668 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.

14:46:24,668 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.

14:46:24,668 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details

14:46:24,673 |-ERROR in ch.qos.logback.core.joran.conditional.IfAction - Could not find Janino library on the class path. Skipping conditional processing.

14:46:24,673 |-ERROR in ch.qos.logback.core.joran.conditional.IfAction - See also http://logback.qos.ch/codes.html#ifJanino

14:46:24,673 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@31:64 - no applicable action for [logger], current ElementPath  is [[configuration][if][logger]]

14:46:24,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@32:42 - no applicable action for [appender-ref], current ElementPath  is [[configuration][if][logger][appender-ref]]


์›์ธ์€ ๋ช…ํ™•ํ•ด๋ณด์˜€๋‹ค. Janino๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—†์–ด์„œ์˜€๋‹ค.

<if>, <elseif> ๋“ฑ์˜ ํƒœ๊ทธ๋ฌธ๋ฒ•์„ Janino ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ํ•ด์„์„ ํ•ด์ฃผ๋Š”๋ฐ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—†์–ด์„œ logback ์„ค์ •ํŒŒ์ผํ•ด์„์„ ์ œ๋Œ€๋กœ ๋ชปํ•œ ์ƒํƒœ์˜€๋‹ค.

๊ทธ๋ž˜์„œ ํ˜„์žฌ์ผ์ž ๊ฐ€์žฅ ์ตœ์‹ ๋ฒ„์ „์œผ๋กœ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.14</version>
</dependency>


๊ทธ๋Ÿฐ๋ฐ ๋˜ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค ใ… ใ… 

 Failed to parse condition [property('myproject.profile').equals('live')] org.codehaus.commons.compiler.CompileException: Line 1, Column 45: Closing single quote missing

at org.codehaus.commons.compiler.CompileException: Line 1, Column 45: Closing single quote missing


์กฐ๊ฑด์œผ๋กœ ์ค€๊ฒŒ ํŒŒ์‹ฑ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹จ๋‹ค. ๊ทธ๋ž˜์„œ ์ปจ๋””์…˜์„ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ”๊พธ์—ˆ๋‹ค.

<if condition='property("myproject.profile").equals("local")'>

์Œ๋”ฐ์˜ดํ‘œ์™€ ํ™‘๋”ฐ์˜ดํ‘œ๋ฅผ ๋ฐ”๊พผ๊ฑฐ๋‹ค.

๊ทธ๋ฆฌ๊ณ  if-else if - else ๋ฅผ ํ•˜๊ธฐ์œ„ํ•ด์„  nested if๋ฅผ ์จ์•ผํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ nested <if> ๋ฅผ ์“ฐ๊ธฐ์—” ์•ˆ์ชฝ์œผ๋กœ ์ธ๋ดํŠธ ๋˜๋Š” ๊ฒŒ ๋งŽ์•„์ ธ์„œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.

์–ด์ฐจํ”ผ ์กฐ๊ฑด์„ equals๋กœ ๋น„๊ตํ•˜๊ณ  ์žˆ์œผ๋‹ˆ 

๊ฒฐ๊ณผ์ ์œผ๋กœ if๋ฌธ๋งŒ ์จ์„œ profile๊ฐ’์„ ๋น„๊ตํ•ด์ฃผ์–ด๋„ if-elseif์˜ ํšจ๊ณผ๋Š” ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ”๊ฟจ๋‹ค.

<if condition='property("myproject.profile").equals("live")'>
<then>
<logger name="com.myproject" level="debug">
<appender-ref ref="ELASTIC"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<root level="info"/>
</then>
</if>
<if condition='property("myproject.profile").equals("dev")'>
<then>
<logger name="com.myproject" level="debug">
<appender-ref ref="ELASTIC"/>
</logger>
<logger name="org.springframework" level="WARN"/>
<root level="info"/>
</then>
</if>
<if condition='property("myproject.profile").equals("local")'>
<then>
<logger name="com.myproject" level="debug">
<appender-ref ref="STDOUT"/>
</logger>
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</then>
</if>

์ด์ œ ์ž˜ ๋™์ž‘ํ•œ๋‹ค.


java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonStreamContext.<init>(II)V

์œ„ ์˜ค๋ฅ˜๋Š” jackson-core ๋ฒ„์ „๊ณผ jackson-bind ๋ฒ„์ „์ด ์„œ๋กœ ๋‹ค๋ฅผ ๊ฒฝ์šฐ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค.

์˜ค๋ฅ˜๋ฐœ์ƒ์›์ธ 

๋ฉ”์ด๋ธ ํ”„๋กœ์ ํŠธ์—์„œ ์ผ๋ถ€ ์˜์กด์„ฑ(dependency)์„ ์‹ ๊ทœ๋กœ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ, ํ•ด๋‹น dependency๊ฐ€ jackson-core์˜ 2.8.0๋ฒ„์ „์„ ํฌํ•จํ•˜๊ณ ์žˆ์—ˆ๋‹ค. ๊ธฐ์กด์— jackson-core, jackson-databind ๋ฒ„์ „์„ 2.9.0 ์„ ์‚ฌ์šฉํ•˜๋„๋ก ์˜์กด์„ฑ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์‹ ๊ทœ ์ถ”๊ฐ€ ์˜์กด์„ฑ์ด jackson-core 2.8.0 ๋ฒ„์ „์„ ํฌํ•จํ•˜๊ณ ์žˆ์–ด ์•ฑ ๊ธฐ๋™ ์‹œ 2.8.0๋ฒ„์ „์ด ๋ฌผ๋ ค์˜ฌ๋ผ๊ฐ€๋ฉด์„œ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

์˜ค๋ฅ˜ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

์‹ ๊ทœ๋กœ ์ถ”๊ฐ€ํ•œ ์˜์กด์„ฑ์— ์•„๋ž˜์™€ ๊ฐ™์ด exclusion ์ฒ˜๋ฆฌํ•จ.

<dependency>
<groupId>com.internetitem</groupId>
<artifactId>logback-elasticsearch-appender</artifactId>
<version>1.6</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
</exclusions>
</dependency>


์ฐธ๊ณ ์ž๋ฃŒ : ๊นƒํ—™ jackson-core ์ด์Šˆ