+++ .classpath
... | ... | @@ -0,0 +1,11 @@ |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<classpath> | |
| 3 | + <classpathentry kind="src" path="src"/> | |
| 4 | + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-11.0.1"> | |
| 5 | + <attributes> | |
| 6 | + <attribute name="module" value="true"/> | |
| 7 | + </attributes> | |
| 8 | + </classpathentry> | |
| 9 | + <classpathentry exported="true" kind="con" path="org.eclipse.jdt.USER_LIBRARY/lib"/> | |
| 10 | + <classpathentry kind="output" path="bin"/> | |
| 11 | +</classpath> |
+++ .gitignore
... | ... | @@ -0,0 +1,6 @@ |
| 1 | +## Ignore Visual Studio temporary files, build results, and | |
| 2 | +## files generated by popular Visual Studio add-ons. | |
| 3 | + | |
| 4 | +bin/ | |
| 5 | +target/ | |
| 6 | +.git/ |
+++ .project
... | ... | @@ -0,0 +1,23 @@ |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<projectDescription> | |
| 3 | + <name>logApi</name> | |
| 4 | + <comment></comment> | |
| 5 | + <projects> | |
| 6 | + </projects> | |
| 7 | + <buildSpec> | |
| 8 | + <buildCommand> | |
| 9 | + <name>org.eclipse.wst.common.project.facet.core.builder</name> | |
| 10 | + <arguments> | |
| 11 | + </arguments> | |
| 12 | + </buildCommand> | |
| 13 | + <buildCommand> | |
| 14 | + <name>org.eclipse.jdt.core.javabuilder</name> | |
| 15 | + <arguments> | |
| 16 | + </arguments> | |
| 17 | + </buildCommand> | |
| 18 | + </buildSpec> | |
| 19 | + <natures> | |
| 20 | + <nature>org.eclipse.jdt.core.javanature</nature> | |
| 21 | + <nature>org.eclipse.wst.common.project.facet.core.nature</nature> | |
| 22 | + </natures> | |
| 23 | +</projectDescription> |
+++ lib/json-20210307.jar
| Binary file is not shown |
+++ lib/mssql-jdbc-9.4.0.jre11.jar
| Binary file is not shown |
+++ src/logApi/DBConnection.java
... | ... | @@ -0,0 +1,110 @@ |
| 1 | +package logApi; | |
| 2 | + | |
| 3 | +import java.sql.Connection; | |
| 4 | +import java.sql.DriverManager; | |
| 5 | +import java.sql.ResultSet; | |
| 6 | +import java.sql.SQLException; | |
| 7 | +import java.sql.Statement; | |
| 8 | +import java.util.ArrayList; | |
| 9 | +import java.util.HashMap; | |
| 10 | +import java.util.Map; | |
| 11 | +import java.util.concurrent.ExecutionException; | |
| 12 | + | |
| 13 | +import org.json.JSONArray; | |
| 14 | +import org.json.JSONException; | |
| 15 | +import org.json.JSONObject; | |
| 16 | + | |
| 17 | +import java.util.List; | |
| 18 | + | |
| 19 | +public class DBConnection { | |
| 20 | + private Connection con; | |
| 21 | + private Statement stmt; | |
| 22 | + private ResultSet rs; | |
| 23 | + public static JSONArray jarr; | |
| 24 | + public DBConnection() { | |
| 25 | + try { | |
| 26 | + String url = "jdbc:sqlserver://signus-sf1.koreacentral.cloudapp.azure.com:14443;databaseName=H3_DB"; | |
| 27 | + String user = "hanmi"; | |
| 28 | + String passwd = "u3hanmi"; | |
| 29 | + con = DriverManager.getConnection(url, user, passwd); | |
| 30 | + System.out.println("DB���� ����"); | |
| 31 | + stmt = con.createStatement(); | |
| 32 | + System.out.println("Statement��ü ���� ����"); | |
| 33 | + List<Map> list = new ArrayList<Map>() ; | |
| 34 | + List<String> keyList = new ArrayList<String>(); | |
| 35 | + Map map = new HashMap(); | |
| 36 | + | |
| 37 | + | |
| 38 | + rs = stmt.executeQuery("select * from T_SYS_LOG_SF WHERE SEND_YN= 'N' ORDER BY 1 "); //��ȸ�� ������� ResultSet�� rs�� �����Ѵ�. | |
| 39 | + | |
| 40 | + while(rs.next()) { | |
| 41 | + | |
| 42 | + map.put("crtfcKey", rs.getString(2)); | |
| 43 | + map.put("logDt", rs.getString(3)); | |
| 44 | + map.put("useSe", rs.getString(4)); | |
| 45 | + map.put("sysUser", rs.getString(5)); | |
| 46 | + map.put("conectIp", rs.getString(6)); | |
| 47 | + map.put("dataUsgqty", rs.getString(7)); | |
| 48 | + //map.put("SEND_YN", rs.getString(8)); | |
| 49 | + //map.put("PROC_NM", rs.getString(9)); | |
| 50 | + list.add( map); | |
| 51 | + keyList.add(rs.getString(1)); | |
| 52 | + map = new HashMap(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + rs.close(); | |
| 56 | + stmt.close(); | |
| 57 | + | |
| 58 | + jarr = convertListToJson(list); | |
| 59 | + | |
| 60 | + | |
| 61 | + for(int i=0; i<keyList.size(); i++) { | |
| 62 | + try { | |
| 63 | + LogAPI.send("logData="+jarr.get(i).toString()); | |
| 64 | + | |
| 65 | + stmt = con.createStatement(); | |
| 66 | + stmt.executeUpdate("UPDATE T_SYS_LOG_SF SET SEND_YN='N' WHERE IDX = "+keyList.get(i)); | |
| 67 | + stmt.close(); | |
| 68 | + } catch (JSONException e) { | |
| 69 | + // TODO Auto-generated catch block | |
| 70 | + e.printStackTrace(); | |
| 71 | + } catch (ExecutionException e) { | |
| 72 | + // TODO Auto-generated catch block | |
| 73 | + e.printStackTrace(); | |
| 74 | + } catch (InterruptedException e) { | |
| 75 | + // TODO Auto-generated catch block | |
| 76 | + e.printStackTrace(); | |
| 77 | + } | |
| 78 | + | |
| 79 | + | |
| 80 | + } | |
| 81 | + | |
| 82 | + con.close(); | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + } catch (SQLException e) { | |
| 87 | + System.out.println("DB Connect Failed"); | |
| 88 | + System.out.print("Error: " + e.getMessage()); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | + | |
| 93 | + public static JSONArray convertListToJson(List<Map> list) { | |
| 94 | + JSONArray jsonArray = new JSONArray(); | |
| 95 | + for (Map<String, Object> map : list) { | |
| 96 | + jsonArray.put(convertMapToJson(map)); | |
| 97 | + } | |
| 98 | + return jsonArray; | |
| 99 | + } | |
| 100 | + | |
| 101 | + | |
| 102 | + public static JSONObject convertMapToJson(Map<String, Object> map) { | |
| 103 | + JSONObject json = new JSONObject(); | |
| 104 | + for (Map.Entry<String, Object> entry : map.entrySet()) { | |
| 105 | + String key = entry.getKey(); Object value = entry.getValue(); json.put(key, value); | |
| 106 | + } | |
| 107 | + return json; | |
| 108 | + } | |
| 109 | + | |
| 110 | +} |
+++ src/logApi/LogAPI.java
... | ... | @@ -0,0 +1,157 @@ |
| 1 | +package logApi; | |
| 2 | + | |
| 3 | +import java.net.URI; | |
| 4 | +import java.net.http.HttpClient; | |
| 5 | +import java.net.http.HttpHeaders; | |
| 6 | +import java.net.http.HttpRequest; | |
| 7 | +import java.net.http.HttpResponse; | |
| 8 | +import java.nio.charset.Charset; | |
| 9 | +import java.util.concurrent.CompletableFuture; | |
| 10 | +import java.util.concurrent.ExecutionException; | |
| 11 | +import java.util.concurrent.ExecutorService; | |
| 12 | +import java.util.concurrent.Executors; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Java 9 introduced a new incubating HttpClient API for dealing with HTTP requests. | |
| 16 | + * 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. | |
| 17 | + */ | |
| 18 | +public class LogAPI { | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Example for sending a synchronous GET request | |
| 22 | + * | |
| 23 | + * @throws java.io.IOException | |
| 24 | + * @throws InterruptedException | |
| 25 | + */ | |
| 26 | + private static void demo1() throws java.io.IOException, InterruptedException { | |
| 27 | + | |
| 28 | + System.out.println("Demo 1"); | |
| 29 | + HttpRequest request = HttpRequest.newBuilder() | |
| 30 | + .uri(URI.create("https://signus-mesweb.koreacentral.cloudapp.azure.com/api/user")) | |
| 31 | + .header("Content-Type", "text/plain") | |
| 32 | + .GET() | |
| 33 | + .build(); | |
| 34 | + | |
| 35 | + /* | |
| 36 | + The new HttpClient can be used either synchronously or asynchronously. | |
| 37 | + A /synchronous/ request blocks the current thread until the response is available. | |
| 38 | + BodyHandlers define the expected type of response body (e.g. as string, byte-array or file): | |
| 39 | + */ | |
| 40 | + var client = HttpClient.newHttpClient(); | |
| 41 | + | |
| 42 | + HttpResponse.BodyHandler<String> asString = HttpResponse.BodyHandlers.ofString(); | |
| 43 | + /* | |
| 44 | + * HttpResponse.BodyHandlers | |
| 45 | + * .olLines() | .ofByteArray() | ofFile() | ofFileDownload() | |
| 46 | + */ | |
| 47 | + | |
| 48 | + HttpResponse<String> response = client.send(request, asString); | |
| 49 | + | |
| 50 | + int statusCode = response.statusCode(); | |
| 51 | + System.out.printf("Status Code: %s%n", statusCode); | |
| 52 | + HttpHeaders headers = response.headers(); | |
| 53 | + System.out.printf("Response Headers: %s%n", headers); | |
| 54 | + System.out.println(response.body()); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Example for sending an asynchronous GET request | |
| 59 | + * | |
| 60 | + * @throws InterruptedException | |
| 61 | + * @throws java.util.concurrent.ExecutionException | |
| 62 | + */ | |
| 63 | + private static void demo2() throws InterruptedException, java.util.concurrent.ExecutionException { | |
| 64 | + | |
| 65 | + System.out.println("Demo 2"); | |
| 66 | + | |
| 67 | + var request = HttpRequest.newBuilder() | |
| 68 | + .uri(URI.create("https://example.com")) | |
| 69 | + // .GET() // can be omitted as it is the default... | |
| 70 | + .build(); | |
| 71 | + | |
| 72 | + | |
| 73 | + /* | |
| 74 | + * A request can also be performed asynchronously. | |
| 75 | + * Calling sendAsync does not block the current thread and instead returns a | |
| 76 | + * CompletableFuture to construct asynchronous operation pipelines. | |
| 77 | + */ | |
| 78 | + | |
| 79 | + var client = HttpClient.newHttpClient(); | |
| 80 | + CompletableFuture<HttpResponse<String>> responseFuture = // | |
| 81 | + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); | |
| 82 | + | |
| 83 | + responseFuture | |
| 84 | + .thenApply(HttpResponse::body) | |
| 85 | + .thenAccept(System.out::println) | |
| 86 | + .get() // wait for result | |
| 87 | + ; | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Example for sending an asynchronous POST request | |
| 92 | + * | |
| 93 | + * @throws InterruptedException | |
| 94 | + * @throws java.util.concurrent.ExecutionException | |
| 95 | + */ | |
| 96 | + private static void demo3() throws ExecutionException, InterruptedException { | |
| 97 | + | |
| 98 | + System.out.println("Demo 3"); | |
| 99 | + | |
| 100 | + var postRequest = HttpRequest.newBuilder() | |
| 101 | + .uri(URI.create("https://signus-mesweb.koreacentral.cloudapp.azure.com/api/user")) | |
| 102 | + .header("Content-Type", "text/plain") | |
| 103 | + .POST(HttpRequest.BodyPublishers.ofString("Hi there!")) | |
| 104 | + .build(); | |
| 105 | + | |
| 106 | + ExecutorService executor = Executors.newSingleThreadExecutor(); | |
| 107 | + var client = HttpClient.newBuilder().executor(executor).build(); | |
| 108 | + | |
| 109 | + var responseFuture = client.sendAsync(postRequest, HttpResponse.BodyHandlers.ofString()); | |
| 110 | + | |
| 111 | + responseFuture.thenApply(res -> { | |
| 112 | + System.out.printf("StatusCode: %s%n", res.statusCode()); | |
| 113 | + return res; | |
| 114 | + }) | |
| 115 | + .thenApply(HttpResponse::body) | |
| 116 | + .thenAccept(System.out::println) | |
| 117 | + .get(); | |
| 118 | + | |
| 119 | + executor.shutdownNow(); | |
| 120 | + | |
| 121 | + } | |
| 122 | + | |
| 123 | + | |
| 124 | + public static void send(String body) throws ExecutionException, InterruptedException { | |
| 125 | + | |
| 126 | + System.out.println("Demo 3:"+HttpRequest.BodyPublishers.ofString(body)); | |
| 127 | + | |
| 128 | + var postRequest = HttpRequest.newBuilder() | |
| 129 | + .uri(URI.create("https://log.smart-factory.kr/apisvc/sendLogDataJSON.do")) | |
| 130 | + .header("Content-Type", "application/x-www-form-urlencoded") | |
| 131 | + .POST(HttpRequest.BodyPublishers.ofString(body, Charset.forName("UTF-8"))) | |
| 132 | + .build(); | |
| 133 | + | |
| 134 | + ExecutorService executor = Executors.newSingleThreadExecutor(); | |
| 135 | + var client = HttpClient.newBuilder().executor(executor).build(); | |
| 136 | + | |
| 137 | + var responseFuture = client.sendAsync(postRequest, HttpResponse.BodyHandlers.ofString()); | |
| 138 | + | |
| 139 | + responseFuture.thenApply(res -> { | |
| 140 | + System.out.printf("StatusCode: %s%n", res.statusCode()); | |
| 141 | + return res; | |
| 142 | + }) | |
| 143 | + .thenApply(HttpResponse::body) | |
| 144 | + .thenAccept(System.out::println) | |
| 145 | + .get(); | |
| 146 | + | |
| 147 | + executor.shutdownNow(); | |
| 148 | + | |
| 149 | + } | |
| 150 | + | |
| 151 | + | |
| 152 | + public static void main(String args[]) { | |
| 153 | + new DBConnection(); | |
| 154 | + } | |
| 155 | + | |
| 156 | + | |
| 157 | +}(No newline at end of file) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?