DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > How about Distributed Queues?

How about Distributed Queues?

Dmitriy Setrakyan user avatar by
Dmitriy Setrakyan
·
Aug. 31, 11 · Java Zone · Interview
Like (1)
Save
Tweet
10.17K Views

Join the DZone community and get the full member experience.

Join For Free

Did you ever wish you could take a data structure you are familiar with and distribute it over grid? For example, why not take java.util.concurrent.BlockingDeque and add something to it on one node and poll it from another node? Or why not have a distributed primary key generator which would guarantee uniqueness on all nodes? Or how about a distributed java.util.concurrent.atomic.AtomicLong which can be updated and read from any node on the grid? GridGain gives you such capability. What GridGain did is actually take most of the data structures from java.util.concurrent framework and made sure they could be used in distributed fashion.

In this blog I want to show how flexible GridGain distributed queues are. On top implementing java.util.Collection interface and supporting different modes of operation, like collocated vs. non-collocated, or bounded vs. ubounded modes, you can actually control how elements are ordered within queues. GridGain supports FIFO, LIFO, and Priority based queues out of the box.

FIFO queues (first-in-first-out) are the most traditional queues where elements are added from the tail and polled form the queue head. LIFO queues (last-in-first-out) resemble more of stack features instead of queues. In LIFO queues elements are added and polled from the tail.

But the most interesting queue type is Priority queue where user can control the order of the elements. Priority queue order elements within the queue based on priority attribute specified by the user. Priority attribute of a queue element is annotated via @GridCacheQueuePriority annotation. Here is an example of how priority queue can be created and used. 

public void priorityQueueExample() {
Random rand = new Random();

Grid grid = G.grid();

// Initialize new unbounded collocated priority queue.
GridCacheQueue<PriorityItem> queue =
grid.cache().queue("myqueue", PRIORITY);

// Store 20 elements in queue with random priority.
for (int i = 0; i < 20; i++) {
int priority = rand.nextInt(20);

queue.put(new PriorityItem(priority, "somedata-" + i));
}

PriorityItem item = null;

int lastPriority = 0;

do {
item = queue.poll();

// Ensure the elements are correctly ordered based on priority.
assert lastPriority <= item.priority();

lastPriority = item.priority();
}
while (item != null);
}

...

// Class defining sample queue element with its priority specified via
// @GridCacheQueuePriority annotation attached to priority field.
private static class PriorityItem implements Serializable {
// Priority of queue item.
@GridCacheQueuePriority
private final int priority;

private final String data;

private SampleItem(int priority, String data) {
this.priority = priority;
this.data = data;
}

public int priority() {
return priority;
}
}

Read more about GridGain queues here

From http://gridgain.blogspot.com/2011/08/how-about-distributed-queues.html

Element Relational database Data structure Data (computing) Attribute (computing) Framework Form (document) Interface (computing) Blog

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Transactions vs. Analytics in Apache Kafka
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance
  • How To Integrate Event Streaming Into Your Applications
  • Top Soft Skills to Identify a Great Software Engineer

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo