if(!sender.sendQueueFull()) { final int msgNum = sent.incrementAndGet(); Message message = message("Hello " + msgNum);
int msgNum = sent.incrementAndGet(); System.out.println("Sending message " + msgNum + " to client, for address: " + remoteSource.getAddress()); Message m = message("Hello " + msgNum + " from Server!"); sender.send(m, delivery -> { System.out.println("Message " + msgNum + " was received by the client.");
/** * Creates a Message object with the given String contained as an AmqpValue body, and the 'to' address set as given. * * @param address * the 'to' address to set * @param body * the string to set as an AmqpValue body * @return the message */ public static Message message(String address, String body) { Message value = message(body); value.setAddress(address); return value; }
/** * Return a raw AMQP message * * @return */ public Message toAmqp() { Message message = ProtonHelper.message(); message.setSubject(AMQP_SUBJECT); message.setCorrelationId(String.format(AmqpHelper.AMQP_CLIENT_PUBLISH_ADDRESS_TEMPLATE, this.clientId)); return message; }
/** * Return a raw AMQP message * * @return */ public Message toAmqp() { Message message = ProtonHelper.message(); message.setSubject(AMQP_SUBJECT); message.setCorrelationId(String.format(AmqpHelper.AMQP_CLIENT_PUBLISH_ADDRESS_TEMPLATE, this.clientId)); message.setReplyTo(String.format(AmqpHelper.AMQP_CLIENT_CONTROL_ADDRESS_TEMPLATE, this.clientId)); return message; }
/** * Return a raw AMQP message * * @return */ public Message toAmqp() { Message message = ProtonHelper.message(); message.setSubject(AMQP_SUBJECT); message.setMessageId(this.messageId); return message; }
/** * Creates a Message object with the given String contained as an AmqpValue body. * * @param body * the string to set as an AmqpValue body * @return the message */ public static Message message(String body) { Message value = message(); value.setBody(new AmqpValue(body)); return value; }
@Override public final Future<ProtonDelivery> send(final String deviceId, final Map<String, ?> properties, final byte[] payload, final String contentType, final String registrationAssertion) { Objects.requireNonNull(deviceId); Objects.requireNonNull(payload); Objects.requireNonNull(contentType); Objects.requireNonNull(registrationAssertion); final Message msg = ProtonHelper.message(); msg.setAddress(getTo(deviceId)); MessageHelper.setPayload(msg, contentType, payload); setApplicationProperties(msg, properties); addProperties(msg, deviceId, registrationAssertion); return send(msg); }
/** * Return a raw AMQP message * * @return */ public Message toAmqp() { Message message = ProtonHelper.message(); message.setSubject(AMQP_SUBJECT); message.setCorrelationId(String.format(AmqpHelper.AMQP_CLIENT_PUBLISH_ADDRESS_TEMPLATE, this.clientId)); message.setBody(new AmqpValue(this.topics)); return message; }
/** * Verifies that the endpoint forwards a request message via the event bus. */ @Test public void testProcessMessageSendsRequestViaEventBus() { final Message msg = ProtonHelper.message(); msg.setMessageId("4711"); msg.setSubject(RegistrationConstants.ACTION_ASSERT); msg.setBody(new AmqpValue(new JsonObject().put("temp", 15).encode())); MessageHelper.annotate(msg, resource); endpoint.processRequest(msg, resource, Constants.PRINCIPAL_ANONYMOUS); verify(eventBus).send(eq(RegistrationConstants.EVENT_BUS_ADDRESS_REGISTRATION_IN), any(JsonObject.class), any(DeliveryOptions.class)); } }
/** * Verifies that the registration assertion is set on a downstream message. */ @Test public void testAddPropertiesAddsRegistrationAssertion() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token")); assertThat(MessageHelper.getRegistrationAssertion(message), is("token")); }
/** * Verifies that the endpoint forwards a request message via the event bus. */ @Test public void testProcessMessageSendsRequestViaEventBus() { final Message msg = ProtonHelper.message(); msg.setMessageId("random-id"); msg.setSubject(TenantConstants.TenantAction.get.toString()); MessageHelper.addTenantId(msg, Constants.DEFAULT_TENANT); MessageHelper.annotate(msg, resource); endpoint.processRequest(msg, resource, Constants.PRINCIPAL_ANONYMOUS); verify(eventBus).send(eq(TenantConstants.EVENT_BUS_ADDRESS_TENANT_IN), any(JsonObject.class), any(DeliveryOptions.class)); } }
/** * Verifies that the fall back content type is set on a downstream message * if no default has been configured for the device. */ @Test public void testAddPropertiesAddsFallbackContentType() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token")); assertThat(MessageHelper.getRegistrationAssertion(message), is("token")); assertThat(message.getContentType(), is(AbstractProtocolAdapterBase.CONTENT_TYPE_OCTET_STREAM)); }
private static Message givenAValidMessageWithoutBody(final CredentialsConstants.CredentialsAction action) { final Message msg = ProtonHelper.message(); msg.setMessageId("msg"); msg.setReplyTo("reply"); msg.setSubject(action.toString()); return msg; } }
private Message givenAMessageHavingProperties(final TenantConstants.TenantAction action) { final Message msg = ProtonHelper.message(); msg.setMessageId("msg"); msg.setReplyTo("reply"); msg.setSubject(action.toString()); return msg; } }
/** * Verifies that the registered default content type is set on a downstream message. */ @Test public void testAddPropertiesAddsDefaultContentType() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token", "application/hono")); assertThat(MessageHelper.getRegistrationAssertion(message), is("token")); assertThat(message.getContentType(), is("application/hono")); }
private static Message givenAMessageHavingProperties(final String deviceId, final String action) { final Message msg = ProtonHelper.message(); msg.setMessageId("msg-id"); msg.setReplyTo("reply"); msg.setSubject(action); if (deviceId != null) { MessageHelper.addDeviceId(msg, deviceId); } return msg; } }
/** * Verifies that the registered default content type is not set on a downstream message * that already contains a content type. */ @Test public void testAddPropertiesDoesNotAddDefaultContentType() { final Message message = ProtonHelper.message(); message.setContentType("application/existing"); adapter.addProperties(message, newRegistrationAssertionResult("token", "application/hono")); assertThat(MessageHelper.getRegistrationAssertion(message), is("token")); assertThat(message.getContentType(), is("application/existing")); }
/** * Verifies that the adapter's name is set on a downstream message. */ @Test public void testAddPropertiesAddsAdapterName() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token")); assertThat( MessageHelper.getApplicationProperty( message.getApplicationProperties(), MessageHelper.APP_PROPERTY_ORIG_ADAPTER, String.class), is(ADAPTER_NAME)); }
/** * Verifies that the registration assertion is not set on a downstream message * if the downstream peer does not require it. */ @Test public void testAddPropertiesOmitsRegistrationAssertion() { final Message message = ProtonHelper.message(); adapter.addProperties(message, newRegistrationAssertionResult("token"), false); assertNull(MessageHelper.getRegistrationAssertion(message)); }