如何在我的 HttpClient 执行器中遵循单一职责原则?

2024-05-25

我在用RestTemplate http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html as my HttpClient执行 URL,服务器将返回一个 json 字符串作为响应。客户将通过以下方式调用该库DataKey对象具有userId in it.

  • 使用给定的userId,我会找出哪些机器可以获取数据,然后将这些机器存储在LinkedList,这样我就可以按顺序执行它们。
  • 之后我将检查第一个主机名是否在阻止列表中。如果它不在阻止列表中,那么我将使用列表中的第一个主机名创建一个 URL 并执行它,如果响应成功,则返回响应。但是假设如果第一个主机名在阻止列表中,那么我将尝试获取列表中的第二个主机名并创建 url 并执行它,所以基本上,在创建 URL 之前首先找到不在阻止列表中的主机名.
  • 现在,假设我们选择了不在阻止列表中的第一个主机名并执行了 URL,但服务器因某种原因关闭或没有响应,那么我将执行列表中的第二个主机名并继续执行此操作,直到获得成功的响应。但请确保它们不在阻止列表中,因此我们需要遵循上述要点。
  • 如果所有服务器都已关闭或位于阻止列表中,那么我可以简单地记录并返回服务不可用的错误。

下面是我的 DataClient 类,它将由客户调用,他们将通过DataKey反对getData method.

public class DataClient implements Client {

    private RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
    private ExecutorService service = Executors.newFixedThreadPool(15);

    public Future<DataResponse> getData(DataKey key) {
        DataExecutorTask task = new DataExecutorTask(key, restTemplate);
        Future<DataResponse> future = service.submit(task);

        return future;
    }
}

下面是我的 DataExecutorTask 类:

public class DataExecutorTask implements Callable<DataResponse> {

    private DataKey key;
    private RestTemplate restTemplate;

    public DataExecutorTask(DataKey key, RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
        this.key = key;
    }

    @Override
    public DataResponse call() {
        DataResponse dataResponse = null;
        ResponseEntity<String> response = null;

        MappingsHolder mappings = ShardMappings.getMappings(key.getTypeOfFlow());

        // given a userId, find all the hostnames 
        // it can also have four hostname or one hostname or six hostname as well in the list
        List<String> hostnames = mappings.getListOfHostnames(key.getUserId());

        for (String hostname : hostnames) {
            // If host name is null or host name is in local block list, skip sending request to this host
            if (ClientUtils.isEmpty(hostname) || ShardMappings.isBlockHost(hostname)) {
                continue;
            }
            try {
                String url = generateURL(hostname);
                response = restTemplate.exchange(url, HttpMethod.GET, key.getEntity(), String.class);

                if (response.getStatusCode() == HttpStatus.NO_CONTENT) {
                    dataResponse = new DataResponse(response.getBody(), DataErrorEnum.NO_CONTENT,
                            DataStatusEnum.SUCCESS);
                } else {
                    dataResponse = new DataResponse(response.getBody(), DataErrorEnum.OK,
                            DataStatusEnum.SUCCESS);
                }

                break;
                // below codes are duplicated looks like
            } catch (HttpClientErrorException ex) {
                HttpStatusCodeException httpException = (HttpStatusCodeException) ex;
                DataErrorEnum error = DataErrorEnum.getErrorEnumByException(httpException);
                String errorMessage = httpException.getResponseBodyAsString();
                dataResponse = new DataResponse(errorMessage, error, DataStatusEnum.ERROR);

                return dataResponse;
            } catch (HttpServerErrorException ex) {
                HttpStatusCodeException httpException = (HttpStatusCodeException) ex;
                DataErrorEnum error = DataErrorEnum.getErrorEnumByException(httpException);
                String errorMessage = httpException.getResponseBodyAsString();
                dataResponse = new DataResponse(errorMessage, error, DataStatusEnum.ERROR);

                return dataResponse;
            } catch (RestClientException ex) {
                // if it comes here, then it means some of the servers are down so adding it into block list
                ShardMappings.blockHost(hostname);
            }
        }

        if (ClientUtils.isEmpty(hostnames)) {
            dataResponse = new DataResponse(null, DataErrorEnum.PERT_ERROR, DataStatusEnum.ERROR);
        } else if (response == null) { // either  all the servers are down or all the servers were in block list
            dataResponse = new DataResponse(null, DataErrorEnum.SERVICE_UNAVAILABLE, DataStatusEnum.ERROR);
        }

        return dataResponse;
    }
}

我的阻止列表每 1 分钟不断从另一个后台线程更新。如果任何服务器关闭并且没有响应,那么我需要使用以下命令来阻止该服务器 -

ShardMappings.blockHost(hostname);

为了检查是否有任何服务器在阻止列表中,我使用这个 -

ShardMappings.isBlockHost(hostname);

我回来了SERVICE_UNAVAILABLE如果服务器已关闭或位于阻止列表中,则基于response == null check, 不确定这是否是正确的方法。

我想我根本没有遵循单一责任原则。 任何人都可以提供一个例子,这里使用 SRP 原则的最佳方法是什么?

经过深思熟虑,我能够提取如下所示的主机类,但不确定在我上面使用这个的最好方法是什么 DataExecutorTask class.

public class Hosts {

    private final LinkedList<String> hostsnames = new LinkedList<String>();

    public Hosts(final List<String> hosts) {
        checkNotNull(hosts, "hosts cannot be null");
        this.hostsnames.addAll(hosts);
    }

    public Optional<String> getNextAvailableHostname() {
        while (!hostsnames.isEmpty()) {
            String firstHostname = hostsnames.removeFirst();
            if (!ClientUtils.isEmpty(firstHostname) && !ShardMappings.isBlockHost(firstHostname)) {
                return Optional.of(firstHostname);
            }
        }
        return Optional.absent();
    }

    public boolean isEmpty() {
        return hostsnames.isEmpty();
    }
}

你的担心是有道理的。首先我们看一下原始数据执行器做了什么:

First, it is getting list of hostnames
Next, it loops through every hostnames that do the following things:
    It checks whether the hostname is valid to send request.
    If not valid: skip. 
    Else continue.
        Generate the URL based on hostname
        Send the request
        Translate the request response to domain response
        Handle exceptions
If the hostnames is empty, generate an empty response
Return response

现在,我们可以做什么来遵循SRP?正如我所看到的,我们可以将这些操作分为一些组。我所看到的是,这些操作可以分为:

HostnameValidator:        checks whether the hostname is valid to send request
--------------
HostnameRequestSender:    Generate the URL
                          Send the request
--------------
HttpToDataResponse:       Translate the request response to domain response
--------------
HostnameExceptionHandler: Handle exceptions

也就是说,一种解耦操作并遵循 SRP 的方法。还有其他方法,例如简化操作:

First, it is getting list of hostnames
If the hostnames is empty, generate an empty response
Next, it loops through every hostnames that do the following things:
    It checks whether the hostname is valid to send request
    If not valid: remove hostname
    Else: Generate the URL based on hostname
Next, it loops through every valid hostnames that do the following things:
    Send the request
    Translate the request response to domain response
    Handle exceptions
Return response

那么还可以拆分为:

HostnameValidator:        checks whether the hostname is valid to send request
--------------
ValidHostnameData:        Getting list of hostnames
                          Loops through every hostnames that do the following things:
                              Checks whether the hostname is valid to send request
                              If not valid: remove hostname
                              Else: Generate the URL based on hostname
--------------
HostnameRequestSender:    Send the request
--------------
HttpToDataResponse:       Translate the request response to domain response
--------------
HostnameExceptionHandler: Handle exceptions

当然还有其他方法可以做到。我将实现细节留空,因为有很多方法可以实现它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在我的 HttpClient 执行器中遵循单一职责原则? 的相关文章

随机推荐

  • PHP:从字符串中修剪子字符串的最佳方法

    想要处理一组字符串 并修剪一些结尾 myEnding 从最后每个字符串的如果存在的话 最简单的方法是什么 我知道使用正则表达式一切皆有可能 但这似乎是一项简单的任务 我想知道是否存在一个简单的工具来实现这一点 Thanks Gidi 我选择
  • GitHub MarkDown:可以使用宏和变量吗?

    我一直在学习 github markdown 我有一个关于变量和宏的问题 是否可以定义变量或宏来防止重复打印文本块 用例是我有一个表生成一个大的超链接网格 链接如下所示 http www a big long big big long hy
  • 在名称为 [重复] 的 DispatcherServlet 中未找到带有 URI 的 HTTP 请求的映射...

    这个问题在这里已经有答案了 我已经检查了 stackoverflow 上几乎所有相关文章 但我就是无法解决我的问题 这是代码 网络 xml
  • Python textwrap.wrap 导致 \n 问题

    所以我只是重新格式化了一堆代码以合并textwrap wrap 却发现我所有的 n都消失了 这是一个例子 from textwrap import wrap def wrapAndPrint msg width 25 wrap msg to
  • 超时后无法部署到 AWS Elastic Beanstalk

    我是 AWS Elastic Beanstalk 的新手 这是我第一次遇到此问题 我尝试通过将我的应用程序 zip 上传到实例来部署新版本 更新完成后出现错误 已完成 但命令行超时 我增加了配置文件中的超时并重新部署 之后我收到了此消息 在
  • Junit测试中LocalDateTime反序列化的问题

    我有问题LocalDateTime反序列化Junit测试 我有简单的REST API返回一些DTO目的 当我呼叫端点时 响应没有问题 它是正确的 然后我尝试编写单元测试 得到MvcResult并使用ObjectMapper将其转换为我的DT
  • F# 静态成员类型约束

    我正在尝试定义一个函数 factorize 它使用类似于 Seq sum 的结构类型约束 需要静态成员 Zero One 和 以便它可以与 int long bigint 等一起使用 似乎无法获得正确的语法 并且无法找到有关该主题的大量资源
  • CSS 中“body > *”是什么意思?

    我试图了解 jQTouch 实现的 CSS 效果 http www jqtouch com http www jqtouch com 它有一些 CSS 定义 其中包含如下语法body gt body gt webkit backface v
  • 为什么这个基本的 imagejpeg() resizer 返回黑色图像?

    EDIT 感谢您的所有回答 特别是 Mailerdaimon 他注意到我没有在imagecopyresampled功能 我不再得到黑色图像 但我仍然得到一些黑色部分 所以我认为我的比例公式应该更新 如果我上传横向图像 新图像的高度小于 17
  • 在 Objective-C 中使用类别重写方法

    我可以使用类类别来覆盖已使用类别实现的方法吗 像这样 1 原始方法 BOOL method return true 2 重写方法 BOOL method NSLog error return true 这可行吗 还是非法的 From 苹果文
  • 为 Swift 中的Optional提供默认值?

    如果您只想在 nil 的情况下提供默认值 那么在 Swift 中处理可选值的习惯用法似乎过于冗长 if let value optionalValue do something with value else do the same thi
  • userDidAcceptCloudKitShareWith 未被调用

    func application application UIApplication userDidAcceptCloudKitShareWith cloudKitShareMetadata CKShare Metadata 单击共享 cl
  • fork 和 exec 之间的区别

    两者有什么区别fork and exec 指某东西的用途fork and exec它体现了 UNIX 的精神 它提供了一种非常简单的方法来启动新进程 The fork调用基本上复制了当前进程 在almost任何方式 并非所有内容都会被复制
  • Codeigniter 中的多个查询[重复]

    这个问题在这里已经有答案了 可能的重复 MYSQL在codeigniter中多次插入 https stackoverflow com questions 2790638 mysql multiple insert in codeignite
  • jqGrid如何将额外的类应用于标题列

    我想在特定列上应用一个额外的类 我知道通过在 colModel 中指定这一点对于行是可能的 但这些类仅应用于 结果行 中的列 而不应用于标题 我想要达到的是通过简单的类名隐藏较小视口的特定列 与 Twitter Bootstrap 一起使用
  • 从数组中输入多个数字,每个数字检查是否为整数

    每个人 我希望有人能帮我弄清楚C语言的一些东西 这是我第一次认真地做IT方面的作业 我没有经验 而且我正在电子学习中学习 所以老师的帮助不是很好 我需要用C语言开发控制台应用程序 用户需要输入10个整数 如果插入的数字不是整数 需要输出错误
  • 为什么我们在 Lisp 中需要 funcall?

    为什么我们必须使用funcall在 Common Lisp 中调用高阶函数 例如 为什么我们必须使用 defun foo test func args funcall test func args 而不是更简单的 defun bar tes
  • 我可以在 Express POST 请求中进行 DOM 操作吗?

    我正在使用基本的 HTML CSS 前端 目前有一个登陆页面 上面有一个表单 可将 一些数据发送到数据库 当请求完成后 它期待某种响应 在这种情况下 我正在重新渲染页面 但是 我想用某种感谢消息替换表单 以便用户知道它已正确发送 我尝试过简
  • 如何在Python中比较列表列表中的元素以及比较列表列表中的键?

    我有以下顺序 seq ATG ATG ATG ATG GAC GAT GAA CCT GCC GCG GCA GCT 这是一个字典键 用于存储每个密码子的氨基酸值 三联碱基 例如ATG GCT etc aminoacid TTT F TTC
  • 如何在我的 HttpClient 执行器中遵循单一职责原则?

    我在用RestTemplate http docs spring io spring docs current javadoc api org springframework web client RestTemplate html as