|
| 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