简体   繁体   English

java nio的优先选择器?

[英]priority selector with java nio?

I'm working with Amazon SQS queues, and I want to have two queues, let's say one with high priority tasks, and other with low priority tasks. 我正在使用Amazon SQS队列,我想有两个队列,比方说一个具有高优先级任务,另一个具有低优先级任务。

I want to create a setup where my app always consumes all high priority tasks first before it starts consuming the low priority ones (starvation is no problem in my case). 我想创建一个设置,使我的应用始终先消耗所有高优先级任务,然后再开始消耗低优先级任务(在我看来,饥饿是没有问题的)。

My first ideia was to have two cosumers, one for each queue, getting the messages from each queue and putting them into a java.util.PriorityQueue, but I wanted to avoid the inserting (and sorting/blocking/synchronizing) cost, because there will a high volume of messages. 我的第一个想法是拥有两个消费者,每个队列一个,从每个队列获取消息并将它们放入java.util.PriorityQueue,但是我想避免插入(和排序/阻塞/同步)开销,因为在那里将大量的消息。

Then, I thought about usign java.nio, but I couldn't figure out if this is possible. 然后,我想到了usign java.nio,但是我不知道这是否可行。

My idea was to still have the two consumers, but now they put their tasks in two nio.buffers (high priority buffer, low priority buffer), these buffers are bound to nio.channels (high p. channel, low p. channel), and then a selector should somehow consume first from high priority channel if possible, before consuming from low priority channel. 我的想法是仍然有两个使用者,但现在他们将任务放在两个nio.buffers(高优先级缓冲区,低优先级缓冲区)中,这些缓冲区绑定到nio.channels(高p.channel,低p.channel) ,然后选择器应该以某种方式首先从高优先级通道进行消耗,然后再从低优先级通道进行消耗。

I haven't found a way to configure this selection order. 我还没有找到配置此选择顺序的方法。 Is this possible? 这可能吗?

Any other alternative? 还有其他选择吗?

thanks in advance. 提前致谢。

Amazon SQS provide a function to query unread message size. Amazon SQS提供了查询未读消息大小的功能。

When you want get message,query unread message size from high priority SQS,if the size > 0,get message from high priority,else get message from low priority sqs. 当您要获取消息时,从高优先级SQS查询未读消息的大小,如果大小> 0,则从高优先级获取消息,否则从低优先级sqs获取消息。

AWSQSClient.getQueueUnreadSize(String queue_name);

How's this? 这个怎么样?

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

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