-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
MockQueue.java
141 lines (127 loc) · 4.82 KB
/
MockQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.ironmq;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.UUID;
import io.iron.ironmq.Client;
import io.iron.ironmq.EmptyQueueException;
import io.iron.ironmq.HTTPException;
import io.iron.ironmq.Ids;
import io.iron.ironmq.Message;
import io.iron.ironmq.MessageOptions;
import io.iron.ironmq.Messages;
import io.iron.ironmq.Queue;
public class MockQueue extends Queue {
private Map<String, Message> messages = new LinkedHashMap<String, Message>();
public MockQueue(Client client, String name) {
super(client, name);
}
@Override
public String push(String msg, long delay) throws IOException {
String randint = new BigInteger(24 * 8, new Random()).toString(16);
Message message = new Message();
message.setBody(msg);
message.setDelay(delay);
message.setId(randint);
message.setReservationId(UUID.randomUUID().toString());
messages.put(randint, message);
return randint;
}
@Override
public Ids pushMessages(String[] msg, long delay) throws IOException {
for (String messageName : msg) {
Message message = new Message();
message.setBody(messageName);
message.setDelay(delay);
String randint = new BigInteger(24 * 8, new Random()).toString(16);
message.setId(randint);
message.setReservationId(UUID.randomUUID().toString());
messages.put(randint, message);
}
Ids ids = null;
try {
Constructor<Ids> constructor = Ids.class.getDeclaredConstructor(Messages.class);
constructor.setAccessible(true);
Messages messageList = new Messages(new ArrayList<Message>(messages.values()));
ids = constructor.newInstance(messageList);
} catch (Exception e) {
}
return ids;
}
@Override
public void deleteMessage(String id, String reservationId) throws IOException {
if (messages.containsKey(id)) {
messages.remove(id);
} else {
throw new HTTPException(404, "not found");
}
}
@Override
public void deleteMessages(Messages messages) throws IOException {
MessageOptions[] messageOptions = messages.toMessageOptions();
for (MessageOptions messageOption : messageOptions) {
deleteMessage(messageOption.getId(), messageOption.getReservationId());
}
}
@Override
public Message peek() throws IOException {
if (messages.size() > 0) {
return messages.entrySet().iterator().next().getValue();
}
throw new EmptyQueueException();
}
@Override
public Message reserve() throws IOException {
if (messages.size() > 0) {
Entry<String, Message> next = messages.entrySet().iterator().next();
return next.getValue();
}
throw new EmptyQueueException();
}
@Override
public Messages reserve(int numberOfMessages) throws IOException {
return reserve(numberOfMessages, 120);
}
@Override
public Messages reserve(int numberOfMessages, int timeout, int wait) throws IOException {
if (messages.size() > 0) {
Iterator<Entry<String, Message>> iterator = messages.entrySet().iterator();
int i = 0;
List<Message> list = new ArrayList<Message>();
while (iterator.hasNext() && i < numberOfMessages) {
Entry<String, Message> next = iterator.next();
list.add(next.getValue());
i++;
}
Messages messages = new Messages(list.toArray(new Message[list.size()]));
return messages;
}
throw new EmptyQueueException();
}
void add(Message message) {
messages.put(message.getId(), message);
}
}