2020年12月8日 星期二

Quarkus-RestEasy & HttpRequest/Client

ResteasyTest.java (ref: quarkus guide and HowToDoInJava )
RecursiveTask/RecursiveAction不合的樣子@@"
package org.acme.lifecycle;

import io.quarkus.runtime.annotations.QuarkusMain;

import javax.ws.rs.core.Response;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import io.quarkus.runtime.Quarkus;


@QuarkusMain
public class ResteasyTest
{
    private static void getExample_one().
    {   
        ResteasyClient client = new ResteasyClientBuilder().build(); //for resteasy-api version 3.x
        ResteasyWebTarget target = client.target("http://localhost:2020/Helloworld/All");
        Response response = target.request().get();
        String value = response.readEntity(String.class);
        System.out.println(value);
        response.close();
    }   
    public static void main(String ... args) {
        System.out.println("Hello World");
        getExample_one();
        Quarkus.run(args);
    }   
}


ResteasHttpRequestClient.java (ref: httpclient, Java 11 HttpClient Examples)
package org.acme.lifecycle;

import io.quarkus.runtime.annotations.QuarkusMain;

import javax.ws.rs.core.Response;

import java.net.URI;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;

import io.quarkus.runtime.Quarkus;


@QuarkusMain
public class ResteasyTest
{
    private static void getExample_one()
    {   
        ResteasyClient client = new ResteasyClientBuilder().build(); //for resteasy-api version 3.x
        ResteasyWebTarget target = client.target("http://localhost:2020/Helloworld/All");
        Response response = target.request().get();
        String value = response.readEntity(String.class);
        System.out.println(value);
        response.close();
                HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/json; charset=UTF-8")
                .POST(BodyPublishers.ofString(reqBody))
                .build();
        HttpResponse<String> response = null;
		try 
		{
		    response = client.send(request, HttpResponse.BodyHandlers.ofString());
		} 
		catch (IOException e) 
		{
		    e.printStackTrace();
		} 
		catch (InterruptedException e) 
		{
		    e.printStackTrace();
		}
        return response.body();
    }
    
    private static String getExample_two()
    {
        //HttpClient client = HttpClient.newHttpClient();
        HttpClient client = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_1_1)
                .connectTimeout(Duration.ofSeconds(tout))
                .build();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .timeout(Duration.ofSeconds(tout))
                .header("Content-Type", "application/json; charset=UTF-8")
                .POST(BodyPublishers.ofString(reqBody))
                .build();

        try {
            return client.sendAsync(request, BodyHandlers.ofString())
                    .thenApply(HttpResponse::body).get().toString();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    private static String getExample_three()
    {
        //HttpClient client = HttpClient.newHttpClient();
        HttpClient client = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_1_1)
                .connectTimeout(Duration.ofSeconds(tout))
                .build();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .timeout(Duration.ofSeconds(tout))
                .header("Content-Type", "application/json; charset=UTF-8")
                .GET()
                .build();
        CompletableFuture <HttpResponse<String>> response =
                client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
        String result = null;
        try {
            result = response.thenApply(HttpResponse::body).get();
        }
        catch (InterruptedException e) 
        {
            e.printStackTrace();
        } 
        catch (ExecutionException e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public static void main(String ... args) {
        System.out.println("Hello World");
        getExample_one();
        //two跟three寫法其實相同
        getExample_two();
        getExample_three();
        Quarkus.run(args);
    }   
}

pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.acme</groupId>
  <artifactId>lifecycle-quickstart</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <properties>
    <surefire-plugin.version>2.22.1</surefire-plugin.version>
    <maven.compiler.target>11</maven.compiler.target>
    <quarkus.platform.version>1.10.3.Final</quarkus.platform.version>
    <maven.compiler.source>11</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.parameters>true</maven.compiler.parameters>
    <quarkus-plugin.version>1.10.3.Final</quarkus-plugin.version>
    <compiler-plugin.version>3.8.1</compiler-plugin.version>
    <quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>${quarkus.platform.group-id}</groupId>
        <artifactId>${quarkus.platform.artifact-id}</artifactId>
        <version>${quarkus.platform.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
     <!-- core library -->
    <dependency>
       <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
       <version>3.6.0.Final</version>
    </dependency>
    <!-- JAXB support -->
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>3.6.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>jaxrs-api</artifactId>
        <version>3.0.12.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.6.0.Final</version>
    </dependency>

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-arc</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-maven-plugin</artifactId>
        <version>${quarkus-plugin.version}</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <goals>
              <goal>build</goal>
              <goal>generate-code</goal>
              <goal>generate-code-tests</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${compiler-plugin.version}</version>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${surefire-plugin.version}</version>
        <configuration>
          <systemPropertyVariables>
            <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
            <maven.home>${maven.home}</maven.home>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>native</id>
      <activation>
        <property>
          <name>native</name>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${surefire-plugin.version}</version>
            <executions>
              <execution>
                <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
                <configuration>
                  <systemPropertyVariables>
                    <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
                    <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                    <maven.home>${maven.home}</maven.home>
                  </systemPropertyVariables>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      <properties>
        <quarkus.package.type>native</quarkus.package.type>
      </properties>
    </profile>
  </profiles>
</project>

編譯時碰到的問題, 因為環境是 java 11的,
使用太新的 resteasy 版本,所以發生些錯誤,查一下 java 11大約是何時 release後
找那個時間點左右的版本就行了
Caused by: org.apache.maven.plugin.MojoExecutionException: Dev mode process did not complete successfully
...
...
[ERROR] Using javaTool: /opt/java/jdk-11.0.8/bin/java
...
...
[ERROR] Failed to execute goal io.quarkus:quarkus-maven-plugin:1.10.3.Final:dev (default-cli) on project lifecycle-quickstart: Failed to run: Dev mode process did not complete successfully -> [Help 1]

編譯:
./mvnw compile quarkus:dev -Ddebug=5006 -Dquarkus.log.file.enable=true -Dquarkus.log.file.rotation.max-file-size=10M -Dquarkus.log.file.rotation.max-backup-index=20

ref:
1. What's New in Maven
2. quarkus guide
3. HowToDoInJava 

沒有留言:

張貼留言