Skip to content

Commit

Permalink
[update][reader][redisreader] Migrate Apache HttpComponents from 4.x …
Browse files Browse the repository at this point in the history
…to 5.x
  • Loading branch information
wgzhao committed Oct 9, 2024
1 parent febf6bd commit 09ab6d6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 48 deletions.
10 changes: 2 additions & 8 deletions plugin/reader/redisreader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,9 @@
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5-fluent</artifactId>
<version>${httpclient.version}</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,14 @@
import com.wgzhao.addax.common.spi.Reader;
import com.wgzhao.addax.common.util.Configuration;
import com.wgzhao.addax.plugin.reader.redisreader.impl.SentinelReplicator;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.hc.client5.http.fluent.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.HostAndPort;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -181,7 +175,7 @@ public void startRead(RecordSender recordSender)
String masterName = connection.getString(RedisKey.MASTER_NAME, null);
File file = new File(UUID.randomUUID() + ".rdb");
if (uri.startsWith("http") || uri.startsWith("https")) {
this.download(new URI(uri), file);
Request.get(uri).execute().saveContent(file);
}
else if (uri.startsWith("tcp")) {
this.dump(uriToHosts(uri), mode, connection.getString(RedisKey.AUTH), masterName, file);
Expand Down Expand Up @@ -335,6 +329,7 @@ private boolean matchDB(int db)
*
* @param hosts list of {@link HostAndPort}
* @param mode redis running mode, cluster, master/slave, sentinel or cluster
* @param auth auth password
* @param masterName master name for sentinel mode
* @param outFile file which dump to
* @throws IOException file not found
Expand All @@ -344,7 +339,7 @@ private void dump(List<HostAndPort> hosts, String mode, String auth, String mast
throws IOException, URISyntaxException
{
LOG.info("mode = {}", mode);
OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
OutputStream out = new BufferedOutputStream(Files.newOutputStream(outFile.toPath()));
RawByteListener rawByteListener = rawBytes -> {
try {
out.write(rawBytes);
Expand Down Expand Up @@ -411,37 +406,6 @@ private List<HostAndPort> uriToHosts(String uris)
return result;
}

/**
* 下载远程rdb文件
*
* @param uri uri
* @param outFile file will be written
* @throws IOException when can not reach to uri
*/
private void download(URI uri, File outFile)
throws IOException
{
CloseableHttpClient httpClient = this.getHttpClient();
CloseableHttpResponse response = httpClient.execute(new HttpGet(uri));
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
byte[] bytes = new byte[4096 * 1000];

int len;
try (FileOutputStream out = new FileOutputStream(outFile)) {
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
in.close();
}
}

private CloseableHttpClient getHttpClient()
{
return HttpClientBuilder.create().build();
}

private void collectType(int type)
{
String name = getTypeName(type);
Expand Down

0 comments on commit 09ab6d6

Please sign in to comment.