Skip to content

Commit 4a17321

Browse files
committed
Initial
0 parents  commit 4a17321

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RabbitMQjava.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="com.rabbitmq:amqp-client:5.0.0" level="project" />
11+
</component>
12+
</module>

src/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.isc.rabbitmq.Wrapper
3+

src/isc/rabbitmq/API.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package isc.rabbitmq;
2+
3+
import com.rabbitmq.client.*;
4+
5+
import java.io.IOException;
6+
7+
8+
/**
9+
* Created by eduard on 06.10.2017.
10+
*/
11+
public class API {
12+
private final Channel _channel;
13+
14+
private final String _queue;
15+
16+
private final Connection _connection;
17+
18+
public API(String host, int port, String user, String pass, String virtualHost, String queue) throws Exception {
19+
ConnectionFactory factory = new ConnectionFactory();
20+
factory.setHost(host);
21+
factory.setPort(port);
22+
factory.setUsername(user);
23+
factory.setPassword(pass);
24+
factory.setVirtualHost(virtualHost);
25+
26+
27+
_connection = factory.newConnection();
28+
_channel = _connection.createChannel();
29+
_channel.queueDeclare(queue, false, false, false, null);
30+
_queue = queue;
31+
}
32+
33+
public String[] readMessageStream(byte[] msg) throws Exception {
34+
String[] result = new String[15];
35+
36+
GetResponse response = readMessage(result);
37+
if (response == null) {
38+
// No message retrieved.
39+
} else {
40+
System.arraycopy(response.getBody(),0,msg,0, response.getBody().length);
41+
}
42+
return result;
43+
}
44+
45+
public void sendMessage(byte[] msg) throws Exception {
46+
sendMessageToQueue(_queue, msg);
47+
}
48+
49+
public void sendMessageToQueue(String queue, byte[] msg) throws Exception {
50+
_channel.basicPublish("", queue, null, msg);
51+
}
52+
53+
public String[] readMessageString() throws Exception {
54+
String[] result = new String[16];
55+
56+
GetResponse response = readMessage(result);
57+
if (response == null) {
58+
// No message retrieved.
59+
} else {
60+
result[15] = new String(response.getBody());
61+
}
62+
return result;
63+
}
64+
65+
/*
66+
Get message and fill basic array props
67+
*/
68+
private GetResponse readMessage(String[] msg) throws IOException {
69+
boolean autoAck = true;
70+
GetResponse response = _channel.basicGet(_queue, autoAck);
71+
if (response == null) {
72+
// No message retrieved.
73+
} else {
74+
AMQP.BasicProperties props = response.getProps();
75+
msg[0] = Integer.toString(response.getBody().length);
76+
msg[1] = Integer.toString(response.getMessageCount());
77+
msg[2] = props.getContentType();
78+
msg[3] = props.getContentEncoding();
79+
msg[4] = props.getCorrelationId();
80+
msg[5] = props.getReplyTo();
81+
msg[6] = props.getExpiration();
82+
msg[7] = props.getMessageId();
83+
msg[8] = props.getType();
84+
msg[9] = props.getUserId();
85+
msg[10] = props.getAppId();
86+
msg[11] = props.getClusterId();
87+
msg[12] = props.getDeliveryMode() != null ? Integer.toString(props.getDeliveryMode()) : null;
88+
msg[13] = props.getPriority() != null ? Integer.toString(props.getPriority()) : null;
89+
msg[14] = props.getTimestamp() != null ? props.getTimestamp().toString() : null;
90+
}
91+
return response;
92+
93+
}
94+
95+
public void close()throws Exception {
96+
_channel.close();
97+
_connection.close();
98+
}
99+
100+
}

0 commit comments

Comments
 (0)