Skip to content

Commit

Permalink
Merge pull request #25 from youngfreeFJS/feature/exec-timeout
Browse files Browse the repository at this point in the history
feature: LyrebirdClient 支持自定义请求超时时间
  • Loading branch information
youngfreeFJS authored Sep 9, 2021
2 parents 590043d + 679188f commit b7ae5e3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.meituan-dianping.lyrebird.sdk</groupId>
<artifactId>lyrebird-java-client</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>

<name>lyrebird-java-client</name>
<url>https://github.com/Meituan-Dianping/lyrebird-java-client</url>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/com/meituan/lyrebird/Lyrebird.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.meituan.lyrebird.client.api.*;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

public class Lyrebird {
private LyrebirdClient client;
Expand All @@ -20,7 +21,25 @@ public Lyrebird() {
}

public Lyrebird(String lyrebirdRemoteAddress) {
client = new LyrebirdClient(lyrebirdRemoteAddress);
this(lyrebirdRemoteAddress,TimeUnit.SECONDS,0,10,10,0);
}

public Lyrebird(
String lyrebirdRemoteAddress,
TimeUnit timeUnit,
long callTimeout,
long connectTimeout,
long readTimeout,
long writeTimeout
) {
client = new LyrebirdClient(
lyrebirdRemoteAddress,
timeUnit,
callTimeout,
connectTimeout,
readTimeout,
writeTimeout
);
}

/**
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,44 @@
import io.socket.client.IO;
import io.socket.client.Socket;
import java.net.URISyntaxException;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

public class LyrebirdClient {
private LyrebirdService lyrebirdService;
private Socket socket;

public LyrebirdClient(String lyrebirdRemoteAddress) {
public LyrebirdClient(
String lyrebirdRemoteAddress,
TimeUnit timeUnit,
long callTimeout,
long connectTimeout,
long readTimeout,
long writeTimeout
) {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
Retrofit retrofit = new Retrofit

OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.callTimeout(callTimeout, timeUnit)
.connectTimeout(connectTimeout,timeUnit)
.readTimeout(readTimeout,timeUnit)
.writeTimeout(writeTimeout,timeUnit);

Retrofit.Builder builder = new Retrofit
.Builder()
.baseUrl(lyrebirdRemoteAddress)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.build();
.client(httpClient.build());

Retrofit retrofit = builder.build();

// create service
lyrebirdService = retrofit.create(LyrebirdService.class);
}

Expand Down

0 comments on commit b7ae5e3

Please sign in to comment.