队列 - 请问Java里的LinkedBlockingQueue如何在分布式下使用
黄舟
黄舟 2017-04-18 10:26:23
0
4
927

如题,请问Java里的LinkedBlockingQueue如何在分布式下使用

因为现在还没有使用MQ等中间件,所以使用了Java里的LinkedBlockingQueue来做队列

可是这个队列只能存在本地,一旦集群的话,每台服务器上就都有一个队列在跑了,就成了多个队列了

我希望能改造成在分布式环境下也只有一个队列,可是无从下手

希望前辈们能指导迷津

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(4)
PHPzhong

For distribution, you should use middleware. I don’t know how to apply the things in concurrent to a distributed environment. It should be not simple, otherwise there wouldn’t be so many MQs.

Peter_Zhu

hazelcast

import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
 
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class DistributedQueue {
    public static void main(String[] args) throws InterruptedException {
        Config config = new Config();
        HazelcastInstance h = Hazelcast.newHazelcastInstance(config);
        BlockingQueue<String> queue = h.getQueue("my-distributed-queue");
        queue.offer("item");
        String item = queue.poll();
 
        //Timed blocking Operations
        queue.offer("anotheritem", 500, TimeUnit.MILLISECONDS);
        String anotherItem = queue.poll(5, TimeUnit.SECONDS);
 
        //Indefinitely blocking Operations
        queue.put("yetanotheritem");
        String yetanother = queue.take();
    }
}
左手右手慢动作

In your distributed environment, you still want to have only one queue. How much memory do you need?
If you must have only one queue, you can wrap the LinkedBlockingQueue layer to provide an HTTP service to the outside world, and then let other machines in the distribution call this service.

Ty80

LinkedBlockingQueue is not recommended. It is easy to cause problems. Capacity is a problem. If it is too big, there will be a lot of problems. If it is too small, there will be a lot of problems.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template