-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix-0000: quick update & refactoring
- Loading branch information
Showing
4 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/demo/http/FutureRequestExecutionServiceDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.demo.http; | ||
|
||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import java.util.concurrent.Future; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.ResponseHandler; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.protocol.HttpClientContext; | ||
import org.apache.http.impl.client.FutureRequestExecutionMetrics; | ||
import org.apache.http.impl.client.FutureRequestExecutionService; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.impl.client.HttpRequestFutureTask; | ||
/** | ||
* https://www.cnblogs.com/papering/p/9963987.html | ||
* Preface http://hc.apache.org/httpcomponents-client-ga/tutorial/html/preface.html | ||
*/ | ||
public class FutureRequestExecutionServiceDemo { | ||
public static void main(String[] args) { | ||
FutureRequestExecutionServiceDemo demo = new FutureRequestExecutionServiceDemo(); | ||
demo.test(); | ||
} | ||
|
||
private void test() { | ||
HttpClient httpClient = HttpClientBuilder.create().setMaxConnPerRoute(5).build(); | ||
ExecutorService executorService = Executors.newFixedThreadPool(5); | ||
FutureRequestExecutionService futureRequestExecutionService = | ||
new FutureRequestExecutionService(httpClient, executorService); | ||
HttpRequestFutureTask<Void> execute = futureRequestExecutionService.execute(new HttpGet("http://www.google.com"), HttpClientContext.create(), new DummyResponseHander()); | ||
FutureRequestExecutionMetrics metrics = futureRequestExecutionService.metrics(); | ||
System.out.println("Active connection count: " + metrics.getActiveConnectionCount()); | ||
System.out.println("Scheduled connection count: " + metrics.getScheduledConnectionCount()); | ||
System.out.println("Successful connection count: " + metrics.getSuccessfulConnectionCount()); | ||
System.out.println("Successful connection average duration: " + metrics.getSuccessfulConnectionAverageDuration()); | ||
System.out.println("Failed connection count: " + metrics.getFailedConnectionCount()); | ||
System.out.println("Failed connection average duration: " + metrics.getFailedConnectionAverageDuration()); | ||
System.out.println("Task count: " + metrics.getTaskCount()); | ||
System.out.println("Request count: " + metrics.getRequestCount()); | ||
System.out.println("Request average duration: " + metrics.getRequestAverageDuration()); | ||
System.out.println("Task average duration: " + metrics.getTaskAverageDuration()); | ||
executorService.shutdown(); | ||
|
||
// metrics.getActiveConnectionCount(); // currently active connections | ||
// metrics.getScheduledConnectionCount(); // currently scheduled connections | ||
// metrics.getSuccessfulConnectionCount(); // total number of successful requests | ||
// metrics.getSuccessfulConnectionAverageDuration(); // average request duration | ||
// metrics.getFailedConnectionCount(); // total number of failed tasks | ||
// metrics.getFailedConnectionAverageDuration(); // average duration of failed tasks | ||
// metrics.getTaskCount(); // total number of tasks scheduled | ||
// metrics.getRequestCount(); // total number of requests | ||
// metrics.getRequestAverageDuration(); // average request duration | ||
// metrics.getTaskAverageDuration(); // average task duration | ||
} | ||
|
||
private class DummyResponseHander implements ResponseHandler<Void> { | ||
@Override | ||
public Void handleResponse(org.apache.http.HttpResponse response) { | ||
System.out.println("Response received"); | ||
response.setStatusCode(200); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.demo.lombok; | ||
; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.experimental.Accessors; | ||
|
||
@Accessors(fluent = true) | ||
class Person { | ||
@Getter | ||
@Setter | ||
private String name; | ||
|
||
@Getter | ||
@Setter | ||
private int age; | ||
} | ||
|
||
public class LombokDemo { | ||
public static void main(String[] args) { | ||
Person person = new Person(); | ||
person.name("John").age(25); | ||
|
||
System.out.println("Name: " + person.name()); | ||
System.out.println("Age: " + person.age()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.demo.redis; | ||
|
||
|
||
import redis.clients.jedis.Jedis; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class RedisExample { | ||
public static void main(String[] args) { | ||
// use HostAndPort | ||
Jedis jedis = new Jedis("localhost", 6379); | ||
|
||
System.out.println("Connection to server successfully"); | ||
//check whether server is running or not | ||
System.out.println("Server is running: "+jedis.ping()); | ||
|
||
//set the data in redis string | ||
jedis.set("tutorial-name", "Redis tutorial"); | ||
System.out.println("Stored string in redis:: "+ jedis.get("username")); | ||
|
||
//store data in redis list | ||
jedis.lpush("tutorial-list", "Redis"); | ||
jedis.lpush("tutorial-list", "Mongodb"); | ||
jedis.lpush("tutorial-list", "Mysql"); | ||
|
||
// Get the stored data and print it | ||
List<String> list = jedis.lrange("tutorial-list", 0 ,5); | ||
|
||
for(int i = 0; i<list.size(); i++) { | ||
System.out.println("Stored string in redis:: "+list.get(i)); | ||
} | ||
|
||
//store data in redis list | ||
// Get the stored data and print it | ||
Set<String> set = jedis.keys("*"); | ||
|
||
for (String key : set) { | ||
System.out.println("List of stored keys:: "+key); | ||
} | ||
} | ||
} |