search

Home  >  Q&A  >  body text

队列 - 请问Java里的LinkedBlockingQueue如何在分布式下使用

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

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

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

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

希望前辈们能指导迷津

黄舟黄舟2769 days ago907

reply all(4)I'll reply

  • PHPz

    PHPz2017-04-18 10:28:23

    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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:28:23

    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();
        }
    }

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:28:23

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:28:23

    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.

    reply
    0
  • Cancelreply