김상식 김상식 2022-07-28
initial source
@44a55fa98564cdd4a6a2cecf591a28ed42dbc43f
 
.classpath (added)
+++ .classpath
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-11.0.1">
+		<attributes>
+			<attribute name="module" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry exported="true" kind="con" path="org.eclipse.jdt.USER_LIBRARY/lib"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
 
.gitignore (added)
+++ .gitignore
@@ -0,0 +1,6 @@
+## Ignore Visual Studio temporary files, build results, and
+## files generated by popular Visual Studio add-ons.
+
+bin/
+target/
+.git/
 
.project (added)
+++ .project
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>logApi</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.wst.common.project.facet.core.builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
+	</natures>
+</projectDescription>
 
lib/json-20210307.jar (Binary) (added)
+++ lib/json-20210307.jar
Binary file is not shown
 
lib/mssql-jdbc-9.4.0.jre11.jar (Binary) (added)
+++ lib/mssql-jdbc-9.4.0.jre11.jar
Binary file is not shown
 
src/logApi/DBConnection.java (added)
+++ src/logApi/DBConnection.java
@@ -0,0 +1,110 @@
+package logApi;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.util.List;
+
+public class DBConnection {
+	private Connection con;
+	private Statement stmt;
+	private ResultSet rs;
+	public 	static JSONArray jarr;
+	public DBConnection() {
+		try {
+			String url = "jdbc:sqlserver://signus-sf1.koreacentral.cloudapp.azure.com:14443;databaseName=H3_DB";
+			String user = "hanmi";
+			String passwd = "u3hanmi";
+			con = DriverManager.getConnection(url, user, passwd);
+			System.out.println("DB���� ����");
+			stmt = con.createStatement();
+			System.out.println("Statement��ü ���� ����");
+			List<Map> list = new ArrayList<Map>() ;
+			List<String> keyList = new ArrayList<String>();
+			Map map = new HashMap();
+			
+			
+			rs = stmt.executeQuery("select * from T_SYS_LOG_SF WHERE SEND_YN= 'N' ORDER BY 1 "); //��ȸ�� ������� ResultSet�� rs�� �����Ѵ�. 
+			
+			while(rs.next()) {
+				
+				map.put("crtfcKey", rs.getString(2));
+				map.put("logDt", rs.getString(3));
+				map.put("useSe", rs.getString(4));
+				map.put("sysUser", rs.getString(5));
+				map.put("conectIp", rs.getString(6));
+				map.put("dataUsgqty", rs.getString(7));
+				//map.put("SEND_YN", rs.getString(8));
+				//map.put("PROC_NM", rs.getString(9));
+				list.add( map);
+				keyList.add(rs.getString(1));
+				map = new HashMap();
+			}
+			
+			rs.close();
+			stmt.close();	
+			
+			jarr = convertListToJson(list);
+
+			
+			for(int i=0; i<keyList.size(); i++) {
+				try {
+					LogAPI.send("logData="+jarr.get(i).toString());
+					
+					stmt = con.createStatement();
+					stmt.executeUpdate("UPDATE T_SYS_LOG_SF SET SEND_YN='N' WHERE IDX = "+keyList.get(i));
+					stmt.close();
+				} catch (JSONException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				} catch (ExecutionException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				} catch (InterruptedException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+				
+			
+			}
+			
+			con.close();
+			
+
+			
+		} catch (SQLException e) {
+			System.out.println("DB Connect Failed");
+			System.out.print("Error: " + e.getMessage());
+		}
+	}
+	
+	
+	public static JSONArray convertListToJson(List<Map> list) { 
+		JSONArray jsonArray = new JSONArray(); 
+		for (Map<String, Object> map : list) { 
+			jsonArray.put(convertMapToJson(map)); 
+		} 
+		return jsonArray; 
+	}
+
+	
+	public static JSONObject convertMapToJson(Map<String, Object> map) { 
+		JSONObject json = new JSONObject(); 
+		for (Map.Entry<String, Object> entry : map.entrySet()) { 
+			String key = entry.getKey(); Object value = entry.getValue(); json.put(key, value); 
+		} 
+		return json; 
+	}
+
+}
 
src/logApi/LogAPI.java (added)
+++ src/logApi/LogAPI.java
@@ -0,0 +1,157 @@
+package logApi;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpHeaders;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.Charset;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * Java 9 introduced a new incubating HttpClient API for dealing with HTTP requests.
+ * As of Java 11 this API is now final and available in the standard libraries package java.net. Let's explore what we can do with this API.
+ */
+public class LogAPI {
+
+    /**
+     * Example for sending a synchronous GET request
+     *
+     * @throws java.io.IOException
+     * @throws InterruptedException
+     */
+    private static void demo1() throws java.io.IOException, InterruptedException {
+
+        System.out.println("Demo 1");
+        HttpRequest request = HttpRequest.newBuilder()
+                .uri(URI.create("https://signus-mesweb.koreacentral.cloudapp.azure.com/api/user"))
+                .header("Content-Type", "text/plain")
+                .GET()
+                .build();
+
+        /*
+        The new HttpClient can be used either synchronously or asynchronously.
+        A /synchronous/ request blocks the current thread until the response is available.
+        BodyHandlers define the expected type of response body (e.g. as string, byte-array or file):
+         */
+        var client = HttpClient.newHttpClient();
+
+        HttpResponse.BodyHandler<String> asString = HttpResponse.BodyHandlers.ofString();
+        /*
+         * HttpResponse.BodyHandlers
+         *    .olLines() | .ofByteArray() | ofFile() | ofFileDownload()
+         */
+
+        HttpResponse<String> response = client.send(request, asString);
+
+        int statusCode = response.statusCode();
+        System.out.printf("Status Code: %s%n", statusCode);
+        HttpHeaders headers = response.headers();
+        System.out.printf("Response Headers: %s%n", headers);
+        System.out.println(response.body());
+    }
+
+    /**
+     * Example for sending an asynchronous GET request
+     *
+     * @throws InterruptedException
+     * @throws java.util.concurrent.ExecutionException
+     */
+    private static void demo2() throws InterruptedException, java.util.concurrent.ExecutionException {
+
+        System.out.println("Demo 2");
+
+        var request = HttpRequest.newBuilder()
+                .uri(URI.create("https://example.com"))
+                // .GET() // can be omitted as it is the default...
+                .build();
+
+
+        /*
+         * A request can also be performed asynchronously.
+         * Calling sendAsync does not block the current thread and instead returns a
+         * CompletableFuture to construct asynchronous operation pipelines.
+         */
+
+        var client = HttpClient.newHttpClient();
+        CompletableFuture<HttpResponse<String>> responseFuture = //
+                client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
+
+        responseFuture
+                .thenApply(HttpResponse::body)
+                .thenAccept(System.out::println)
+                .get() // wait for result
+        ;
+    }
+
+    /**
+     * Example for sending an asynchronous POST request
+     *
+     * @throws InterruptedException
+     * @throws java.util.concurrent.ExecutionException
+     */
+    private static void demo3() throws ExecutionException, InterruptedException {
+
+        System.out.println("Demo 3");
+
+        var postRequest = HttpRequest.newBuilder()
+                .uri(URI.create("https://signus-mesweb.koreacentral.cloudapp.azure.com/api/user"))
+                .header("Content-Type", "text/plain")
+                .POST(HttpRequest.BodyPublishers.ofString("Hi there!"))
+                .build();
+
+        ExecutorService executor = Executors.newSingleThreadExecutor();
+        var client = HttpClient.newBuilder().executor(executor).build();
+
+        var responseFuture = client.sendAsync(postRequest, HttpResponse.BodyHandlers.ofString());
+
+        responseFuture.thenApply(res -> {
+            System.out.printf("StatusCode: %s%n", res.statusCode());
+            return res;
+        })
+                .thenApply(HttpResponse::body)
+                .thenAccept(System.out::println)
+                .get();
+
+        executor.shutdownNow();
+    
+    }
+    
+    
+    public static void send(String body) throws ExecutionException, InterruptedException {
+
+        System.out.println("Demo 3:"+HttpRequest.BodyPublishers.ofString(body));
+
+        var postRequest = HttpRequest.newBuilder()
+                .uri(URI.create("https://log.smart-factory.kr/apisvc/sendLogDataJSON.do"))
+                .header("Content-Type", "application/x-www-form-urlencoded")
+                .POST(HttpRequest.BodyPublishers.ofString(body, Charset.forName("UTF-8")))
+                .build();
+
+        ExecutorService executor = Executors.newSingleThreadExecutor();
+        var client = HttpClient.newBuilder().executor(executor).build();
+
+        var responseFuture = client.sendAsync(postRequest, HttpResponse.BodyHandlers.ofString());
+
+        responseFuture.thenApply(res -> {
+            System.out.printf("StatusCode: %s%n", res.statusCode());
+            return res;
+        })
+                .thenApply(HttpResponse::body)
+                .thenAccept(System.out::println)
+                .get();
+
+        executor.shutdownNow();
+    
+    }
+    
+    
+	public static void main(String args[]) {
+		new DBConnection();
+	}
+
+    
+}(No newline at end of file)
Add a comment
List