|
1 | 1 | package com.github.brainlag.nsq; |
2 | 2 |
|
| 3 | +import java.nio.charset.Charset; |
3 | 4 | import java.util.ArrayList; |
4 | 5 | import java.util.List; |
5 | 6 |
|
6 | 7 | public class NSQCommand { |
| 8 | + String line; |
| 9 | + List<byte[]> data = new ArrayList<>(); |
7 | 10 |
|
8 | | - public static NSQCommand instance(String line) { |
9 | | - NSQCommand n = new NSQCommand(); |
10 | | - n.setLine(line); |
11 | | - return n; |
12 | | - } |
13 | | - |
14 | | - public static NSQCommand instance(String line, byte[] bytes) { |
15 | | - NSQCommand n = instance(line); |
16 | | - n.addBytes(bytes); |
17 | | - return n; |
18 | | - } |
19 | | - |
20 | | - String line; |
21 | | - List<byte[]> data = new ArrayList<>(); |
22 | | - |
23 | | - public void addBytes(byte[] bytes) { |
24 | | - data.add(bytes); |
25 | | - } |
26 | | - |
27 | | - public String getLine() { |
28 | | - return line; |
29 | | - } |
30 | | - public void setLine(String line) { |
31 | | - if (!line.endsWith("\n")) { |
32 | | - line = line +"\n"; |
33 | | - } |
34 | | - |
35 | | - this.line = line; |
36 | | - } |
37 | | - public List<byte[]> getData() { |
38 | | - return data; |
39 | | - } |
40 | | - public void setData(List<byte[]> data) { |
41 | | - this.data = data; |
42 | | - } |
43 | | - |
44 | | - public String toString() { |
| 11 | + private NSQCommand() { /** no instances */ } |
| 12 | + |
| 13 | + public void addBytes(byte[] bytes) { |
| 14 | + data.add(bytes); |
| 15 | + } |
| 16 | + |
| 17 | + public String getLine() { |
| 18 | + return line; |
| 19 | + } |
| 20 | + |
| 21 | + public void setLine(String line) { |
| 22 | + if (!line.endsWith("\n")) { |
| 23 | + line = line + "\n"; |
| 24 | + } |
| 25 | + |
| 26 | + this.line = line; |
| 27 | + } |
| 28 | + |
| 29 | + public List<byte[]> getData() { |
| 30 | + return data; |
| 31 | + } |
| 32 | + |
| 33 | + public void setData(List<byte[]> data) { |
| 34 | + this.data = data; |
| 35 | + } |
| 36 | + |
| 37 | + public String toString() { |
45 | 38 | return this.getLine().trim(); |
46 | 39 | } |
| 40 | + |
| 41 | + // ASCII stores a reference to the charset needed for commands |
| 42 | + public static final Charset ASCII = Charset.forName("ascii"); |
| 43 | + |
| 44 | + // Identify creates a new Command to provide information about the client. After connecting, |
| 45 | + // it is generally the first message sent. |
| 46 | + // |
| 47 | + // The supplied body should be a map marshaled into JSON to provide some flexibility |
| 48 | + // for this command to evolve over time. |
| 49 | + // |
| 50 | + // See http://nsq.io/clients/tcp_protocol_spec.html#identify for information |
| 51 | + // on the supported options |
| 52 | + public static NSQCommand identify(byte[] body) { |
| 53 | + return NSQCommand.instance("IDENTIFY", body); |
| 54 | + } |
| 55 | + |
| 56 | + // Touch creates a new Command to reset the timeout for |
| 57 | + // a given message (by id) |
| 58 | + public static NSQCommand touch(byte[] messageID) { |
| 59 | + return NSQCommand.instance("TOUCH " + new String(messageID, ASCII)); |
| 60 | + } |
| 61 | + |
| 62 | + // Finish creates a new Command to indiciate that |
| 63 | + // a given message (by id) has been processed successfully |
| 64 | + public static NSQCommand finish(byte[] messageID) { |
| 65 | + return NSQCommand.instance("FIN " + new String(messageID, ASCII)); |
| 66 | + } |
| 67 | + |
| 68 | + // Subscribe creates a new Command to subscribe to the given topic/channel |
| 69 | + public static NSQCommand subscribe(String topic, String channel) { |
| 70 | + return NSQCommand.instance("SUB " + topic + " " + channel); |
| 71 | + } |
| 72 | + |
| 73 | + // StartClose creates a new Command to indicate that the |
| 74 | + // client would like to start a close cycle. nsqd will no longer |
| 75 | + // send messages to a client in this state and the client is expected |
| 76 | + // finish pending messages and close the connection |
| 77 | + public static NSQCommand startClose() { |
| 78 | + return NSQCommand.instance("CLS"); |
| 79 | + } |
| 80 | + |
| 81 | + public static NSQCommand requeue(byte[] messageID, int timeoutMillis) { |
| 82 | + return NSQCommand.instance("REQ " + new String(messageID, ASCII) + " " + timeoutMillis); |
| 83 | + } |
| 84 | + |
| 85 | + // Nop creates a new Command that has no effect server side. |
| 86 | + // Commonly used to respond to heartbeats |
| 87 | + public static NSQCommand nop() { |
| 88 | + return NSQCommand.instance("NOP"); |
| 89 | + } |
| 90 | + |
| 91 | + // Ready creates a new Command to specify |
| 92 | + // the number of messages a client is willing to receive |
| 93 | + public static NSQCommand ready(int rdy) { |
| 94 | + return NSQCommand.instance("RDY " + rdy); |
| 95 | + } |
| 96 | + |
| 97 | + // Publish creates a new Command to write a message to a given topic |
| 98 | + public static NSQCommand publish(String topic, byte[] message) { |
| 99 | + return NSQCommand.instance("PUB " + topic, message); |
| 100 | + } |
| 101 | + |
| 102 | + // MultiPublish creates a new Command to write more than one message to a given topic |
| 103 | + // (useful for high-throughput situations to avoid roundtrips and saturate the pipe) |
| 104 | + // Note: can only be used with more than 1 bodies! |
| 105 | + public static NSQCommand multiPublish(String topic, List<byte[]> bodies) { |
| 106 | + NSQCommand cmd = NSQCommand.instance("MPUB " + topic); |
| 107 | + cmd.setData(bodies); |
| 108 | + return cmd; |
| 109 | + } |
| 110 | + |
| 111 | + private static NSQCommand instance(String line) { |
| 112 | + NSQCommand n = new NSQCommand(); |
| 113 | + n.setLine(line); |
| 114 | + return n; |
| 115 | + } |
| 116 | + |
| 117 | + private static NSQCommand instance(String line, byte[] bytes) { |
| 118 | + NSQCommand n = instance(line); |
| 119 | + n.addBytes(bytes); |
| 120 | + return n; |
| 121 | + } |
| 122 | + |
47 | 123 | } |
0 commit comments