├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── github
│ └── xetorthio
│ └── jedisque
│ ├── BinaryJedisque.java
│ ├── Command.java
│ ├── Jedisque.java
│ ├── JedisqueBuilder.java
│ ├── Job.java
│ ├── JobInfo.java
│ ├── JobParams.java
│ └── Keyword.java
└── test
└── java
└── com
└── github
└── xetorthio
└── jedisque
├── BinaryJedisqueTest.java
├── ConnectionTest.java
└── JedisqueTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | # Package Files #
7 | *.jar
8 | *.war
9 | *.ear
10 |
11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12 | hs_err_pid*
13 |
14 | .classpath
15 | .project
16 | .settings/
17 | target
18 | nodes.conf
19 |
20 | pom.xml.releaseBackup
21 | release.properties
22 |
23 | .idea
24 | *.iml
25 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Jonathan Leibiusky and Marcos Lilljedahl
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | PATH := ./disque-git/src:${PATH}
2 |
3 | define DISQUE_CONF
4 | daemonize yes
5 | port 7711
6 | pidfile /tmp/disque1.pid
7 | logfile /tmp/disque1.log
8 | appendonly no
9 | endef
10 |
11 | export DISQUE_CONF
12 | start: cleanup
13 | echo "$$DISQUE_CONF" | disque-server -
14 |
15 | cleanup:
16 |
17 | stop:
18 | kill `cat /tmp/disque1.pid`
19 |
20 | test:
21 | make start
22 | sleep 2
23 | mvn -Dtest=${TEST} clean compile test
24 | make stop
25 |
26 | release:
27 | make start
28 | mvn clean release:clean release:prepare
29 | mvn release:perform
30 | make stop
31 |
32 | format:
33 | mvn java-formatter:format
34 |
35 | travis-install:
36 | [ ! -e redis-git ] && git clone https://github.com/antirez/disque.git disque-git || true
37 | make -C redis-git -j4
38 |
39 | .PHONY: test
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Jedisque
2 |
3 | Jedisque is a minimal java client for [Disque](http://github.com/antirez/disque "Disque").
4 |
5 | Jedisque uses [Jedis](http://github.com/xetorthio/jedis "Jedis") as a redis client.
6 |
7 | ## How do I use it?
8 |
9 | To use it just:
10 |
11 | ```xml
12 |
13 | com.github.xetorthio
14 | jedisque
15 | x.y.z
16 | jar
17 | compile
18 |
19 | ```
20 |
21 | Please replace ```x.y.z``` version with one of the available versions.
22 |
23 | ```java
24 | Jedisque q = new Jedisque(
25 | new URI("disque://192.168.0.1:7711"),
26 | new URI("disque://192.168.0.4:8822")
27 | );
28 | String jobId = q.addJob("foo", "bar", 10000);
29 | ```
30 |
31 | ```java
32 | Jedisque q = new Jedisque(
33 | new URI("disque://192.168.0.1:7711"),
34 | new URI("disque://192.168.0.4:8822")
35 | );
36 | List jobs = q.getJob("foo", "foo2");
37 | ```
38 |
39 | For more usage examples check the [tests](https://github.com/xetorthio/jedisque/blob/master/src/test/java/com/github/xetorthio/jedisque/JedisqueTest.java).
40 |
41 | And you are done!
42 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | com.github.xetorthio
4 | jedisque
5 | 0.0.5-SNAPSHOT
6 | jedisque
7 | Java client for disque
8 | https://github.com/xetorthio/jedisque
9 |
10 |
11 | marcosnils
12 | Marcos Lilljedahl
13 | marcosnils@gmail.com
14 | http://github.com/marcosnils
15 | Mantika
16 | http://mantika.ca
17 |
18 |
19 | xetorthio
20 | Jonathan Leibiusky
21 | ionathan@gmail.com
22 | http://github.com/xetorthio
23 | Mantika
24 | http://mantika.ca
25 |
26 |
27 |
28 |
29 | MIT
30 | http://github.com/xetorthio/jedis/raw/master/LICENSE.txt
31 | repo
32 |
33 |
34 |
35 | github
36 | http://github.com/xetorthio/jedisque/issues
37 |
38 |
39 | scm:git:git@github.com:xetorthio/jedisque.git
40 | scm:git:git@github.com:xetorthio/jedisque.git
41 | scm:git:git@github.com:xetorthio/jedisque.git
42 | jedisque-0.0.1
43 |
44 |
45 |
46 | junit
47 | junit
48 | 4.11
49 | jar
50 | test
51 |
52 |
53 | redis.clients
54 | jedis
55 | 2.7.2
56 | jar
57 | compile
58 |
59 |
60 |
61 |
62 |
63 | org.apache.maven.plugins
64 | maven-release-plugin
65 | 2.5
66 |
67 | true
68 | false
69 | release
70 | deploy
71 |
72 |
73 |
74 | org.apache.maven.plugins
75 | maven-compiler-plugin
76 | 3.1
77 |
78 | 1.6
79 | 1.6
80 |
81 |
82 |
83 | org.sonatype.plugins
84 | nexus-staging-maven-plugin
85 | 1.6.3
86 | true
87 |
88 | ossrh
89 | https://oss.sonatype.org/
90 | true
91 |
92 |
93 |
94 | org.apache.maven.plugins
95 | maven-source-plugin
96 | 2.2.1
97 |
98 |
99 | attach-sources
100 |
101 | jar-no-fork
102 |
103 |
104 |
105 |
106 |
107 | org.apache.maven.plugins
108 | maven-javadoc-plugin
109 | 2.9.1
110 |
111 |
112 | attach-javadocs
113 |
114 | jar
115 |
116 |
117 |
118 |
119 |
120 | org.apache.maven.plugins
121 | maven-gpg-plugin
122 | 1.5
123 |
124 |
125 | sign-artifacts
126 | verify
127 |
128 | sign
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 | ossrh
138 | https://oss.sonatype.org/content/repositories/snapshots
139 |
140 |
141 | ossrh
142 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
143 |
144 |
145 |
146 |
--------------------------------------------------------------------------------
/src/main/java/com/github/xetorthio/jedisque/BinaryJedisque.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import redis.clients.jedis.Protocol;
4 | import redis.clients.jedis.exceptions.JedisConnectionException;
5 | import redis.clients.util.SafeEncoder;
6 |
7 | import java.net.URI;
8 | import java.net.URISyntaxException;
9 | import java.util.*;
10 |
11 | public class BinaryJedisque extends redis.clients.jedis.Connection {
12 | static final private int DISQUE_PORT = 7711;
13 | private final List uris = new ArrayList();
14 | private Random randomGenerator = new Random();
15 |
16 | public BinaryJedisque() {
17 | try {
18 | uris.add(new URI("disque://localhost:" + DISQUE_PORT));
19 | } catch (URISyntaxException e) {
20 | }
21 | }
22 |
23 | public BinaryJedisque(URI... uris) {
24 | this.uris.addAll(Arrays.asList(uris));
25 | }
26 |
27 | @Override
28 | public void connect() {
29 | while (!this.isConnected()) {
30 | if (uris.size() == 0) {
31 | throw new JedisConnectionException("Could not connect to any of the provided nodes");
32 | }
33 | int index = randomGenerator.nextInt(uris.size());
34 |
35 | try {
36 | URI uri = uris.get(index);
37 | setHost(uri.getHost());
38 | setPort(uri.getPort());
39 | super.connect();
40 | } catch (JedisConnectionException e) {
41 | uris.remove(index);
42 | }
43 | }
44 | }
45 |
46 | public String addJob(byte[] queueName, byte[] job, long mstimeout) {
47 | sendCommand(Command.ADDJOB, queueName, job, Protocol.toByteArray(mstimeout));
48 | return getBulkReply();
49 | }
50 |
51 | public String addJob(byte[] queueName, byte[] job, long mstimeout, JobParams params) {
52 | ListaddJobCommand = new ArrayList();
53 | addJobCommand.add(queueName);
54 | addJobCommand.add(job);
55 | addJobCommand.add(Protocol.toByteArray(mstimeout));
56 | addJobCommand.addAll(params.getParams());
57 | sendCommand(Command.ADDJOB, addJobCommand.toArray(new byte[addJobCommand.size()][]));
58 | return getBulkReply();
59 | }
60 |
61 |
62 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/xetorthio/jedisque/Command.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import redis.clients.jedis.ProtocolCommand;
4 | import redis.clients.util.SafeEncoder;
5 |
6 | public enum Command implements ProtocolCommand {
7 | ADDJOB, GETJOB, ACKJOB, INFO, QLEN, QPEEK, DELJOB, DEQUEUE, ENQUEUE, FASTACK, SHOW, PING, WORKING;
8 | private final byte[] raw;
9 |
10 | Command() {
11 | raw = SafeEncoder.encode(this.name());
12 | }
13 |
14 | @Override
15 | public byte[] getRaw() {
16 | return raw;
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/github/xetorthio/jedisque/Jedisque.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import redis.clients.jedis.Protocol;
4 | import redis.clients.jedis.exceptions.JedisConnectionException;
5 | import redis.clients.util.SafeEncoder;
6 |
7 | import java.net.URI;
8 | import java.net.URISyntaxException;
9 | import java.util.ArrayList;
10 | import java.util.Arrays;
11 | import java.util.List;
12 | import java.util.Random;
13 |
14 | public class Jedisque extends BinaryJedisque {
15 | private Random randomGenerator = new Random();
16 |
17 | public Jedisque() {
18 | super();
19 | }
20 |
21 | public Jedisque(URI... uris) {
22 | super(uris);
23 | }
24 |
25 | public String addJob(String queueName, String job, long mstimeout) {
26 | return addJob(SafeEncoder.encode(queueName), SafeEncoder.encode(job), mstimeout);
27 | }
28 |
29 | public String addJob(String queueName, String job, long mstimeout, JobParams params) {
30 | return addJob(SafeEncoder.encode(queueName), SafeEncoder.encode(job), mstimeout, params);
31 | }
32 |
33 | public List getJob(String... queueNames) {
34 | final byte[][] params = new byte[queueNames.length + 1][];
35 | params[0] = Keyword.FROM.raw;
36 | System.arraycopy(SafeEncoder.encodeMany(queueNames), 0, params, 1, queueNames.length);
37 | sendCommand(Command.GETJOB, params);
38 | return JedisqueBuilder.JOB_LIST.build(getObjectMultiBulkReply());
39 | }
40 |
41 |
42 | public List getJob(long timeout, long count, String ...queueNames) {
43 | final byte[][] params = new byte[queueNames.length + 5][];
44 | params[0] = Keyword.TIMEOUT.raw;
45 | params[1] = Protocol.toByteArray(timeout);
46 | params[2] = Keyword.COUNT.raw;
47 | params[3] = Protocol.toByteArray(count);
48 | params[4] = Keyword.FROM.raw;
49 | System.arraycopy(SafeEncoder.encodeMany(queueNames), 0, params, 5, queueNames.length);
50 | sendCommand(Command.GETJOB, params);
51 | return JedisqueBuilder.JOB_LIST.build(getObjectMultiBulkReply());
52 | }
53 |
54 | public Long ackjob(String... jobIds) {
55 | sendCommand(Command.ACKJOB, jobIds);
56 | return getIntegerReply();
57 | }
58 |
59 | public String info() {
60 | sendCommand(Command.INFO);
61 | return getBulkReply();
62 | }
63 |
64 | public String info(String section) {
65 | sendCommand(Command.INFO, section);
66 | return getBulkReply();
67 | }
68 |
69 | public Long qlen(String queueName) {
70 | sendCommand(Command.QLEN, queueName);
71 | return getIntegerReply();
72 |
73 | }
74 |
75 | public List qpeek(String queueName, long count) {
76 | sendCommand(Command.QPEEK, SafeEncoder.encode(queueName), Protocol.toByteArray(count));
77 | return JedisqueBuilder.JOB_LIST.build(getObjectMultiBulkReply());
78 | }
79 |
80 | public Long delJob(String jobId) {
81 | sendCommand(Command.DELJOB, jobId);
82 | return getIntegerReply();
83 | }
84 |
85 | public Long dequeue(String... jobIds) {
86 | sendCommand(Command.DEQUEUE, jobIds);
87 | return getIntegerReply();
88 | }
89 |
90 | public Long enqueue(String... jobIds) {
91 | sendCommand(Command.ENQUEUE, jobIds);
92 | return getIntegerReply();
93 | }
94 |
95 | public Long fastack(String ...jobIds) {
96 | sendCommand(Command.FASTACK, jobIds);
97 | return getIntegerReply();
98 | }
99 |
100 |
101 | public JobInfo show(String jobId) {
102 | sendCommand(Command.SHOW, jobId);
103 | return JedisqueBuilder.JOB_SHOW.build(getObjectMultiBulkReply());
104 | }
105 |
106 |
107 | public String ping() {
108 | sendCommand(Command.PING);
109 | return getBulkReply();
110 | }
111 |
112 | public Long working(String jobId) {
113 | sendCommand(Command.WORKING, jobId);
114 | return getIntegerReply();
115 | }
116 |
117 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/xetorthio/jedisque/JedisqueBuilder.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import redis.clients.jedis.Builder;
4 | import redis.clients.jedis.BuilderFactory;
5 | import redis.clients.util.SafeEncoder;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | public class JedisqueBuilder extends BuilderFactory {
11 |
12 | public static final Builder> JOB_LIST = new Builder>() {
13 | @SuppressWarnings("unchecked")
14 | public List build(Object data) {
15 | if (null == data) {
16 | return null;
17 | }
18 | List> l = (List>) data;
19 |
20 | // Order matters that's why we're using a LL
21 | final List result = new ArrayList();
22 |
23 | for (List rawJob : l) {
24 | result.add(new Job(rawJob.get(0), rawJob.get(1),
25 | rawJob.get(2)));
26 | }
27 |
28 | return result;
29 | }
30 |
31 | };
32 |
33 | public static final Builder JOB_SHOW = new Builder() {
34 | @SuppressWarnings("unchecked")
35 | public JobInfo build(Object data) {
36 | if (null == data) {
37 | return null;
38 | }
39 | List showRaw = (List) data;
40 |
41 | return new JobInfo(SafeEncoder.encode(showRaw.get(1)),
42 | SafeEncoder.encode(showRaw.get(3)),
43 | SafeEncoder.encode(showRaw.get(5)),
44 | BuilderFactory.LONG.build(showRaw.get(7)),
45 | BuilderFactory.LONG.build(showRaw.get(9)),
46 | BuilderFactory.LONG.build(showRaw.get(11)),
47 | BuilderFactory.LONG.build(showRaw.get(13)),
48 | BuilderFactory.LONG.build(showRaw.get(15)),
49 | BuilderFactory.STRING_LIST.build(showRaw.get(17)),
50 | BuilderFactory.STRING_LIST.build(showRaw.get(19)),
51 | BuilderFactory.LONG.build(showRaw.get(21)),
52 | BuilderFactory.LONG.build(showRaw.get(23)),
53 | SafeEncoder.encode(showRaw.get(25)));
54 | }
55 |
56 | };
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/com/github/xetorthio/jedisque/Job.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import redis.clients.util.SafeEncoder;
4 |
5 | import java.util.Arrays;
6 |
7 | public class Job {
8 |
9 | private byte[] id;
10 | private byte[] queueName;
11 | private byte[] body;
12 |
13 | public Job() {
14 | }
15 |
16 | public Job(byte[] queueName, byte[] id, byte[] body) {
17 | super();
18 | this.id = id;
19 | this.queueName = queueName;
20 | this.body = body;
21 | }
22 |
23 | public String getId() {
24 | return SafeEncoder.encode(id);
25 | }
26 |
27 | public byte[] getIdAsBytes() {
28 | return id;
29 | }
30 |
31 | public void setId(byte[] id) {
32 | this.id = id;
33 | }
34 |
35 | public String getQueueName() {
36 | return SafeEncoder.encode(queueName);
37 | }
38 |
39 | public byte[] getQueueNameAsBytes() {
40 | return this.queueName;
41 | }
42 |
43 | public void setQueueName(byte[] queueName) {
44 | this.queueName = queueName;
45 | }
46 |
47 | public String getBody() {
48 | return SafeEncoder.encode(body);
49 | }
50 |
51 | public byte[] getBodyAsBytes() {
52 | return body;
53 | }
54 |
55 | public String getStringBody() {
56 | return SafeEncoder.encode(body);
57 | }
58 |
59 | public void setBody(byte[] body) {
60 | this.body = body;
61 | }
62 |
63 | @Override
64 | public int hashCode() {
65 | final int prime = 31;
66 | int result = 1;
67 | result = prime * result + ((id == null) ? 0 : id.hashCode());
68 | result = prime * result + ((body == null) ? 0 : body.hashCode());
69 | result = prime * result
70 | + ((queueName == null) ? 0 : queueName.hashCode());
71 | return result;
72 | }
73 |
74 | @Override
75 | public boolean equals(Object obj) {
76 |
77 | if (this == obj)
78 | return true;
79 | if (obj == null)
80 | return false;
81 | if (getClass() != obj.getClass())
82 | return false;
83 | Job other = (Job) obj;
84 | if (id == null) {
85 | if (other.id != null)
86 | return false;
87 | } else if (!Arrays.equals(id, other.id))
88 | return false;
89 | if (body == null) {
90 | if (other.body != null)
91 | return false;
92 | } else if (!Arrays.equals(body, other.body))
93 | return false;
94 | if (queueName == null) {
95 | if (other.queueName != null)
96 | return false;
97 | } else if (!Arrays.equals(queueName, other.queueName))
98 | return false;
99 | return true;
100 | }
101 |
102 | }
103 |
--------------------------------------------------------------------------------
/src/main/java/com/github/xetorthio/jedisque/JobInfo.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import java.util.List;
4 |
5 | public class JobInfo {
6 |
7 | private String id;
8 | private String queue;
9 | private String state;
10 | private long repl;
11 | private long ttl;
12 | private long ctime;
13 | private long delay;
14 | private long retry;
15 | private List nodesDelivered;
16 | private List nodesConfirmed;
17 | private long requeueWithin;
18 | private long awakeWithin;
19 | private String body;
20 |
21 | public JobInfo(String id, String queue, String state, long repl, long ttl, long ctime, long delay, long retry,
22 | List nodesDelivered, List nodesConfirmed, long requeueWithin, long awakeWithin, String body) {
23 | super();
24 | this.id = id;
25 | this.queue = queue;
26 | this.state = state;
27 | this.repl = repl;
28 | this.ttl = ttl;
29 | this.ctime = ctime;
30 | this.delay = delay;
31 | this.retry = retry;
32 | this.nodesDelivered = nodesDelivered;
33 | this.nodesConfirmed = nodesConfirmed;
34 | this.requeueWithin = requeueWithin;
35 | this.awakeWithin = awakeWithin;
36 | this.body = body;
37 | }
38 |
39 | public String getId() {
40 | return id;
41 | }
42 |
43 | public String getQueue() {
44 | return queue;
45 | }
46 |
47 | public String getState() {
48 | return state;
49 | }
50 |
51 | public long getRepl() {
52 | return repl;
53 | }
54 |
55 | public long getTtl() {
56 | return ttl;
57 | }
58 |
59 | public long getCtime() {
60 | return ctime;
61 | }
62 |
63 | public long getDelay() {
64 | return delay;
65 | }
66 |
67 | public long getRetry() {
68 | return retry;
69 | }
70 |
71 | public List getNodesDelivered() {
72 | return nodesDelivered;
73 | }
74 |
75 | public List getNodesConfirmed() {
76 | return nodesConfirmed;
77 | }
78 |
79 | public long getRequeueWithin() {
80 | return requeueWithin;
81 | }
82 |
83 | public long getAwakeWithin() {
84 | return awakeWithin;
85 | }
86 |
87 | public String getBody() {
88 | return body;
89 | }
90 |
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/src/main/java/com/github/xetorthio/jedisque/JobParams.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import redis.clients.jedis.Protocol;
4 |
5 | import java.util.ArrayList;
6 | import java.util.Collection;
7 | import java.util.Collections;
8 | import java.util.List;
9 |
10 | public class JobParams {
11 |
12 | private List params = new ArrayList();
13 |
14 | public JobParams setReplicate(Integer replicate) {
15 | params.add(Keyword.REPLICATE.raw);
16 | params.add(Protocol.toByteArray(replicate));
17 | return this;
18 | }
19 |
20 | public JobParams setDelay(Integer delay) {
21 | params.add(Keyword.DELAY.raw);
22 | params.add(Protocol.toByteArray(delay));
23 | return this;
24 | }
25 |
26 | public JobParams setRetry(Integer retry) {
27 | params.add(Keyword.RETRY.raw);
28 | params.add(Protocol.toByteArray(retry));
29 | return this;
30 | }
31 |
32 | public JobParams setTTL(Integer ttl) {
33 | params.add(Keyword.TTL.raw);
34 | params.add(Protocol.toByteArray(ttl));
35 | return this;
36 | }
37 |
38 | public JobParams setMaxlen(Integer maxlen) {
39 | params.add(Keyword.MAXLEN.raw);
40 | params.add(Protocol.toByteArray(maxlen));
41 | return this;
42 | }
43 |
44 | public JobParams setAsync(Boolean async) {
45 | params.add(Keyword.ASYNC.raw);
46 | return this;
47 | }
48 |
49 | public Collection getParams() {
50 | return Collections.unmodifiableCollection(params);
51 | }
52 |
53 | }
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/src/main/java/com/github/xetorthio/jedisque/Keyword.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import redis.clients.util.SafeEncoder;
4 |
5 |
6 | enum Keyword {
7 | FROM, REPLICATE, DELAY, RETRY, TTL, MAXLEN, ASYNC, TIMEOUT, COUNT;
8 | final byte[] raw;
9 |
10 | Keyword() {
11 | raw = SafeEncoder.encode(this.name());
12 | }
13 |
14 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/xetorthio/jedisque/BinaryJedisqueTest.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import org.junit.*;
4 | import redis.clients.util.SafeEncoder;
5 |
6 | import java.util.Arrays;
7 | import java.util.List;
8 | import java.util.UUID;
9 |
10 | import static org.junit.Assert.assertEquals;
11 | import static org.junit.Assert.assertNotNull;
12 | import static org.junit.Assert.assertTrue;
13 |
14 | public class BinaryJedisqueTest {
15 | private static byte[] getQueueName() {
16 | return UUID.randomUUID().toString().getBytes();
17 | }
18 | byte[] binaryValue;
19 | static Jedisque q;
20 |
21 | @Before
22 | public void setUp() throws Exception {
23 | q = new Jedisque();
24 | StringBuilder sb = new StringBuilder();
25 |
26 | for (int n = 0; n < 1000; n++) {
27 | sb.append("A");
28 | }
29 |
30 | binaryValue = sb.toString().getBytes();
31 | }
32 |
33 | @After
34 | public void tearDown() throws Exception {
35 | q.close();
36 | }
37 |
38 | @Test
39 | public void addJob() {
40 | String jobId = q.addJob(getQueueName(), binaryValue, 10);
41 | assertNotNull(jobId);
42 | }
43 |
44 | @Test
45 | public void addJobWithParams() {
46 | JobParams params = new JobParams().setReplicate(1).setRetry(10).setTTL(10).setMaxlen(10).setDelay(5)
47 | .setAsync(true);
48 | String jobId = q.addJob(getQueueName(), binaryValue, 10, params);
49 | assertNotNull(jobId);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/test/java/com/github/xetorthio/jedisque/ConnectionTest.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import java.net.URI;
4 | import java.net.URISyntaxException;
5 |
6 | import org.junit.Test;
7 |
8 | import redis.clients.jedis.exceptions.JedisConnectionException;
9 |
10 | public class ConnectionTest {
11 |
12 | @Test
13 | public void iterateOverHosts() throws URISyntaxException {
14 | Jedisque q = new Jedisque(new URI("disque://localhost:55665"), new URI("disque://localhost:7711"));
15 | q.info();
16 | q.close();
17 | }
18 |
19 | @Test(expected = JedisConnectionException.class)
20 | public void throwExceptionWhenNodesAreUnavailbale() throws URISyntaxException {
21 | Jedisque q = new Jedisque(new URI("disque://localhost:55665"), new URI("disque://localhost:55666"));
22 | q.info();
23 | q.close();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/test/java/com/github/xetorthio/jedisque/JedisqueTest.java:
--------------------------------------------------------------------------------
1 | package com.github.xetorthio.jedisque;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.junit.Assert.assertNotNull;
5 |
6 | import java.util.List;
7 | import java.util.UUID;
8 |
9 | import org.junit.After;
10 | import org.junit.Assert;
11 | import org.junit.Before;
12 | import org.junit.Ignore;
13 | import org.junit.Test;
14 |
15 | public class JedisqueTest {
16 | private static String getQueueName() {
17 | return UUID.randomUUID().toString();
18 | }
19 |
20 | static Jedisque q;
21 |
22 | @Before
23 | public void setUp() throws Exception {
24 | q = new Jedisque();
25 | }
26 |
27 | @After
28 | public void tearDown() throws Exception {
29 | q.close();
30 | }
31 |
32 | @Test
33 | public void addJob() {
34 | String jobId = q.addJob(getQueueName(), "message", 10);
35 | assertNotNull(jobId);
36 | }
37 |
38 | @Test
39 | public void addJobWithParams() {
40 | JobParams params = new JobParams().setReplicate(1).setRetry(10).setTTL(10).setMaxlen(10).setDelay(5)
41 | .setAsync(true);
42 | String jobId = q.addJob(getQueueName(), "message", 10, params);
43 | assertNotNull(jobId);
44 | }
45 |
46 | @Test
47 | public void getJob() {
48 | String queue = getQueueName();
49 | String jobId = q.addJob(queue, "message", 10);
50 | List jobs = q.getJob(queue);
51 | Job job = jobs.get(0);
52 | assertEquals(jobId, job.getId());
53 | assertEquals("message", job.getStringBody());
54 | assertEquals(queue, job.getQueueName());
55 | }
56 |
57 | @Test
58 | public void getJobWithParams() {
59 | String queue = getQueueName();
60 | q.addJob(queue, "message", 10);
61 | q.addJob(queue, "message", 10);
62 | List jobs = q.getJob(100, 2, queue);
63 | assertEquals(jobs.size(), 2);
64 | }
65 |
66 | @Test
67 | public void ackJob() {
68 | String jobId = q.addJob(getQueueName(), "message", 10);
69 | Long count = q.ackjob(jobId);
70 | assertEquals(count.longValue(), 1);
71 | }
72 |
73 | @Test
74 | public void fastAck() {
75 | String jobId = q.addJob("fastack", "message", 10);
76 | Long count = q.fastack(jobId);
77 | assertEquals(count.longValue(), 1);
78 | }
79 |
80 | @Test
81 | public void info() {
82 | String info = q.info();
83 | assertNotNull(info);
84 | info = q.info("server");
85 | assertNotNull(info);
86 | }
87 |
88 | @Test
89 | @Ignore(value = "pending")
90 | public void hello() {
91 |
92 | }
93 |
94 | @Test
95 | public void qlen() {
96 | String queue = getQueueName();
97 | Long qlen = q.qlen(queue);
98 | assertEquals(qlen.longValue(), 0);
99 | }
100 |
101 | @Test
102 | @Ignore(value = "pending (not yet implemented)")
103 | public void qstat() {
104 | }
105 |
106 | @Test
107 | public void qpeek() {
108 | // We're testing also the response parsing here
109 | String queue = getQueueName();
110 | q.addJob(queue, "testJob", 10);
111 | q.addJob(queue, "testJob2", 10);
112 | List jobs = q.qpeek(queue, 2);
113 | Job job = jobs.get(0);
114 | assertEquals(job.getStringBody(), "testJob");
115 | job = jobs.get(1);
116 | assertEquals(job.getStringBody(), "testJob2");
117 | }
118 |
119 | @Test
120 | public void qpeekEmpty() {
121 | List jobs = q.qpeek(getQueueName(), 2);
122 | assertEquals(jobs.size(), 0);
123 | }
124 |
125 | @Test
126 | public void qpeekInverse() {
127 | String queue = getQueueName();
128 | List jobs = q.qpeek(queue, -2);
129 | assertEquals(jobs.size(), 0);
130 | }
131 |
132 | @Test
133 | public void enqueue() {
134 | String queue = getQueueName();
135 | String jobId = q.addJob(queue, "testJob", 10);
136 | Long count = q.enqueue(jobId);
137 | assertEquals(count.longValue(), 0);
138 | }
139 |
140 | @Test
141 | public void dequeue() throws InterruptedException {
142 | String queue = getQueueName();
143 | String jobId = q.addJob(queue, "testJob", 10);
144 | Long count = q.dequeue(jobId);
145 | assertEquals(count.longValue(), 1);
146 | }
147 |
148 | @Test
149 | public void delJob() {
150 | String queue = getQueueName();
151 | String jobId = q.addJob(queue, "testJob", 10);
152 | Long count = q.delJob(jobId);
153 | assertEquals(count.longValue(), 1);
154 | }
155 |
156 | @Test
157 | public void show() {
158 | String queue = getQueueName();
159 | String jobId = q.addJob(queue, "testJob", 10);
160 | JobInfo jobInfo = q.show(jobId);
161 | assertNotNull(jobInfo);
162 | }
163 |
164 | @Test
165 | public void ping() {
166 | String pong = q.ping();
167 | assertNotNull(pong);
168 | }
169 |
170 | @Test
171 | public void working() {
172 | String queue = getQueueName();
173 | String jobId = q.addJob(queue, "testJob", 10);
174 | Long secs = q.working(jobId);
175 | assertNotNull(secs);
176 | Assert.assertNotEquals(0L, secs.longValue());
177 | }
178 |
179 | @Test
180 | @Ignore(value = "pending")
181 | public void scan() {
182 | }
183 | }
184 |
--------------------------------------------------------------------------------