Java Rekognition客户端根据每个请求不断重建
我正在使用AWS Rekognion构建一个应用程序,我意识到每次我向服务发出请求时,都会不断重新建立与AWS的连接,这会降低性能。有没有办法使一个连接在整个会话中保持不变?我的代码如下所示:Java Rekognition客户端根据每个请求不断重建,java,amazon-web-services,amazon-s3,amazon-ec2,amazon-rekognition,Java,Amazon Web Services,Amazon S3,Amazon Ec2,Amazon Rekognition,我正在使用AWS Rekognion构建一个应用程序,我意识到每次我向服务发出请求时,都会不断重新建立与AWS的连接,这会降低性能。有没有办法使一个连接在整个会话中保持不变?我的代码如下所示: private static final AmazonRekognition rekognitionClient = RekognitionUtil.setupRekognitionClient(); private static AWSCredentialsProvider setupCredenti
private static final AmazonRekognition rekognitionClient = RekognitionUtil.setupRekognitionClient();
private static AWSCredentialsProvider setupCredentials(String accessKey, String secretKey) {
AWSCredentialsProvider provider = new AWSCredentialsProvider() {
@Override
public AWSCredentials getCredentials() {
return new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
LOG.info("Access key: " + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey));
return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey);
}
@Override
public String getAWSSecretKey() {
LOG.info("Secret key: " + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey));
return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey);
}
};
}
@Override
public void refresh() {
}
};
return provider;
}
private static AmazonRekognition setupRekognitionClient() {
AWSCredentialsProvider provider = setupCredentials("xxxx", "xxxx");
return AmazonRekognitionClientBuilder.standard().withCredentials(provider).withRegion(ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_REGION,"xxx")).build();
}
private static AWSCredentialsProvider setupCredentials(String accessKey, String secretKey) {
AWSCredentialsProvider provider = new AWSCredentialsProvider() {
@Override
public AWSCredentials getCredentials() {
return new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
LOG.info("Access key: " + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey));
return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey);
}
@Override
public String getAWSSecretKey() {
LOG.info("Secret key: " + ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey));
return ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey);
}
};
}
@Override
public void refresh() {
}
};
return provider;
}
private static AmazonRekognition setupRekognitionClient() {
AWSCredentialsProvider provider = setupCredentials("xxxx", "xxx");
return AmazonRekognitionClientBuilder.standard().withCredentials(provider).withRegion(ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_REGION,"xxx")).build();
}
public static String searchCollectionByFace(String collectionId, ByteBuffer sourceByteBuffer) throws Exception {
LOG.info("Searching face collection by face...");
String faceId = "";
try {
ObjectMapper objectMapper = new ObjectMapper();
// Get an image object from S3 bucket.
Image image = new Image().withBytes(sourceByteBuffer);
// Search collection for faces similar to the largest face in the image.
SearchFacesByImageRequest searchFacesByImageRequest = new SearchFacesByImageRequest().withCollectionId(collectionId).withImage(image).withFaceMatchThreshold(70F).withMaxFaces(2);
SearchFacesByImageResult searchFacesByImageResult = rekognitionClient.searchFacesByImage(searchFacesByImageRequest);
List<FaceMatch> faceImageMatches = searchFacesByImageResult.getFaceMatches();
for (FaceMatch face : faceImageMatches) {
LOG.info(face.getFace().getFaceId());
if(face.getFace().getConfidence() > SIMILARITY_LIMIT){
faceId = face.getFace().getFaceId();
}
}
return faceId;
} catch (Exception ex) {
LOG.error("Error has occurred searching for face", ex);
throw new Exception();
}
}
private static final AmazonRekognition rekognitionClient=RekognitionUtil.setupRekognitionClient();
专用静态AWSCredentials提供程序设置凭据(字符串accessKey、字符串secretKey){
AWSCredentialsProvider=新的AWSCredentialsProvider(){
@凌驾
公共AWSCredentials getCredentials(){
返回新的AWSCredentials(){
@凌驾
公共字符串getAWSAccessKeyId(){
LOG.info(“访问键:+ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_Access_key,accessKey));
返回ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey);
}
@凌驾
公共字符串getAWSSecretKey(){
LOG.info(“密钥:+ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_Secret_key,secretKey));
返回ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey);
}
};
}
@凌驾
公共无效刷新(){
}
};
退货供应商;
}
私有静态AmazonRekognition设置RekognitionClient(){
AWSCredentialsProvider=设置凭证(“xxxx”、“xxxx”);
返回AmazonRekognitionClientBuilder.standard().withCredentials(provider).withRegion(ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_REGION,“xxx”).build();
}
专用静态AWSCredentials提供程序设置凭据(字符串accessKey、字符串secretKey){
AWSCredentialsProvider=新的AWSCredentialsProvider(){
@凌驾
公共AWSCredentials getCredentials(){
返回新的AWSCredentials(){
@凌驾
公共字符串getAWSAccessKeyId(){
LOG.info(“访问键:+ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_Access_key,accessKey));
返回ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_ACCESS_KEY,accessKey);
}
@凌驾
公共字符串getAWSSecretKey(){
LOG.info(“密钥:+ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_Secret_key,secretKey));
返回ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_SECRET_KEY,secretKey);
}
};
}
@凌驾
公共无效刷新(){
}
};
退货供应商;
}
私有静态AmazonRekognition设置RekognitionClient(){
AWSCredentialsProvider=设置凭证(“xxxx”、“xxx”);
返回AmazonRekognitionClientBuilder.standard().withCredentials(provider).withRegion(ConfigUtil.getString(ConfigConstants.CONFIG_REKOGNITION_REGION,“xxx”).build();
}
公共静态字符串searchCollectionByFace(String collectionId,ByteBuffer sourceByteBuffer)引发异常{
LOG.info(“按人脸搜索人脸集合…”);
字符串faceId=“”;
试一试{
ObjectMapper ObjectMapper=新的ObjectMapper();
//从S3存储桶中获取图像对象。
Image Image=new Image().withBytes(sourceByteBuffer);
//搜索与图像中最大面相似的面集合。
SearchFacesByImageRequest SearchFacesByImageRequest=新的SearchFacesByImageRequest()。带有collectionId(collectionId)。带有图像(image)。带有人脸匹配阈值(70F)。带有MaxFaces(2);
SearchFacesByImageResult SearchFacesByImageResult=rekognitionClient.searchFacesByImage(searchFacesByImageRequest);
List faceImageMatches=searchFacesByImageResult.getFaceMatches();
用于(FaceMatch面:faceImageMatches){
LOG.info(face.getFace().getFaceId());
if(face.getFace().getConfidence()>相似性限制){
faceId=face.getFace().getFaceId();
}
}
返回faceId;
}捕获(例外情况除外){
LOG.error(“搜索人脸时出错”,例如);
抛出新异常();
}
}
您可以尝试微调:
在客户端配置中,.SDK已预先配置了诸如max connections之类的默认值。您可以在此处修改某些行为,请参见和。谢谢!谢谢你!