替代Guava的EvictingQueue的选项,该队列带有@Beta注解。

huangapple go评论84阅读模式
英文:

Alternatives to Guava's EvictingQueue, which is annotated with @Beta

问题

在我项目的关键部分,基本上允许对象被控制器异步接收,然后放入一个 Queue 中,由一个线程按顺序从队列中逐个进行处理,然后服务作出响应,较早处理的对象会一直保留在队列中,直到插入更新的项目。

回到过去(几个月前),我解决这个特定业务问题的 Queue 实现是使用 Guava 的 EvictingQueue,而现在它被标记为 @Beta,所以在未来的 Guava 版本中,应用的这部分功能可能会出现问题。

private final Queue<SomeRandomBusinessObject> items = Queues.synchronizedQueue(EvictingQueue.create(queueSize));

是否有任何线程安全且固定大小的替代方案来实现这个目标?

英文:

In a critical part of my project which basically allows objects to be received by a controller asynchronously, put into a Queue, processed sequentially from the queue one at a time by a thread, then service responds, older processed objects are kept in the queue until newer item insertion.

Back in time (months ago), my Queue implementation for solving this particular business specific issue behind this was to use Guava's EvictingQueue, which now is marked as @Beta, and so this part of the application can break in future Guava releases.

private final Queue&lt;SomeRandomBusinessObject&gt; items = Queues.synchronizedQueue(EvictingQueue.create(queueSize));

Are there any thread-safe and fixed-size alternatives to EvictingQueue to achieve this goal?

答案1

得分: 6

在你的帖子中有一些不准确/错误的地方,所以让我们试着找到共同的基础。

首先,Guava 中的任何新功能从一开始就会被注解为 @Beta,例如 EvictingQueue 在 15.0 版本中(链接指向 15.0 版本文档)。所以你可能在几个月前错过了这个事实,但没关系,因为……

……@Beta 实际上并不意味着它会在没有任何通知的情况下进行更改 - 相反,不久前,在从社区收到一些反馈后,Guava 开发人员制定了关于何时以及何种情况下可以进行更改的相当严格的政策。请参阅PhilosophyExplained wiki 页面,其中写道(我强调):

> # Beta APIs
>
> Beta API 代表了我们还没有准备好冻结的 Guava 功能,出于各种原因:因为这些方法可能没有足够的用户,因为它们可能会被移动,因为它们的使用可能过于狭窄,不能将它们包含在 Guava 中。
>
> 话虽如此,@Beta API 经过了充分的测试和支持,并且受到了与 Guava 的其余部分一样的关心和喜爱。

这意味着 EvictingQueue 的质量并不比它不是“beta 功能”的情况要差。

> @Beta 注解的最大含义是,注解的类或方法可能会发生更改。它们可以以任何方式进行修改,甚至可以随时删除。 如果你的代码本身就是一个库(即它在你自己无法控制的用户的 CLASSPATH 中使用),你不应该使用 beta API,除非你重新打包它们(例如使用 ProGuard)。

这可能是你在谈论“将来可能会中断”的时候提出的关切,但是……

> 尽管如此,@Beta 功能往往保持相对稳定。如果我们决定删除一个 @Beta 功能,通常会在删除之前的一个发布版本中将其标记为弃用。

所以不会默默地发生这种情况(据我观察,通常会有一个或多个版本的弃用过程)。

这就引出了我要提出的最后一点:

> 另一方面,如果你希望将某个功能移出 @Beta,请提交一个问题。 我们通常只有在有特定请求的情况下才会将功能从 @Beta 推广出去,所以如果你不提出要求,这是不会发生的。

总之,我建议你提交一个工单,以推广 EvictingQueue 并使其变为非@Beta,这将消除任何疑虑。另一方面,EvictingQueue 的实现非常简单且独立,因此如果它被移除(不太可能),你可以重新打包它(即使用 ProGuard)或者甚至将代码复制到你的项目中(附带所有许可证)。

英文:

There are couple of inaccuracies / mistakes in your post, so let's just try to find common ground.

First, any new feature in Guava is annotated as @Beta from the beginning, same is true for EvictingQueue in 15.0 (this links to 15.0 docs). So you probably missed that fact couple months ago, but that's OK, because...

...@Beta doesn't really mean it'll be changed without any notice -- on the contrary, some time ago, after some feedback from the community, Guava devs established pretty strict policy about what and when can be changed. See PhilosophyExplained wiki page, which says (emphasis mine):

> # Beta APIs
>
> Beta APIs represent Guava features that we aren't ready to freeze for whatever reason: because the methods might not find enough users, because they might be moved, because their uses might be too narrow to include them in Guava.
>
> That said, @Beta APIs are fully tested and supported, and treated with all the care and affection that the rest of Guava receives.

This means EvictingQueue quality is not worse than if it wasn't a "beta feature".

> The biggest connotation of the @Beta annotation is that annotated classes or methods are subject to change. They can be modified in any way, or even removed, at any time. If your code is a library itself (i.e. it is used on the CLASSPATH of users outside your own control), you should not use beta APIs, unless you repackage them (e.g. using ProGuard).

This could be the concern you brought up when talking about "braking up in the future", but...

> All this said, @Beta features tend to remain relatively stable. If we decide to delete a @Beta feature, we will typically deprecate it for one release before deleting it.

So it won't happen silently (as far as I observed, usually there's more than one release with deprecating though).

Which brings me the the last point:

> On the other hand, if you want something taken out of @Beta, file an issue. We generally promote features out of @Beta only when it's specifically requested, so if you don't ask, it won't happen.

To sum up: I'd suggest you to file a ticket to promote EvictingQueue and make it non-@Beta, which would remove any doubts about it. On the other hand, the EvictingQueue's implementation is quite simple and standalone, so if it's removed (unlikely) you can repakckage it (i.e. use ProGuard) or even copy the code to your project (with all the licenses).

huangapple
  • 本文由 发表于 2020年8月20日 22:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63506943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定