Skip to content
lonslonz edited this page Oct 24, 2014 · 6 revisions

Java

  • Rake는 java는 지원하지 않는다. 즉, 서버 to rake로 로그를 보낼 수 있는 자바로 된 client는 제공하지 않는다.
  • 다만 Rake는 json으로 된 데이터를 받는 api가 개발되어 있으므로, 이를 이용하여 테스트 용으로 간단히 java 코드에서도 로그를 보낼 수 있다.
  • iOS, javascript, android에 익숙하지 않은 개발자가 적용 전에 간단히 테스트 해 볼 수 있도록 예제 샘플을 제공한다.
  • Sentinel과 Rake의 사용법을 간단히 확인할 수 있도록 한다.

코드 샘플

  • 다운로드 샘플 코드

  • 다음의 코드와 같이 Shuttle 객체를 이용하여 로그 정의 대로 정의한다.

      private static final String TOKEN = "9e9b36b3ec8bdce6de5d4b591b7fb6ff731d04e";
    
      public static void main( String[] args )
      {
          RakeSampleSentinelShuttle shuttle = new RakeSampleSentinelShuttle();
      
          SimpleDateFormat formmater = new SimpleDateFormat("yyyyMMddHHmmssSSS");
      
         shuttle.setBase_time(formmater.format(new Date()));
         shuttle.setLocal_time(formmater.format(new Date()));
         shuttle.rake_lib_version("r0.5.0_c1.3").rake_lib("android");
         shuttle.setToken(TOKEN);
         shuttle.page_id("myPage").action_id("MyAction").action_extra_value("extra what");
      
          try {
          	sendLogsToRake(shuttle);
          } catch(Exception e) {
          	e.printStackTrace();
          	return;
          }
          System.out.println("Success");
      }
    
  • sentinel shuttle 객체를 rake에서 받을 수 있는 json 포맷으로 바꾸어 http로 로그를 전송한다. 원래 포맷 변경 및 로그 전송은 iOS, android, javascript의 경우 Shuttle 객체 내부에서 수행한다.

          private static void sendLogsToRake(RakeSampleSentinelShuttle shuttle) throws Exception{
          	ObjectMapper mapper = new ObjectMapper();
              
          	String content = convertShuttleToRakeJson(shuttle);
              System.out.println(content);
          	
          	String logToSend = 
              	HttpUtil.sendHttpPut(
      	         	"https://pg.rake.skplanet.com:8443/log/trackJson",
      		        content, 
          			2000, 
              		2000);
          }
          private static String convertShuttleToRakeJson(RakeSampleSentinelShuttle shuttle) throws Exception {
              ObjectMapper mapper = new ObjectMapper();
      
          	RakeJsonLog rakeJsonLog = new RakeJsonLog();
          	rakeJsonLog.setCompress("plain");
          	Map<String, Object> shuttleMap = mapper.readValue(shuttle.toJSONString(), Map.class);
          	Map<String, Object> dataMap = new HashMap<String, Object>();
          	Map<String, Object> propertiesMap = new HashMap<String, Object>();
      
          	for(Map.Entry<String, Object> entry : shuttleMap.entrySet()) {
              	if(entry.getKey().equals("sentinel_meta")) {
              		Map<String, Object> shuttleMetaMap = (Map<String, Object>)entry.getValue();
              		for(Map.Entry<String, Object> entry2 : shuttleMetaMap.entrySet()) {
              			dataMap.put(entry2.getKey(), entry2.getValue());
              		}	
              	} else {
              		propertiesMap.put(entry.getKey(), entry.getValue());
          		}
          	}
              dataMap.put("properties", propertiesMap);
      
          	List<Map<String, Object>> shuttleMapList = new LinkedList<Map<String, Object>>();
          	shuttleMapList.add(dataMap); 
      
          	rakeJsonLog.setData(shuttleMapList);
          	return mapper.writeValueAsString(rakeJsonLog);
          }
    
Clone this wiki locally