use of joynr.ImmutableMessage in project joynr by bmwcarit.
the class CcMessageRouterTest method testMessageProcessedListenerCalled.
private void testMessageProcessedListenerCalled(MessageRouter messageRouter, Class<? extends Exception> expectedException) throws Exception {
final Semaphore semaphore = new Semaphore(0);
final ImmutableMessage immutableMessage = joynrMessage.getImmutableMessage();
final MessageProcessedListener mockMessageProcessedListener = mock(MessageProcessedListener.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
semaphore.release();
return null;
}
}).when(mockMessageProcessedListener).messageProcessed(anyString());
messageRouter.registerMessageProcessedListener(mockMessageProcessedListener);
if (expectedException == null) {
messageRouter.route(immutableMessage);
} else {
try {
messageRouter.route(immutableMessage);
fail("Expected exception of type " + expectedException);
} catch (Exception e) {
assertEquals(expectedException, e.getClass());
}
}
semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS);
verify(mockMessageProcessedListener).messageProcessed(eq(immutableMessage.getId()));
}
use of joynr.ImmutableMessage in project joynr by bmwcarit.
the class CcMessageRouterTest method retryRoutingWith1msDelay.
private ImmutableMessage retryRoutingWith1msDelay(MessageRouter messageRouter, int ttlMs) throws Exception {
doThrow(new JoynrDelayMessageException(1, "test")).when(messagingStubMock).transmit(any(ImmutableMessage.class), any(SuccessAction.class), any(FailureAction.class));
joynrMessage.setTtlMs(ExpiryDate.fromRelativeTtl(ttlMs).getValue());
joynrMessage.setTtlAbsolute(true);
ImmutableMessage immutableMessage = joynrMessage.getImmutableMessage();
messageRouter.route(immutableMessage);
Thread.sleep(100);
return immutableMessage;
}
use of joynr.ImmutableMessage in project joynr by bmwcarit.
the class LongPollChannel method notifyDispatcher.
private void notifyDispatcher(String responseBody) {
if (responseBody == null || responseBody.length() <= 0) {
return;
}
// the response body could contain multiple SMRF messages
List<ImmutableMessage> listOfMessages;
try {
listOfMessages = Utilities.splitSMRF(responseBody.getBytes(Charsets.UTF_8));
} catch (EncodingException | UnsuppportedVersionException e) {
logger.error("Failed to split and deserialize SMRF messages: {}", e.getMessage());
return;
}
logger.info("LongPollingChannel CHANNEL: {} messages received: {}", listOfMessages.size());
for (final ImmutableMessage message : listOfMessages) {
messageReceiverExecutor.execute(new Runnable() {
@Override
public void run() {
logger.info("ARRIVED {} messageId: {} type: {} from: {} to: {} header: {}", new Object[] { httpget.getURI().toString(), message.getId(), message.getType(), message.getSender(), message.getRecipient(), message.getHeaders().toString() });
logger.debug("\r\n<<<<<<<<<<<<<<<<<\r\n:{}", message);
messageArrivedListener.messageArrived(message);
}
});
}
}
use of joynr.ImmutableMessage in project joynr by bmwcarit.
the class LongPollingMessagingDelegate method postMessage.
/**
* Posts a message to a long polling channel.
*
* @param ccid
* the identifier of the long polling channel
* @param serializedMessage
* the message to send serialized as a SMRF message
* @return the path segment for the message status. The path, appended to
* the base URI of the messaging service, can be used to query the
* message status
*
* @throws JoynrHttpException
* if one of:
* <ul>
* <li>ccid is not set</li>
* <li>the message has expired or not expiry date is set</li>
* <li>no channel registered for ccid</li>
* </ul>
*/
public String postMessage(String ccid, byte[] serializedMessage) {
ImmutableMessage message;
try {
message = new ImmutableMessage(serializedMessage);
} catch (EncodingException | UnsuppportedVersionException e) {
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_DESERIALIZATIONFAILED);
}
if (ccid == null) {
log.error("POST message {} to cluster controller: NULL. Dropped because: channel Id was not set.", message.getId());
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_CHANNELNOTSET);
}
// send the message to the receiver.
if (message.getTtlMs() == 0) {
log.error("POST message {} to cluster controller: {} dropped because: expiry date not set", ccid, message.getId());
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_EXPIRYDATENOTSET);
}
// Relative TTLs are not supported yet.
if (!message.isTtlAbsolute()) {
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_RELATIVE_TTL_UNSPORTED);
}
if (message.getTtlMs() < System.currentTimeMillis()) {
log.warn("POST message {} to cluster controller: {} dropped because: TTL expired", ccid, message.getId());
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_EXPIRYDATEEXPIRED);
}
// look for an existing broadcaster
Broadcaster ccBroadcaster = BroadcasterFactory.getDefault().lookup(Broadcaster.class, ccid, false);
if (ccBroadcaster == null) {
// if the receiver has never registered with the bounceproxy
// (or his registration has expired) then return 204 no
// content.
log.error("POST message {} to cluster controller: {} dropped because: no channel found", ccid, message.getId());
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_CHANNELNOTFOUND);
}
if (ccBroadcaster.getAtmosphereResources().size() == 0) {
log.debug("no poll currently waiting for channelId: {}", ccid);
}
ccBroadcaster.broadcast(message);
return "messages/" + message.getId();
}
use of joynr.ImmutableMessage in project joynr by bmwcarit.
the class JeeMessagingEndpoint method postMessage.
/**
* Receives a binary encoded {@link ImmutableMessage} which it then routes to the {@link #messageReceiver message
* receiver} in the joynr runtime.
*
* @param channelId
* channel id of the receiver.
* @param serializedMessage
* a serialized SMRF message being sent.
* @param uriInfo
* the URI Information for the request being processed.
* @return a location for querying the message status.
*/
@POST
@Path("/{channelId: [A-Z,a-z,0-9,_,\\-,\\.]+}/message")
@Produces({ MediaType.APPLICATION_OCTET_STREAM })
public Response postMessage(@PathParam("channelId") String channelId, byte[] serializedMessage, @Context UriInfo uriInfo) {
if (LOG.isDebugEnabled()) {
LOG.debug("Incoming message:\n" + new String(serializedMessage, Charsets.UTF_8));
}
try {
ImmutableMessage message;
try {
message = new ImmutableMessage(serializedMessage);
} catch (EncodingException | UnsuppportedVersionException exception) {
LOG.error("Failed to deserialize message for channelId {}: {}", channelId, exception.getMessage());
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_DESERIALIZATIONFAILED);
}
if (LOG.isDebugEnabled()) {
LOG.debug("POST to channel: {} message: {}", channelId, message);
}
if (channelId == null) {
LOG.error("POST message to channel: NULL. message: {} dropped because: channel Id was not set", message);
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_CHANNELNOTSET);
}
if (message.getTtlMs() == 0) {
LOG.error("POST message to channel: {} message: {} dropped because: TTL not set", channelId, message);
throw new JoynrHttpException(Status.BAD_REQUEST, JOYNRMESSAGINGERROR_EXPIRYDATENOTSET);
}
if (messageReceiver == null) {
LOG.error("POST message to channel: {} message: {} no receiver for the given channel", channelId, message);
return Response.noContent().build();
}
if (LOG.isTraceEnabled()) {
LOG.trace("passing off message to messageReceiver: {}", channelId);
}
messageReceiver.receive(message);
URI location = uriInfo.getBaseUriBuilder().path("messages/" + message.getId()).build();
return Response.created(location).build();
} catch (WebApplicationException e) {
throw e;
} catch (Exception e) {
LOG.error(format("POST message to channel: %s error: %s", channelId, e.getMessage()), e);
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
}
Aggregations