목차

Id Generation 서비스

개요

시스템을 개발할 때 필요한 유일한 ID를 생성하기 위해 사용하도록 서비스한다.

주요 개념

Universally Unique Identifier(UUID)

UUID는 OSF(Open Software Foundation)에 의해 제정된 고유식별자(Identifier)에 대한 표준이다. UUID는 16-byte (128-bit)의 숫자로 구성된다. UUID를 표현하는 방식에 대한 특별한 규정은 없으나, 일반적으로 아래와 같이 16진법으로 8-4-4-4-12 형식으로 표현한다.

550e8400-e29b-41d4-a716-446655440000

UUID 표준은 아래 문서에 기술되어 있다.

UUID는 다음 5개의 Version이 존재한다.

설명

ID 생성 방식으로는 UUID를 생성하는 UUID Generation Service와 sequence를 활용하는 Sequence Id Generation Service 그리고 키제공을 위한 테이블을 지정하여 생성하는 Table Id Generation Service 3가지가 있다.

UUID Generation Service

새로운 ID를 생성하기 위해 UUID 생성 알고리즘을 이용하여 16 바이트 길이의 ID를 생성한다. String 타입의 ID 생성과 BigDecimal 타입의 ID 생성 두가지 유형 ID 생성을 지원한다. 지원 하는 방법은 설정에 따라서 Mac Address Base Service , IP Address Base Service , No Address Base Service 세가지 유형이 있다.

Mac Address Base Service

MAC Address를 기반으로 유일한 Id를 생성하는 UUIdGenerationService

Configuration
<bean name="UUIdGenerationService" class="egovframework.rte.fdl.idgnr.impl.EgovUUIdGnrService">
    <property name="address">
        <value>00:00:F0:79:19:5B</value>
    </property>				
</bean>
Sample Source
@Resource(name="UUIdGenerationService")
private EgovIdGnrService uUidGenerationService;
 
@Test
public void testUUIdGeneration() throws Exception {
   assertNotNull(uUidGenerationService.getNextStringId());
   assertNotNull(uUidGenerationService.getNextBigDecimalId());
}

IP Address Base Service

IP Address를 기반으로 유일한 Id를 생성하는 UUIdGenerationService

Configuration
<bean name="UUIdGenerationServiceWithIP" class="egovframework.rte.fdl.idgnr.impl.EgovUUIdGnrService">
    <property name="address">
        <value>100.128.120.107</value>
    </property>		
</bean>
Sample Source
@Resource(name="UUIdGenerationServiceWithIP")
private EgovIdGnrService uUIdGenerationServiceWithIP; 
 
@Test
public void testUUIdGenerationIP() throws Exception {
   assertNotNull(uUIdGenerationServiceWithIP.getNextStringId());
   assertNotNull(uUIdGenerationServiceWithIP.getNextBigDecimalId());
}

No Address Base Service

IP Address 설정없이 Math.random()을 이용하여 주소정보를 생성하고 유일한 Id를 생성하는 UUIdGenerationService

Configuration
<bean name="UUIdGenerationServiceWithoutAddress" class="egovframework.rte.fdl.idgnr.impl.EgovUUIdGnrService">
</bean>
Sample Source
@Resource(name="UUIdGenerationServiceWithoutAddress")
private EgovIdGnrService uUIdGenerationServiceWithoutAddress; 
 
@Test
public void testUUIdGenerationNoAddress() throws Exception {
   assertNotNull(uUIdGenerationServiceWithoutAddress.getNextStringId());
   assertNotNull(uUIdGenerationServiceWithoutAddress.getNextBigDecimalId());
}

소스보기

Sequence Id Generation Service

새로운 ID를 생성하기 위해 Database의 SEQUENCE를 사용하는 서비스이다. 서비스를 이용하는 시스템에서 Query를 지정하여 아이디를 생성할 수 있도록 하고 Basic Type ServiceBigDecimal Type Service 두가지를 지원한다.

Basic Type Service

기본타입 ID를 제공하는 서비스로 int,short,byte,long 유형의 ID를 제공한다.

DB Schema
CREATE SEQUENCE idstest MINVALUE 0;
Configuration
  <bean name="primaryTypeSequenceIds" class="egovframework.rte.fdl.idgnr.impl.EgovSequenceIdGnrService" 
                         destroy-method="destroy">
    <property name="dataSource" ref="dataSource"/>
    <property name="query"  value="SELECT idstest.NEXTVAL FROM DUAL"/>
  </bean>
Sample Source
@Resource(name="primaryTypeSequenceIds")
private EgovIdGnrService primaryTypeSequenceIds;
 
@Test
public void testPrimaryTypeIdGeneration() throws Exception {
   //int
   assertNotNull(primaryTypeSequenceIds.getNextIntegerId());
   //short
   assertNotNull(primaryTypeSequenceIds.getNextShortId());
   //byte
   assertNotNull(primaryTypeSequenceIds.getNextByteId());
   //long
   assertNotNull(primaryTypeSequenceIds.getNextLongId());
}

BigDecimal Type Service

BigDecimal ID를 제공하는 서비스로 기본타입 ID 제공 서비스 설정에 추가적으로 useBigDecimals을 “true”로 설정하여 BigDecimal 사용하도록 한다.

DB Schema
CREATE SEQUENCE idstest MINVALUE 0;
Configuration
  <bean name="bigDecimalTypeSequenceIds" class="egovframework.rte.fdl.idgnr.impl.EgovSequenceIdGnrService" 
                         destroy-method="destroy">
    <property name="dataSource" ref="dataSource"/>
    <property name="query"  value="SELECT idstest.NEXTVAL FROM DUAL"/>
    <property name="useBigDecimals" value="true"/>
  </bean>
Sample Source
@Resource(name="bigDecimalTypeSequenceIds")
private EgovIdGnrService bigDecimalTypeSequenceIds;
 
@Test
public void testBigDecimalTypeIdGeneration() throws Exception {
   //BigDecimal
   assertNotNull(bigDecimalTypeSequenceIds.getNextBigDecimalId());
}

Database 별 설정

Oracle
HSQL
IBM DB2
Sybase
MS SQL Server
MySQL

Table Id Generation Service

새로운 아이디를 얻기 위해서 별도의 테이블을 생성하여 키값과 키값에 해당하는 아이디값을 입력하여 관리하여 제공하는 서비스로 table_name(CHAR 또는 VARCHAR타입), next_id(integer 또는 DECIMAL type)와 같이 두 칼럼을 필요로 한다. 별도의 테이블에 설정된 정보만을 사용하여 제공하는 Basic Service와 prefix와 채울 문자열을 지정하여 String ID를 생성할 수 있는 Strategy Base Service를 제공한다.

Basic Service

테이블에 지정된 정보에 의해서 아이디를 생성하는 서비스로 사용하고자 하는 시스템에서 테이블을 생성해서 사용할 수 있다.

DB Schema
CREATE TABLE ids ( table_name varchar(16) NOT NULL, 
		   next_id DECIMAL(30) NOT NULL,
		   PRIMARY KEY (table_name));
INSERT INTO ids VALUES('id','0');
Configuration
   <bean name="basicService" class="egovframework.rte.fdl.idgnr.impl.EgovTableIdGnrService" 
                           destroy-method="destroy">
      <property name="dataSource" ref="dataSource"/>
      <property name="blockSize"  value="10"/>
      <property name="table"	  value="ids"/>
      <property name="tableName"  value="id"/>
   </bean>
Sample Source
@Resource(name="basicService")
private EgovIdGnrService basicService;
 
@Test
public void testBasicService() throws Exception {
   //int
   assertNotNull(basicService.getNextIntegerId());
   //short
   assertNotNull(basicService.getNextShortId());
   //byte
   assertNotNull(basicService.getNextByteId());
   //long
   assertNotNull(basicService.getNextLongId());
   //BigDecimal
   assertNotNull(basicService.getNextBigDecimalId());
   //String
   assertNotNull(basicService.getNextStringId());
}

Strategy Base Service

아이디 생성을 위한 룰을 등록하고 룰에 맞는 아이디를 생성할 수 있도록 지원하는 서비스로 위의 Basic Service에서 추가적으로 Strategy정보 설정을 추가하여 사용 할 수 있다. 단, 이 서비스는 String 타입의 ID만을 제공한다.

DB Schema
CREATE TABLE idttest( table_name varchar(16) NOT NULL, 
		   next_id DECIMAL(30) NOT NULL,
		   PRIMARY KEY (table_name));
INSERT INTO idttest VALUES('test','0');
Configuration
   <bean name="Ids-TestWithGenerationStrategy" class="egovframework.rte.fdl.idgnr.impl.EgovTableIdGnrService" 
                           destroy-method="destroy">
      <property name="dataSource" ref="dataSource"/>
      <property name="strategy" ref="strategy"/>
      <property name="blockSize" value="1"/>
      <property name="table"	 value="idttest"/>
      <property name="tableName" value="test"/>		
   </bean>	
 
   <bean name="strategy" class="egovframework.rte.fdl.idgnr.impl.strategy.EgovIdGnrStrategyImpl">
      <property name="prefix" value="TEST-"/>
      <property name="cipers" value="5"/>
      <property name="fillChar" value="*"/>
   </bean>
Sample Source
@Resource(name="Ids-TestWithGenerationStrategy")
private EgovIdGnrService idsTestWithGenerationStrategy;
 
@Test
public void testIdGenStrategy() throws Exception {
 
   initializeNextLongId("test", 1);
 
   // prefix : TEST-, cipers : 5, fillChar :*)
   for (int i = 0; i < 5; i++)
      assertEquals("TEST-****" + (i + 1), idsTestWithGenerationStrategy.getNextStringId());
}