简体   繁体   English

具有持久性的HTTP POST请求的非阻塞队列

[英]Non-blocking queue of HTTP POST requests with persistence

Before we develop our custom solution, I'm looking for some kind of library, which provides: 在我们开发自定义解决方案之前,我正在寻找某种类型的库,它提供:

Non-blocking queue of HTTP requests HTTP请求的非阻塞队列

with these attributes: 具有以下属性:

  • Persisting requests to avoid it's loss in case of: 在下列情况下坚持要求避免损失:
    • network connectivity interruption 网络连接中断
    • application quit, forced GC on background app 应用程序退出,在后台应用程序上强制GC
    • etc.. 等等..
  • Possibility of putting out all these fields: 推出所有这些领域的可能性:
    • Address 地址
    • Headers
    • POST data 发布数据

So please, is there anything usable right know, what could save us whole day on developing this? 那么请问,有什么可以正确知道的,有什么可以节省我们整天开发这个?

Right now we don't need any callbacks on completed request and neither saving result data, as there won't be such. 现在我们不需要对已完成的请求进行任何回调,也不需要保存结果数据,因为不会有这样的回调。

In my humble opinion, a good and straightforward solution would be to develop your own layer (which shouldn't be so complicated) using a sophisticated framework for connection handling, such as Netty https://netty.io/ , together with a sophisticated framework for asynchronous processing, such as Akka http://akka.io/ 在我看来,一个好的和直接的解决方案是使用复杂的连接处理框架(如Netty https://netty.io/ )和复杂的开发层(不应该这么复杂)异步处理框架,如Akka http://akka.io/

Let's first look inside Netty support for http at http://static.netty.io/3.5/guide/#architecture.8 : 让我们首先在http :http: //static.netty.io/3.5/guide/#architecture.8中查看Netty对http的支持:

4.3. 4.3。 HTTP Implementation HTTP实现

HTTP is definitely the most popular protocol in the Internet. HTTP绝对是Internet上最受欢迎的协议。 There are already a number of HTTP implementations such as a Servlet container. 已经有许多HTTP实现,例如Servlet容器。 Then why does Netty have HTTP on top of its core? 那为什么Netty在其核心上拥有HTTP呢?

Netty's HTTP support is very different from the existing HTTP libraries. Netty的HTTP支持与现有的HTTP库非常不同。 It gives you complete control over how HTTP messages are exchanged at a low level. 它使您可以完全控制HTTP消息在低级别的交换方式。 Because it is basically the combination of an HTTP codec and HTTP message classes, there is no restriction such as an enforced thread model. 因为它基本上是HTTP编解码器和HTTP消息类的组合,所以没有限制,例如强制线程模型。 That is, you can write your own HTTP client or server that works exactly the way you want. 也就是说,您可以编写自己的HTTP客户端或服务器,它们可以按照您想要的方式工作。 You have full control over everything that's in the HTTP specification, including the thread model, connection life cycle, and chunked encoding. 您可以完全控制HTTP规范中的所有内容,包括线程模型,连接生命周期和分块编码。

And now let's dig inside Akka . 现在我们来挖掘阿卡吧 Akka is a framework which provides an excellent abstraction on the top of Java concurrent API, and it comes with API in Java or Scala. Akka是一个框架,它在Java并发API的顶部提供了一个很好的抽象,它带有Java或Scala的API。

  • It provides you a clear way to structure your application as a hierarchy of actors : 它为您提供了一种清晰的方法来将您的应用程序构建为actor的层次结构:
    • Actors communicate through message passing, using immutable message so that you have not to care about thread-safety 参与者通过消息传递进行通信,使用不可变消息,这样您就不必关心线程安全
    • Actors messages are stored in message boxes, which can be durable Actors消息存储在消息框中,这些消息框可以是持久的
    • Actors are responsible for supervising their children 演员负责监督他们的孩子
    • Actors can be run on one or more JVM and can communicate using a wide numbers of protocols Actor可以在一个或多个JVM上运行,并且可以使用大量协议进行通信
  • It provides a lightweight abstraction for asynchronous processing , Future , which is easier to use then Java Futures. 它为异步处理提供了一个轻量级的抽象, Future ,它比Java Futures更容易使用。
  • It provides other fancy stuff such as Event Bus, ZeroMQ adapter, Remoting support, Dataflow concurrency, Scheduler 它提供了其他奇特的东西,如事件总线,ZeroMQ适配器,远程处理支持,数据流并发,调度程序

Once you become familiar with the two frameworks, it turns out that what you need can easily be coded through them. 一旦熟悉了这两个框架,就会发现您需要的内容可以通过它们轻松编码。

In fact, what you need is an http proxy coded in Netty, that upon a request receival sends immediately a message to an Akka Actor of type FSM (http://doc.akka.io/docs/akka/2.0.2/java/fsm.html) which using a durable mailbox (http://doc.akka.io/docs/akka/2.0.2/modules/durable-mailbox.html ) 事实上,你需要的是一个在Netty中编码的http代理,根据请求,接收器会立即向FSM类型的Akka Actor发送消息(http://doc.akka.io/docs/akka/2.0.2/java /fsm.html)使用持久邮箱 (http://doc.akka.io/docs/akka/2.0.2/modules/durable-mailbox.html)

Here is a link to open-source library that was a Master Thesis of a student at Czech Technical University in Prague. 以下是开源图书馆的链接,该图书馆是布拉格捷克技术大学学生的硕士论文。 It is very large and powerful library and mainly focuses on location. 它是一个非常庞大和强大的图书馆,主要关注位置。 The good thing about it, though, is that it omitted the headers and other -ish that REST has. 不过,关于它的好处是它省略了REST所具有的标题和其他东西。

It is the latest fork and hopefully it will give you at least inspiration for "own" solution. 这是最新的分支,希望它至少会给你“自己的”解决方案的灵感。

how about those concurrent collections: 那些并发集合怎么样:

http://mcg.cs.tau.ac.il/projects/concurrent-data-structures http://mcg.cs.tau.ac.il/projects/concurrent-data-structures

i hope that the license is ok . 我希望许可证还可以。

You'll want to have a look to these to posts. 你想看看这些帖子。 (added at the end of the document) (在文件末尾添加)

Very basically an approach that works in a proficient way for me is to separate requests from the queue and the executor. 基本上,一种以熟练的方式为我工作的方法是将请求与队列和执行程序分开。 Requests are executed as Runnables or Callables. 请求以Runnables或Callables的形式执行。 Inherit from them to create different kind of requests to your API or service. 继承它们以向您的API或服务创建不同类型的请求。 Set them up there adding headers and or body prior to to executing them. 在执行它们之前将它们添加到那里添加标题和/或正文。

Enqueue those requests in a queue (choose which fits better for you - I'd say LinkedBlockingQueue will make the job) linked to an executor from within a bound service and calling them from your activity or any other scope. 将这些请求排入队列(选择哪个更适合您 - 我会说LinkedBlockingQueue将完成工作)从绑定服务中链接到执行程序并从您的活动或任何其他范围调用它们。 If you don't need to get responses and callbacks you can avoid using Guava for listening to futures or create your own callbacks. 如果您不需要获得响应和回调,则可以避免使用Guava来收听期货或创建自己的回调。

I'll stay tuned. 我会保持关注。 If you need more depth I can post some specific pieces of code. 如果您需要更多深度,我可以发布一些特定的代码。 There's the source of a basic example in the first link though. 但是,第一个链接中有一个基本示例的来源。

http://ugiagonzalez.com/2012/08/03/using-runnables-queues-and-executors-to-perform-tasks-in-background-threads-in-android/ http://ugiagonzalez.com/2012/08/03/using-runnables-queues-and-executors-to-perform-tasks-in-background-threads-in-android/

http://ugiagonzalez.com/2012/07/02/theres-life-after-asynctasks-in-android/ http://ugiagonzalez.com/2012/07/02/theres-life-after-asynctasks-in-android/

Update: 更新:

You can create another queue for those requests that were impossible to execute. 您可以为那些无法执行的请求创建另一个队列。 One approach that comes to my mind would be to add all your failed requests to the retry queue. 我想到的一种方法是将所有失败的请求添加到重试队列中。 The retry queue would be trying to re-run these tasks while the phone still thinks that there's any kind of internet connection available. 重试队列将尝试重新运行这些任务,而手机仍然认为有任何类型的互联网连接可用。 In the request object you can set a max number of retrials and compare it to a currentRetry number increasing it in every retrial. 在请求对象中,您可以设置最大重试次数,并将其与每次重试时增加的当前重试次数进行比较。 Mmm this might be interesting. 嗯,这可能很有趣。 I'll definitely think about including that in my library. 我肯定会考虑在我的图书馆中加入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM