RuneHive-Game
Loading...
Searching...
No Matches
RequestDispatcher.java
Go to the documentation of this file.
1package dev.advo.fs.dispatch;
2
3import java.util.concurrent.BlockingQueue;
4import java.util.concurrent.PriorityBlockingQueue;
5
6import org.jboss.netty.channel.Channel;
7
8import dev.advo.fs.net.ondemand.OnDemandRequest;
9
10/**
11 * A class which dispatches requests to worker threads.
12 * @author Graham
13 */
14public final class RequestDispatcher {
15
16 /**
17 * The maximum size of a queue before requests are rejected.
18 */
19 private static final int MAXIMUM_QUEUE_SIZE = 1024;
20
21 /**
22 * A queue for pending 'on-demand' requests.
23 */
24 private static final BlockingQueue<ChannelRequest<OnDemandRequest>> onDemandQueue = new PriorityBlockingQueue<ChannelRequest<OnDemandRequest>>();
25
26 /**
27 * Gets the next 'on-demand' request from the queue, blocking if none are
28 * available.
29 * @return The 'on-demand' request.
30 * @throws InterruptedException if the thread is interrupted.
31 */
32 static ChannelRequest<OnDemandRequest> nextOnDemandRequest() throws InterruptedException {
33 return onDemandQueue.take();
34 }
35
36 /**
37 * Dispatches an 'on-demand' request.
38 * @param channel The channel.
39 * @param request The request.
40 */
41 public static void dispatch(Channel channel, OnDemandRequest request) {
42// if (onDemandQueue.size() >= MAXIMUM_QUEUE_SIZE) {
43// channel.close();
44// }
45 onDemandQueue.add(new ChannelRequest<OnDemandRequest>(channel, request));
46 }
47
48 /**
49 * Default private constructor to prevent instantiation.
50 */
52
53 }
54
55}
A specialised request which contains a channel as well as the request object itself.
RequestDispatcher()
Default private constructor to prevent instantiation.
static void dispatch(Channel channel, OnDemandRequest request)
Dispatches an 'on-demand' request.
static ChannelRequest< OnDemandRequest > nextOnDemandRequest()
Gets the next 'on-demand' request from the queue, blocking if none are available.
static final int MAXIMUM_QUEUE_SIZE
The maximum size of a queue before requests are rejected.
static final BlockingQueue< ChannelRequest< OnDemandRequest > > onDemandQueue
A queue for pending 'on-demand' requests.