@Override public void start() throws Exception { ProtonClient client = ProtonClient.create(vertx); client.connect("localhost", 5672, res -> { if(!res.succeeded()) { System.out.println("Connect failed: " + res.cause()); return; } ProtonConnection connection = res.result(); connection.open(); connection.createReceiver(address).handler((delivery, msg) -> { String content = (String) ((AmqpValue) msg.getBody()).getValue(); System.out.println("Received message with content: " + content); // By default, receivers automatically accept (and settle) the delivery // when the handler returns, if no other disposition has been applied. // To change this and always manage dispositions yourself, use the // setAutoAccept method on the receiver. }).open(); }); } }
String content = (String) ((AmqpValue) body).getValue(); System.out.println("Received message for address: " + address + ", body: " + content);
@Override protected Object wrap(AmqpValue val) { return val.getValue(); }
@Override protected Object wrap(AmqpValue val) { return val.getValue(); }
@Override protected Object wrap(AmqpValue val) { return val.getValue(); }
@Override protected Object wrap(AmqpValue val) { return val.getValue(); }
public static Map getResponseBody(Message responseMessage) { return (Map)((AmqpValue)responseMessage.getBody()).getValue(); }
public synchronized List<String> recvMessages(String address, int numMessages) { Queue<Message> queue = queues.get(address); if (queue == null) { return null; } List<String> messages = new ArrayList<>(); while (numMessages > 0) { Message message = queue.poll(); if (message == null) { throw new RuntimeException("No more messages, " + numMessages + " remains"); } messages.add((String)((AmqpValue)message.getBody()).getValue()); numMessages--; } return messages; } }
@SuppressWarnings("unchecked") @Override void setBody(Section body) { if (body == null) { initializeEmptyBody(); } else if (body instanceof AmqpValue) { Object o = ((AmqpValue) body).getValue(); if (o == null) { initializeEmptyBody(); } else if (o instanceof Map) { messageBodyMap = (Map<String, Object>) o; super.setBody(body); } else { throw new IllegalStateException("Unexpected message body type: " + body.getClass().getSimpleName()); } } else { throw new IllegalStateException("Unexpected message body type: " + body.getClass().getSimpleName()); } }
@SuppressWarnings("unchecked") @Override void setBody(Section body) { if (body == null) { initializeEmptyBody(); } else if (body instanceof AmqpValue) { Object o = ((AmqpValue) body).getValue(); if (o == null) { initializeEmptyBody(); } else if (o instanceof Map) { messageBodyMap = (Map<String, Object>) o; super.setBody(body); } else { throw new IllegalStateException("Unexpected message body type: " + body.getClass().getSimpleName()); } } else { throw new IllegalStateException("Unexpected message body type: " + body.getClass().getSimpleName()); } }
private String doOperationWithStringResult(String resource, String operation, Object ... parameters) throws TimeoutException { Message response = doOperation(resource, operation, parameters); String payload = (String) ((AmqpValue)response.getBody()).getValue(); JsonArray json = new JsonArray(payload); return json.getString(0); }
public long getQueueMessageCount(String queueName) throws TimeoutException { log.info("Checking message count for queue {} on broker {}", queueName, syncRequestClient.getRemoteContainer()); Message response = doAttribute("queue." + queueName, "messageCount"); String payload = (String) ((AmqpValue)response.getBody()).getValue(); JsonArray json = new JsonArray(payload); return json.getLong(0); }
public String getQueueAddress(String queueName) throws TimeoutException { log.info("Checking queue address for queue {} on broker {}", queueName, syncRequestClient.getRemoteContainer()); Message response = doOperation("queue." + queueName, "getAddress"); String payload = (String) ((AmqpValue)response.getBody()).getValue(); JsonArray json = new JsonArray(payload); return json.getString(0); }
public Set<String> getDivertNames() throws TimeoutException { log.info("Retrieving divert names"); Message response = doOperation("broker", "getDivertNames"); Set<String> diverts = new LinkedHashSet<>(); JsonArray payload = new JsonArray((String)((AmqpValue)response.getBody()).getValue()); for (int i = 0; i < payload.size(); i++) { JsonArray inner = payload.getJsonArray(i); for (int j = 0; j < inner.size(); j++) { diverts.add(inner.getString(j)); } } return diverts; }
private static List<List<String>> collectRouter(SyncRequestClient client, String entityType, List<String> attributeNames) throws Exception { Map<String, Object> properties = new LinkedHashMap<>(); properties.put("operation", "QUERY"); properties.put("entityType", entityType); Map<String, Object> body = new LinkedHashMap<>(); body.put("attributeNames", attributeNames); Message message = Proton.message(); message.setApplicationProperties(new ApplicationProperties(properties)); message.setBody(new AmqpValue(body)); Message response = client.request(message, 10, TimeUnit.SECONDS); AmqpValue value = (AmqpValue) response.getBody(); Map<?,?> values = (Map<?,?>) value.getValue(); @SuppressWarnings("unchecked") List<List<String>> results = (List<List<String>>) values.get("results"); return results; } }
/** * Return an AMQP_UNSUBSCRIBE message from the raw AMQP one * * @param message raw AMQP message * @return AMQP_UNSUBSCRIBE message */ @SuppressWarnings("unchecked") public static AmqpUnsubscribeMessage from(Message message) { if (!message.getSubject().equals(AMQP_SUBJECT)) { throw new IllegalArgumentException(String.format("AMQP message subject is no %s", AMQP_SUBJECT)); } Section section = message.getBody(); if ((section != null) && (section instanceof AmqpValue)) { List<String> topics = (List<String>) ((AmqpValue) section).getValue(); return new AmqpUnsubscribeMessage(AmqpHelper.getClientIdFromPublishAddress((String) message.getCorrelationId()), topics); } else { throw new IllegalArgumentException("AMQP message wrong body type"); } }
public Set<String> getConnectorNames() throws TimeoutException { log.info("Retrieving conector names for broker {}", syncRequestClient.getRemoteContainer()); Message response = doOperation("broker", "getConnectorServices"); Set<String> connectors = new LinkedHashSet<>(); JsonArray payload = new JsonArray((String)((AmqpValue)response.getBody()).getValue()); for (int i = 0; i < payload.size(); i++) { JsonArray inner = payload.getJsonArray(i); for (int j = 0; j < inner.size(); j++) { String connector = inner.getString(j); if (!connector.equals("amqp-connector")) { connectors.add(connector); } } } return connectors; } }
public Set<String> getQueueNames() throws TimeoutException { log.info("Retrieving queue names for broker {}", syncRequestClient.getRemoteContainer()); Message response = doOperation("broker", "getQueueNames"); Set<String> queues = new LinkedHashSet<>(); JsonArray payload = new JsonArray((String)((AmqpValue)response.getBody()).getValue()); for (int i = 0; i < payload.size(); i++) { JsonArray inner = payload.getJsonArray(i); for (int j = 0; j < inner.size(); j++) { String queueName = inner.getString(j); if (!queueName.equals(syncRequestClient.getReplyTo())) { queues.add(queueName); } } } return queues; }
private void doJSON_to_AMQP_VerifyStringBodyTestImpl(boolean setBodyType) { String testContent = "myTestContent"; JsonObject jsonObject = new JsonObject(); jsonObject.put(AmqpConstants.BODY, testContent); if(setBodyType){ jsonObject.put(AmqpConstants.BODY_TYPE, AmqpConstants.BODY_TYPE_VALUE); } Message protonMsg = translator.convertToAmqpMessage(jsonObject); assertNotNull("Expected converted msg", protonMsg); Section body = protonMsg.getBody(); assertTrue("Unexpected body type", body instanceof AmqpValue); assertEquals("Unexpected message body value", testContent, ((AmqpValue) body).getValue()); }
@Override public void write(AmqpValue value) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); getEncoder().writeObject(value.getValue()); }