-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.java
144 lines (116 loc) · 4.61 KB
/
Connection.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
142
143
144
/**
* Created by sameerraghuram on 4/22/17.
*/
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
/**
* TODO: Write a relaible function to send JSON messages
* TODO: Write a Server (Done)
* TODO: Figure out how GSON works
*/
public class Connection {
String broker;
ClientListener clientListener;
Callbacks callbacks;
public Connection(String broker){
this.broker = broker;
clientListener = new ClientListener();
callbacks = Callbacks.getInstance();
Thread listener = new Thread(clientListener);
listener.start();
}
/**
* Sends a request to the Broker to subscribe the client
* to a message queue residing on the default exchange.
*
* @param queueName: ID of the message queue on the broker
* @param callback: Function to be called everytime a new message arrives.
*/
public void subscribe(String queueName, Consumer<String> callback){
// Construct the request here
// Step 1: Make the GSON object
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
SubscribeMessage subscribeMessage = new SubscribeMessage(queueName);
String message = gson.toJson(subscribeMessage);
// Step 3: Register callback to queue-id
this.callbacks.put(queueName, callback);
// Step 4: Send message
try {
Messenger messenger = new Messenger(this.broker, 12344);
messenger.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Sends a request to the Broker to subscribe the client
* to a message queue residing on the exchange given by exchangeName.
*
* @param exchangeName: Identity of the exchange
* @param queueName: ID of the message queue on the broker
* @param callback: Function to be called everytime a new message arrives.
*/
public void subscribe(String exchangeName, String queueName, Consumer<String> callback){
// Step 1: Make the GSON object
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
SubscribeMessage subscribeMessage = new SubscribeMessage(exchangeName, queueName);
String message = gson.toJson(subscribeMessage);
// Step 3: Register callback to queue-id
this.callbacks.put(exchangeName + "." + queueName, callback);
// TODO: Step 4: Send message
try {
Messenger messenger = new Messenger(this.broker, 12344);
messenger.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public void createQueue(String exchangeName, String queueName){
// Step 1: Make the GSON object
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
CreateQueueMessage createQueueMessage = new CreateQueueMessage(exchangeName, queueName);
String message = gson.toJson(createQueueMessage);
// Step 3: Send message
try {
Messenger messenger = new Messenger(this.broker, 12344);
messenger.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public void createQueue(String queueName){
// Step 1: Make the GSON object
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
CreateQueueMessage createQueueMessage = new CreateQueueMessage(queueName);
String message = gson.toJson(createQueueMessage);
// Step 3: Send message
try {
Messenger messenger = new Messenger(this.broker, 12344);
messenger.sendMessage(message);
//System.out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public void publish(String queueName, String message){
// Step 1: Make the GSON object
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Step 2: Create the request JSON string
PublishMessage publishMessage = new PublishMessage(queueName, message);
message = gson.toJson(publishMessage);
// TODO: Step 4: Send message
try {
Messenger messenger = new Messenger(this.broker, 12344);
messenger.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}