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